2016-04-23 17:00:20 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2016-03-03 21:55:23 +00:00
|
|
|
from django.db import models
|
2016-03-11 18:42:45 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-04-23 17:00:20 +00:00
|
|
|
from django.contrib.auth.models import User, AbstractBaseUser, BaseUserManager, AbstractUser
|
2016-03-07 16:49:02 +00:00
|
|
|
from django.contrib.auth.hashers import make_password
|
|
|
|
from django.core.mail import send_mail
|
2016-03-11 18:42:45 +00:00
|
|
|
from django.core.validators import RegexValidator
|
2016-03-15 23:26:49 +00:00
|
|
|
from django.contrib.auth.models import User
|
2016-04-23 17:00:20 +00:00
|
|
|
from django.contrib.sites.models import Site
|
2016-05-01 12:13:12 +00:00
|
|
|
|
2016-04-26 06:16:03 +00:00
|
|
|
from utils.stripe_utils import StripeUtils
|
2016-03-03 21:55:23 +00:00
|
|
|
|
2016-03-07 16:49:02 +00:00
|
|
|
REGISTRATION_MESSAGE = {'subject': "Validation mail",
|
2016-04-28 17:42:01 +00:00
|
|
|
'message': 'Please validate Your account under this link http://localhost:8000/en-us/digitalglarus/login/validate/{}',
|
2016-03-07 16:49:02 +00:00
|
|
|
'from': 'test@test.com'}
|
|
|
|
|
|
|
|
|
|
|
|
class MyUserManager(BaseUserManager):
|
|
|
|
def create_user(self, email, name, password=None):
|
|
|
|
"""
|
2016-03-15 23:26:49 +00:00
|
|
|
Creates and saves a User with the given email,name and password.
|
2016-03-07 16:49:02 +00:00
|
|
|
"""
|
|
|
|
if not email:
|
|
|
|
raise ValueError('Users must have an email address')
|
|
|
|
|
|
|
|
user = self.model(
|
|
|
|
email=self.normalize_email(email),
|
2016-03-16 14:10:48 +00:00
|
|
|
name=name,
|
|
|
|
validation_slug=make_password(None)
|
2016-03-07 16:49:02 +00:00
|
|
|
)
|
2016-05-02 00:00:38 +00:00
|
|
|
user.is_admin = False
|
2016-03-19 20:28:58 +00:00
|
|
|
user.set_password(password)
|
2016-03-07 16:49:02 +00:00
|
|
|
user.save(using=self._db)
|
|
|
|
return user
|
|
|
|
|
|
|
|
def create_superuser(self, email, name, password):
|
|
|
|
"""
|
2016-03-15 23:26:49 +00:00
|
|
|
Creates and saves a superuser with the given email, name and password.
|
2016-03-07 16:49:02 +00:00
|
|
|
"""
|
|
|
|
user = self.create_user(email,
|
|
|
|
password=password,
|
2016-03-16 14:10:48 +00:00
|
|
|
name=name,
|
2016-03-07 16:49:02 +00:00
|
|
|
)
|
|
|
|
user.is_admin = True
|
|
|
|
user.save(using=self._db)
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
class CustomUser(AbstractBaseUser):
|
|
|
|
VALIDATED_CHOICES = ((0, 'Not validated'), (1, 'Validated'))
|
2016-04-23 17:00:20 +00:00
|
|
|
site = models.ForeignKey(Site, default=1)
|
2016-03-07 16:49:02 +00:00
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
email = models.EmailField(unique=True)
|
|
|
|
|
|
|
|
validated = models.IntegerField(choices=VALIDATED_CHOICES, default=0)
|
|
|
|
validation_slug = models.CharField(db_index=True, unique=True, max_length=50)
|
2016-05-02 00:00:38 +00:00
|
|
|
is_admin = models.BooleanField(
|
2016-03-16 14:10:48 +00:00
|
|
|
_('staff status'),
|
|
|
|
default=False,
|
|
|
|
help_text=_('Designates whether the user can log into this admin site.'),
|
|
|
|
)
|
2016-03-07 16:49:02 +00:00
|
|
|
|
|
|
|
objects = MyUserManager()
|
|
|
|
|
|
|
|
USERNAME_FIELD = "email"
|
2016-03-07 18:19:25 +00:00
|
|
|
REQUIRED_FIELDS = ['name', 'password']
|
2016-03-07 16:49:02 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def register(cls, name, password, email):
|
|
|
|
user = cls.objects.filter(email=email).first()
|
|
|
|
if not user:
|
2016-03-16 14:10:48 +00:00
|
|
|
user = cls.objects.create_user(name=name, email=email, password=password)
|
2016-03-07 16:49:02 +00:00
|
|
|
if user:
|
2016-03-07 18:19:25 +00:00
|
|
|
send_mail(REGISTRATION_MESSAGE['subject'],
|
|
|
|
REGISTRATION_MESSAGE['message'].format(user.validation_slug),
|
2016-03-07 16:49:02 +00:00
|
|
|
REGISTRATION_MESSAGE['from'], [user.email], fail_silently=False)
|
|
|
|
return user
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate_url(cls, validation_slug):
|
|
|
|
user = cls.objects.filter(validation_slug=validation_slug).first()
|
|
|
|
if user:
|
|
|
|
user.validated = 1
|
2016-03-16 14:10:48 +00:00
|
|
|
user.save()
|
2016-03-07 16:49:02 +00:00
|
|
|
return True
|
|
|
|
return False
|
2016-03-07 18:19:25 +00:00
|
|
|
|
2016-03-07 17:39:24 +00:00
|
|
|
def is_superuser(self):
|
2016-03-16 14:10:48 +00:00
|
|
|
return False
|
2016-03-07 18:19:25 +00:00
|
|
|
|
2016-03-07 16:49:02 +00:00
|
|
|
def get_full_name(self):
|
|
|
|
# The user is identified by their email address
|
|
|
|
return self.email
|
|
|
|
|
|
|
|
def get_short_name(self):
|
|
|
|
# The user is identified by their email address
|
|
|
|
return self.email
|
|
|
|
|
|
|
|
def __str__(self): # __unicode__ on Python 2
|
|
|
|
return self.email
|
|
|
|
|
|
|
|
def has_perm(self, perm, obj=None):
|
|
|
|
"Does the user have a specific permission?"
|
|
|
|
# Simplest possible answer: Yes, always
|
2016-05-02 00:00:38 +00:00
|
|
|
return self.is_admin
|
2016-03-07 16:49:02 +00:00
|
|
|
|
|
|
|
def has_module_perms(self, app_label):
|
|
|
|
"Does the user have permissions to view the app `app_label`?"
|
|
|
|
# Simplest possible answer: Yes, always
|
2016-05-02 00:00:38 +00:00
|
|
|
return self.is_admin
|
2016-03-07 16:49:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_staff(self):
|
|
|
|
"Is the user a member of staff?"
|
|
|
|
# Simplest possible answer: All admins are staff
|
|
|
|
return self.is_admin
|
|
|
|
|
2016-03-11 18:42:45 +00:00
|
|
|
|
2016-04-26 06:16:03 +00:00
|
|
|
class StripeCustomer(models.Model):
|
|
|
|
user = models.OneToOneField(CustomUser)
|
|
|
|
stripe_id = models.CharField(unique=True, max_length=100)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_or_create(cls, email=None, token=None):
|
|
|
|
"""
|
|
|
|
Check if there is a registered stripe customer with that email
|
|
|
|
or create a new one
|
|
|
|
"""
|
2016-05-05 06:03:35 +00:00
|
|
|
stripe_customer = None
|
2016-04-26 06:16:03 +00:00
|
|
|
try:
|
2016-05-01 12:13:12 +00:00
|
|
|
stripe_utils = StripeUtils()
|
2016-04-26 06:16:03 +00:00
|
|
|
stripe_customer = cls.objects.get(user__email=email)
|
2016-05-02 00:00:38 +00:00
|
|
|
# check if user is not in stripe but in database
|
|
|
|
stripe_utils.check_customer(stripe_customer.stripe_id, stripe_customer.user, token)
|
2016-04-26 06:16:03 +00:00
|
|
|
return stripe_customer
|
|
|
|
|
|
|
|
except StripeCustomer.DoesNotExist:
|
|
|
|
user = CustomUser.objects.get(email=email)
|
|
|
|
|
|
|
|
stripe_utils = StripeUtils()
|
|
|
|
stripe_data = stripe_utils.create_customer(token, email)
|
2016-04-30 19:29:16 +00:00
|
|
|
stripe_cus_id = stripe_data.get('response_object').get('id')
|
2016-04-26 06:16:03 +00:00
|
|
|
|
|
|
|
stripe_customer = StripeCustomer.objects.\
|
2016-04-30 19:29:16 +00:00
|
|
|
create(user=user, stripe_id=stripe_cus_id)
|
2016-04-26 06:16:03 +00:00
|
|
|
|
|
|
|
return stripe_customer
|
|
|
|
|
2016-05-11 10:02:35 +00:00
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
str cast with a friendly representation of the ContactMessage object
|
|
|
|
"""
|
|
|
|
return "User: %s, Stripe_id: %s" % (self.user, self.stripe_id)
|
|
|
|
|
|
|
|
|
2016-04-26 06:16:03 +00:00
|
|
|
|
2016-03-11 18:42:45 +00:00
|
|
|
class CreditCards(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
user_id = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
|
|
|
|
card_number = models.CharField(max_length=50)
|
|
|
|
expiry_date = models.CharField(max_length=50, validators=[RegexValidator(r'\d{2}\/\d{4}', _(
|
|
|
|
'Use this pattern(MM/YYYY).'))])
|
2016-04-23 17:00:20 +00:00
|
|
|
ccv = models.CharField(max_length=4, validators=[RegexValidator(r'\d{3,4}', _('Wrong CCV number.'))])
|
|
|
|
payment_type = models.CharField(max_length=5, default='N')
|
|
|
|
|
2016-05-01 12:13:12 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
# override saving to database
|
|
|
|
pass
|
|
|
|
|
2016-04-23 17:00:20 +00:00
|
|
|
|
|
|
|
class Calendar(models.Model):
|
|
|
|
datebooked = models.DateField()
|
|
|
|
user = models.ForeignKey(CustomUser)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if kwargs.get('datebooked'):
|
|
|
|
user = kwargs.get('user')
|
|
|
|
kwargs['datebooked'] = datetime.strptime(kwargs.get('datebooked', ''), '%d,%m,%Y')
|
|
|
|
self.user_id = user.id
|
|
|
|
super(Calendar, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
@classmethod
|
2016-05-01 12:13:12 +00:00
|
|
|
def add_dates(cls, dates, user):
|
2016-04-23 17:00:20 +00:00
|
|
|
old_dates = Calendar.objects.filter(user_id=user.id)
|
|
|
|
if old_dates:
|
|
|
|
old_dates.delete()
|
|
|
|
for date in dates:
|
2016-05-01 12:13:12 +00:00
|
|
|
Calendar.objects.create(datebooked=date, user=user)
|