Django Library Users
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. def export_key(self):
  28. return self.user.username
  29. def export_data(self):
  30. rv = {}
  31. rv['timezone'] = self.timezone
  32. rv['language_code'] = self.language_code
  33. return rv
  34. def import_data(self, data):
  35. self.timezone = data['timezone']
  36. self.language_code = data['language_code']
  37. self.save()