Django Library Users
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

models.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from django.conf import settings
  2. from django.contrib.auth.models import User
  3. from django.db import models
  4. import pytz
  5. # GENERAL Methods and Classes
  6. #
  7. def get_userprofile(user):
  8. try:
  9. profile = user.userprofile
  10. except UserProfile.DoesNotExist:
  11. profile = UserProfile(user=user)
  12. profile.save()
  13. return profile
  14. def UserprofilerExist(username, data):
  15. try:
  16. profile = UserProfile.objects.get(user__username=username)
  17. except UserProfile.DoesNotExist:
  18. return False
  19. return profile.timezone == data['timezone'] and profile.language_code == data['language_code']
  20. # USERPROFILE Model
  21. #
  22. class UserProfile(models.Model):
  23. user = models.OneToOneField(User, unique=True, on_delete=models.CASCADE)
  24. timezone = models.CharField(max_length=150, default='UTC', choices=[(t, t) for t in pytz.common_timezones])
  25. language_code = models.CharField(max_length=150, default='en', choices=settings.LANGUAGES)
  26. mail_validated = models.BooleanField(default=False)
  27. mail_pending = models.EmailField(null=True)
  28. def export_key(self):
  29. return self.user.username
  30. def export_data(self):
  31. rv = {}
  32. rv['timezone'] = self.timezone
  33. rv['language_code'] = self.language_code
  34. return rv
  35. def import_data(self, data):
  36. self.timezone = data['timezone']
  37. self.language_code = data['language_code']
  38. self.save()