text: from django.contrib.auth.models import User
  from django.db.models.signals import post_save
  from django.db import models
  from django.conf import settings
  import os.path
  from activities.models import Notification
  import urllib, hashlib
  
  class Profile(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      location = models.CharField(max_length=50, null=True, blank=True)
      url = models.CharField(max_length=50, null=True, blank=True)
      job_title = models.CharField(max_length=50, null=True, blank=True)
      #reputation = models.IntegerField(default=0)
      #language = models.CharField(max_length=5, default='en')
  
      class Meta:
          db_table = 'auth_profile'
  
      def get_url(self):
          url = self.url
          if "http://" not in self.url and "https://" not in self.url and len(self.url) > 0:
              url = "http://" + str(self.url)
          return url 
  
      def get_picture(self):
          no_picture = 'https://upload.wikimedia.org/wikipedia/commons/7/70/User_icon_BLACK-01.png'
          try:
              filename = settings.MEDIA_ROOT / 'profile_pictures' / f'{self.user.username}.jpg'
              picture_url = settings.MEDIA_URL + 'profile_pictures/' + self.user.username + '.jpg'
              if os.path.isfile(filename):
                  return picture_url
              else:
                  gravatar_url = u'http://www.gravatar.com/avatar/{0}?{1}'.format(
                      hashlib.md5(self.user.email.lower().encode('utf-8')).hexdigest(),
                      urllib.parse.urlencode({'d':'identicon', 's':'256'})
                      )
                  return gravatar_url
          except Exception as e:
              print(e)
              return no_picture
  
      def get_screen_name(self):
          try:
              if self.user.get_full_name():
                  return self.user.get_full_name()
              else:
                  return self.user.username
          except:
              return self.user.username
  
      def notify_liked(self, feed):
          if self.user != feed.user:
              Notification(notification_type=Notification.LIKED,
                  from_user=self.user,
                  to_user=feed.user,
                  feed=feed).save()
  
      def unotify_liked(self, feed):
          if self.user != feed.user:
              Notification.objects.filter(notification_type=Notification.LIKED,
                  from_user=self.user, 
                  to_user=feed.user, 
                  feed=feed).delete()
  
      def notify_commented(self, feed):
          if self.user != feed.user:
              Notification(notification_type=Notification.COMMENTED,
                          from_user=self.user,
                          to_user=feed.user,
                          feed=feed).save()
  
      def notify_also_commented(self, feed):
          comments = feed.get_comments()
          users = []
          for comment in comments:
              if comment.user != self.user and comment.user != feed.user:
                  users.append(comment.user.pk)
          users = list(set(users))
          for user in users:
              Notification(notification_type=Notification.ALSO_COMMENTED,
                  from_user=self.user,
                  to_user=User(id=user),
                  feed=feed).save()
  
      def notify_favorited(self, question):
          if self.user != question.user:
              Notification(notification_type=Notification.FAVORITED,
                  from_user=self.user, 
                  to_user=question.user, 
                  question=question).save()
  
      def unotify_favorited(self, question):
          if self.user != question.user:
              Notification.objects.filter(notification_type=Notification.FAVORITED,
                  from_user=self.user, 
                  to_user=question.user, 
                  question=question).delete()
  
      def notify_answered(self, question):
          if self.user != question.user:
              Notification(notification_type=Notification.ANSWERED,
                  from_user=self.user, 
                  to_user=question.user, 
                  question=question).save()
      
      def notify_accepted(self, answer):
          if self.user != answer.user:
              Notification(notification_type=Notification.ACCEPTED_ANSWER,
                  from_user=self.user, 
                  to_user=answer.user, 
                  answer=answer).save()
      
      def unotify_accepted(self, answer):
          if self.user != answer.user:
              Notification.objects.filter(notification_type=Notification.ACCEPTED_ANSWER,
                  from_user=self.user, 
                  to_user=answer.user, 
                  answer=answer).delete()
  
  def create_user_profile(sender, instance, created, **kwargs):
      if created:
          Profile.objects.create(user=instance)
  
  def save_user_profile(sender, instance, **kwargs):
      instance.profile.save()
  
  post_save.connect(create_user_profile, sender=User)
  post_save.connect(save_user_profile, sender=User)