Untitled
raw download clone
PYTHON
views 13
,
size 8887 b
from rest_framework import fields
from rest_framework.serializers import ModelSerializer
from .models import *
from rest_framework import serializers
from rest_framework.fields import CurrentUserDefault
import re


class CustomUserSerializer(serializers.ModelSerializer):

    automated_accounts = serializers.ReadOnlyField(source="accounts")
    token = serializers.ReadOnlyField(source="user_token")
    password = serializers.CharField(min_length=8)

    def create(self, validated_data):
        user = CustomUser.objects.create_user(
            validated_data["username"],
            validated_data["email"],
            validated_data["password"],
        )

        user.phone = validated_data["phone"]
        user.firstname = validated_data["firstname"]
        user.lastname = validated_data["lastname"]

        user.save()
        try:
            Referal_list.objects.create(
                user=CustomUser.objects.get(username__iexact=user.referer_username),
                username=user.username,
            )
            user.referer_username = validated_data["referer_username"]
        except:
            pass
        return user

    def validate(self, data):
        errors = {}
        username = data.get("username")
        firstname = data.get("firstname")
        lastname = data.get("lastname")
        email = data.get("email")
        phone = data.get("phone")

        if not (re.search("^[A-Za-z]{3,32}$", firstname)):
            errors["error"] = "Please Enter Valid First Name"
            raise serializers.ValidationError(errors)

        elif not (re.search("^[A-Za-z]{3,32}$", lastname)):
            errors["error"] = "Please Enter Valid Last Name"
            raise serializers.ValidationError(errors)

        elif not (re.search("^[A-Za-z|0-9]{3,32}$", username)):
            errors["error"] = "Please Enter Valid Username"
            raise serializers.ValidationError(errors)

        elif not (re.search("^([0]{1})([7-9]{1})([0|1]{1})([0-9]{8})", phone)):
            errors["error"] = "Please Enter Valid phone"
            raise serializers.ValidationError(errors)

        elif not phone.isdigit() or len(phone) > 11:
            errors[
                "error"
            ] = "invalid mobile number ,phone number without country code i.e 090,081,070 !"

            raise serializers.ValidationError(errors)

        elif not email.endswith(("@gmail.com", "@yahoo.com")):
            errors["error"] = "We accept only valid gmail or yahoo mail account"
            raise serializers.ValidationError(errors)

        return data

    class Meta:
        model = CustomUser
        fields = (
            "password",
            "id",
            "email",
            "username",
            "firstname",
            "lastname",
            "transaction_pin",
            "passport_photogragh",
            "phone",
            "referal_username",
            "wallet_balance",
            "bonus_balance",
            "user_group",
            "kyc_verify",
            "email_verify",
            "phone_verify",
            "allow_two_factor_auth",
            "allow_email_notification",
            "allow_sms_notification",
            "automated_accounts",
            "token",
        )


class TransactionSerializer(ModelSerializer):

    service = serializers.SerializerMethodField(read_only=True)
    response_data = serializers.SerializerMethodField(read_only=True)

    def get_service(self, obj):
        return obj.service_type.service_name

    def get_response_data(self, obj):
        if obj.service_data:
            return json.loads(obj.service_data)
        else:
            return {}

    class Meta:
        model = Transaction
        fields = (
            "service",
            "service_logo",
            "description",
            "service_logo",
            "description",
            "transaction_type",
            "amount_paid",
            "previous_balance",
            "new_balance",
            "status",
            "created_date",
            "reference",
            "customer_reference",
            "response_data",
        )


class KycSerializer(ModelSerializer):
    class Meta:
        model = KYC
        fields = (
            "customer_name",
            "bvn",
            "account",
            "utility_bill",
            "identity_card",
            "status",
            "bank_name",
            "bank_code",
            "idcardstatus",
            "idcardmessage",
            "utilitystatus",
            "utilitymessage",
            "created_date",
        )


class PlanTypesSerializer(serializers.ModelSerializer):
    class Meta:
        model = PlanTypes
        fields = ("name",)


class NetworkSerializer(serializers.ModelSerializer):

    vtu_percent = serializers.SerializerMethodField(read_only=True)
    sns_percent = serializers.SerializerMethodField(read_only=True)
    awfu_percent = serializers.SerializerMethodField(read_only=True)
    dataplan_type = PlanTypesSerializer(many=True, read_only=True)

    def get_vtu_percent(self, obj):

        request = self.context
        if request["user"].user_group:
            if request["user"].user_group.networks.filter(network=int(obj.id)).exists():
                group = (
                    request["user"]
                    .user_group.networks.filter(network=int(obj.id))
                    .first()
                )

                return group.vtu_percentage

            else:
                return obj.vtu_percentage
        else:
            return obj.vtu_percentage

    def get_sns_percent(self, obj):
        request = self.context
        if request["user"].user_group:
            if request["user"].user_group.networks.filter(network=int(obj.id)).exists():
                group = (
                    request["user"]
                    .user_group.networks.filter(network=int(obj.id))
                    .first()
                )

                return group.sns_percentage

            else:
                return obj.sns_percentage
        else:
            return obj.sns_percentage

    def get_awfu_percent(self, obj):
        request = self.context
        if request["user"].user_group:
            if request["user"].user_group.networks.filter(network=int(obj.id)).exists():
                group = (
                    request["user"]
                    .user_group.networks.filter(network=int(obj.id))
                    .first()
                )

                return group.awfu_percentage

            else:
                return obj.awfu_percentage
        else:
            return obj.awfu_percentage

    class Meta:
        model = Network
        fields = (
            "name",
            "prefix",
            "dataplan_type",
            "network_logo",
            "airtime_convertion_parcentage",
            "data_disable",
            "vtu_percent",
            "awfu_percent",
            "sns_percent",
            "network_number",
            "vtu_disable",
            "recharge_pin_disable",
            "sns_disable",
            "awuf4u_disable",
        )


class PlanSerializer(serializers.ModelSerializer):

    amount = serializers.SerializerMethodField(read_only=True)
    plan_network = serializers.SerializerMethodField(read_only=True)
    plan = serializers.SerializerMethodField(read_only=True)

    def get_plan_network(self, obj):
        return obj.network.name

    def get_plan(self, obj):
        return str(obj.plan_size) + obj.plan_Volume

    def get_amount(self, obj):
        request = self.context
        if request["user"].user_group:
            if request["user"].user_group.plans.filter(plan=int(obj.id)).exists():
                group = (
                    request["user"].user_group.plans.filter(plan=int(obj.id)).first()
                )

                return group.amount

            else:
                return obj.plan_amount
        else:
            return obj.plan_amount

    class Meta:
        model = Plan
        fields = (
            "id",
            "network",
            "plan_type",
            "network",
            "plan_network",
            "month_validate",
            "plan",
            "amount",
            "available",
        )


class WebsiteConfigurationSerializer(serializers.ModelSerializer):
    class Meta:
        model = WebsiteConfiguration
        fields = [
            "primary_color",
            "secondary_color",
            "support_phone_number",
            "whatsapp_group_link",
            "gmail",
            "manual_bank_funding_info_message",
        ]
close fullscreen
Login or Register to edit or fork this paste. It's free.