Пример #1
0
 def create(self, validated_data):
     request = self.context.get("request")
     wallet = request.user.profile.wallet
     if wallet:
         return wallet
     return update_object(Wallet(profile=request.user.profile),
                          validated_data)
Пример #2
0
    def update(self, instance, validated_data):
        user = self._get_request().user
        if instance.user.id != user.id:
            raise serializers.ValidationError(
                {"error": _("You can only edit your own profile.")})
        fullname = validated_data.get('fullname', False)
        updated_instance = update_object(instance, validated_data)

        if validated_data.get('fullname'):
            fullname = fullname.split(' ')
            updated_instance.user.first_name = fullname.pop(0)
            updated_instance.user.last_name = " ".join(el for el in fullname)
            updated_instance.user.save()
        return updated_instance
 def create(self, validated_data):
     profile = self.context.get('profile')
     instance = update_object(Notification(sender=profile), validated_data)
     notification_saved.send(sender=Notification, notification=instance)
     return instance
 def update(self, instance, validated_data):
     updated_instance = update_object(instance, validated_data)
     return updated_instance
    def create(self, validated_data):
        profile = self.context.get('profile')
        now = datetime.datetime.now()
        expiry = validated_data.get('expiry')
        exp_month = ''
        exp_year = ''
        if '/' in expiry:
            expiry.split('/')
            exp_month = expiry.split('/')[0]
            exp_year = str(now.year)[:2] + expiry.split('/')[1]
        # add user as tilled customer
        if not profile.tilled_customer_id:
            customer_result, err = Customers.create({
                "metadata": {
                    "internal_customer_id": str(profile.id)
                },
                "first_name":
                profile.user.first_name or profile.user.username,
                "middle_name":
                "",
                "last_name":
                profile.user.last_name or "",
                "email":
                profile.user.email,
            })
            if err:
                print('Error creating a user')
                logger.warning(err)
            if not err:
                print(customer_result.get('id'),
                      'where to get user details \n')
                profile.tilled_customer_id = customer_result.get('id')
                profile.save()
        # add card as tilled payment method
        result, error = PaymentMethods.create(
            card={
                'cvc': validated_data.get('cvv'),
                'exp_month': int(exp_month),
                'exp_year': int(exp_year),
                'number': validated_data.get('number').replace(' ', '')
            },
            nick_name=validated_data.get('holder'),
            billing_details={
                "address": {
                    "street": "865 Grand Street",
                    "street2": "string",
                    "city": "Brooklyn",
                    "state": "NY",
                    "zip": "11211",
                    "country": "US"
                },
                "email": profile.user.email,
                "name": profile.user.first_name or profile.user.username,
            },
        )

        if error:
            logger.warning(error)
            raise serializers.ValidationError(error)
        printer(result)
        instance = update_object(PaymentCard(profile_id=profile.id),
                                 validated_data)
        instance.tilled_payment_method_id = result.get('id')
        instance.save()
        res, err = PaymentMethods.attach(result.get('id'),
                                         profile.tilled_customer_id)
        if err:
            logger.warning(
                f'Error attaching a payment {result.get("id")} method to profile with Id {profile.tilled_customer_id}'
            )
        return instance