from django.conf import settings from django.contrib.auth.models import User from django.db import models import pytz # GENERAL Methods and Classes # def get_userprofile(user): try: profile = user.userprofile except UserProfile.DoesNotExist: profile = UserProfile(user=user) profile.save() return profile def UserprofilerExist(username, data): try: profile = UserProfile.objects.get(user__username=username) except UserProfile.DoesNotExist: return False return profile.timezone == data['timezone'] and profile.language_code == data['language_code'] # USERPROFILE Model # class UserProfile(models.Model): user = models.OneToOneField(User, unique=True, on_delete=models.CASCADE) timezone = models.CharField(max_length=150, default='UTC', choices=[(t, t) for t in pytz.common_timezones]) language_code = models.CharField(max_length=150, default='en', choices=settings.LANGUAGES) def export_key(self): return self.user.username def export_data(self): rv = {} rv['timezone'] = self.timezone rv['language_code'] = self.language_code return rv def import_data(self, data): self.timezone = data['timezone'] self.language_code = data['language_code'] self.save()