# Path: users/models.py
# class CreditCard(models.Model):
#     """docstring for CreditCard"""
#     user = models.ForeignKey(CustomerUser)
#     owner_name = models.CharField(
#         help_text=_("Preencha como está no " + credit_card_verbose_name),
#         verbose_name=_("Nome do Titular do " + credit_card_verbose_name),
#         max_length=256)
#     card_number = models.CharField(
#         verbose_name=_("Número do " + credit_card_verbose_name),
#         max_length=16)
#     security_code = models.CharField(
#         verbose_name=_("Código de Segurança do " + credit_card_verbose_name),
#         max_length=3)
#     expire_date = models.DateField(
#         verbose_name=_("Validade do " + credit_card_verbose_name))
#     provider = models.CharField(
#         verbose_name=_("Bandeira do " + credit_card_verbose_name),
#         max_length=20)
# 
#     class Meta:
#         verbose_name = _(credit_card_verbose_name)
#         verbose_name_plural = _(credit_card_verbose_name_plural)
# 
#     def __str__(self):
#         return ("************" + self.card_number[-4:])
# 
# class ShippingAddress(models.Model):
#     """docstring for ShippingAddress"""
#     customer = models.ForeignKey(CustomerUser)
#     country = models.CharField(
#         help_text=_(
#             "Preencha com o nome completo do país"
#             " onde esse endereço se encontra."),
#         verbose_name=_("País"),
#         max_length=50)
#     state = models.CharField(
#         help_text=_("Estado ou província onde esse estado se encontra."),
#         verbose_name=_("Estado"),
#         max_length=50)
#     city = models.CharField(
#         help_text=_("Cidade onde esse endereço se encontra."),
#         verbose_name=_("Cidade"),
#         max_length=50)
#     zip_code = models.CharField(
#         help_text=_("Código de Endereço Postal"),
#         verbose_name=_("CEP"),
#         max_length=10, null=True, blank=True)
#     address = models.CharField(
#         help_text=_("Endereço Postal"),
#         verbose_name=_("Endereço"),
#         max_length=256)
#     reference = models.CharField(
#         help_text=_(
#             "Ponto de Referência nas redondezas."
#             " Ex: 'Ao lado da Farmécia'"),
#         verbose_name=_("Ponto de Referência."),
#         max_length=256, null=True, blank=True)
# 
#     class Meta:
#         verbose_name = _('Endereço para Envio')
#         verbose_name_plural = _('Endereços para Envio')
#         ordering = ('country', 'state', 'city', 'zip_code')
# 
#     def __str__(self):
#         return self.zip_code + ' - ' + self.address


from rest_framework import permissions
from django.core.exceptions import ObjectDoesNotExist
from .models import CreditCard, ShippingAddress

class DefaultUserItemsPermissions(permissions.BasePermission):

    def __init__(self):
        self.permission = False
        self.request = None
        self.user_id = ''
        self.card_id = ''
        self.card_user_id = ''
        self.address_id = ''
        self.address_user_id = ''
        self.authorized_user = False

    def has_permission(self, request, view):

        if request.user.is_anonymous:
            return False

        elif request.user.is_superuser:
            return True

        elif 'credit_cards' in request.path:
            self.request = request
            self.user_id = str(self.request.user.id)
            self.card_id = self.request.path.split('/credit_cards/')[1][:-1]

            if self.request.method == 'POST':
                self.permission = True

            try:

# Path: users/models.py
# class CreditCard(models.Model):
#     """docstring for CreditCard"""
#     user = models.ForeignKey(CustomerUser)
#     owner_name = models.CharField(
#         help_text=_("Preencha como está no " + credit_card_verbose_name),
#         verbose_name=_("Nome do Titular do " + credit_card_verbose_name),
#         max_length=256)
#     card_number = models.CharField(
#         verbose_name=_("Número do " + credit_card_verbose_name),
#         max_length=16)
#     security_code = models.CharField(
#         verbose_name=_("Código de Segurança do " + credit_card_verbose_name),
#         max_length=3)
#     expire_date = models.DateField(
#         verbose_name=_("Validade do " + credit_card_verbose_name))
#     provider = models.CharField(
#         verbose_name=_("Bandeira do " + credit_card_verbose_name),
#         max_length=20)
# 
#     class Meta:
#         verbose_name = _(credit_card_verbose_name)
#         verbose_name_plural = _(credit_card_verbose_name_plural)
# 
#     def __str__(self):
#         return ("************" + self.card_number[-4:])
# 
# class ShippingAddress(models.Model):
#     """docstring for ShippingAddress"""
#     customer = models.ForeignKey(CustomerUser)
#     country = models.CharField(
#         help_text=_(
#             "Preencha com o nome completo do país"
#             " onde esse endereço se encontra."),
#         verbose_name=_("País"),
#         max_length=50)
#     state = models.CharField(
#         help_text=_("Estado ou província onde esse estado se encontra."),
#         verbose_name=_("Estado"),
#         max_length=50)
#     city = models.CharField(
#         help_text=_("Cidade onde esse endereço se encontra."),
#         verbose_name=_("Cidade"),
#         max_length=50)
#     zip_code = models.CharField(
#         help_text=_("Código de Endereço Postal"),
#         verbose_name=_("CEP"),
#         max_length=10, null=True, blank=True)
#     address = models.CharField(
#         help_text=_("Endereço Postal"),
#         verbose_name=_("Endereço"),
#         max_length=256)
#     reference = models.CharField(
#         help_text=_(
#             "Ponto de Referência nas redondezas."
#             " Ex: 'Ao lado da Farmécia'"),
#         verbose_name=_("Ponto de Referência."),
#         max_length=256, null=True, blank=True)
# 
#     class Meta:
#         verbose_name = _('Endereço para Envio')
#         verbose_name_plural = _('Endereços para Envio')
#         ordering = ('country', 'state', 'city', 'zip_code')
# 
#     def __str__(self):
#         return self.zip_code + ' - ' + self.address


# Path: users/permissions.py
from rest_framework import permissions
from django.core.exceptions import ObjectDoesNotExist
from .models import CreditCard, ShippingAddress


class DefaultUserItemsPermissions(permissions.BasePermission):

    def __init__(self):
        self.permission = False
        self.request = None
        self.user_id = ''
        self.card_id = ''
        self.card_user_id = ''
        self.address_id = ''
        self.address_user_id = ''
        self.authorized_user = False

    def has_permission(self, request, view):

        if request.user.is_anonymous:
            return False

        elif request.user.is_superuser:
            return True

        elif 'credit_cards' in request.path:
            self.request = request
            self.user_id = str(self.request.user.id)
            self.card_id = self.request.path.split('/credit_cards/')[1][:-1]

            if self.request.method == 'POST':
                self.permission = True

            try:

                card = CreditCard.objects.filter(pk=int(self.card_id))