# Path: dinette/models.py
# class DinetteUserProfile(models.Model):
#     user = models.OneToOneField(User)
#     last_activity = models.DateTimeField(auto_now_add=True)
#     #When was the last session. Used in page activity since last session.
#     last_session_activity = models.DateTimeField(auto_now_add=True)
#     userrank = models.CharField(max_length=30, default="Junior Member")
#     last_posttime = models.DateTimeField(auto_now_add=True)
#     photo = models.ImageField(upload_to='dinette/files', null=True, blank=True)
#     signature = models.CharField(max_length = 1000, null = True, blank = True)
#     slug = models.SlugField(max_length=200, db_index=True, unique=True)
#     is_subscribed_to_digest = models.BooleanField(default=False)
#     
#     def __unicode__(self):
#         return self.user.username
#     
#     #Populate the user fields for easy access
#     @property
#     def username(self):
#         return self.user.username
#     
#     @property
#     def first_name(self):
#         return self.user.first_name
#     
#     @property
#     def last_name(self):
#         return self.user.last_name
#     
#     def get_total_posts(self):
#         print self.user.ftopics_set.count() + self.user.reply_set.count()
#         return self.user.ftopics_set.count() + self.user.reply_set.count()
#     
#     def is_online(self):
#         from django.conf import settings
#         last_online_duration = getattr(settings, 'LAST_ONLINE_DURATION', 900)
#         now = datetime.datetime.now()
#         if (now - self.last_activity).seconds < last_online_duration:
#             return True
#         return False   
# 
#     def getMD5(self):
#         m = hashlib.md5()
#         m.update(self.user.email)        
#         return m.hexdigest()
#     
#     def get_since_last_visit(self):
#         "Topics with new relies since last visit"
#         return Ftopics.objects.get_new_since(self.last_session_activity)
#      
#     @models.permalink
#     def get_absolute_url(self):
#         return ('dinette_user_profile', [self.slug])
#     
#     def save(self, *args, **kwargs):
#         if not self.slug:
#             slug = slugify(self.user.username)
#             if slug:
#                 same_slug_count = self._default_manager.filter(slug__startswith=slug).count()
#                 if same_slug_count:
#                     slug = slug + str(same_slug_count)
#                 self.slug = slug
#             else:
#                 #fallback to user id
#                 slug = self.user.id
#         super(DinetteUserProfile, self).save(*args, **kwargs)


import datetime
from dinette.models import DinetteUserProfile
                from django.conf import settings
    from copy import deepcopy



class UserActivity:
    def process_request(self, req):
        if req.user.is_authenticated():
            #last = req.user.get_profile().last_activity
            try:
                try:
                    user_profile = req.user.dinetteuserprofile
                except DinetteUserProfile.DoesNotExist:
                    now = datetime.datetime.now()

# Path: dinette/models.py
# class DinetteUserProfile(models.Model):
#     user = models.OneToOneField(User)
#     last_activity = models.DateTimeField(auto_now_add=True)
#     #When was the last session. Used in page activity since last session.
#     last_session_activity = models.DateTimeField(auto_now_add=True)
#     userrank = models.CharField(max_length=30, default="Junior Member")
#     last_posttime = models.DateTimeField(auto_now_add=True)
#     photo = models.ImageField(upload_to='dinette/files', null=True, blank=True)
#     signature = models.CharField(max_length = 1000, null = True, blank = True)
#     slug = models.SlugField(max_length=200, db_index=True, unique=True)
#     is_subscribed_to_digest = models.BooleanField(default=False)
#     
#     def __unicode__(self):
#         return self.user.username
#     
#     #Populate the user fields for easy access
#     @property
#     def username(self):
#         return self.user.username
#     
#     @property
#     def first_name(self):
#         return self.user.first_name
#     
#     @property
#     def last_name(self):
#         return self.user.last_name
#     
#     def get_total_posts(self):
#         print self.user.ftopics_set.count() + self.user.reply_set.count()
#         return self.user.ftopics_set.count() + self.user.reply_set.count()
#     
#     def is_online(self):
#         from django.conf import settings
#         last_online_duration = getattr(settings, 'LAST_ONLINE_DURATION', 900)
#         now = datetime.datetime.now()
#         if (now - self.last_activity).seconds < last_online_duration:
#             return True
#         return False   
# 
#     def getMD5(self):
#         m = hashlib.md5()
#         m.update(self.user.email)        
#         return m.hexdigest()
#     
#     def get_since_last_visit(self):
#         "Topics with new relies since last visit"
#         return Ftopics.objects.get_new_since(self.last_session_activity)
#      
#     @models.permalink
#     def get_absolute_url(self):
#         return ('dinette_user_profile', [self.slug])
#     
#     def save(self, *args, **kwargs):
#         if not self.slug:
#             slug = slugify(self.user.username)
#             if slug:
#                 same_slug_count = self._default_manager.filter(slug__startswith=slug).count()
#                 if same_slug_count:
#                     slug = slug + str(same_slug_count)
#                 self.slug = slug
#             else:
#                 #fallback to user id
#                 slug = self.user.id
#         super(DinetteUserProfile, self).save(*args, **kwargs)


# Path: dinette/middleware.py
import datetime
from dinette.models import DinetteUserProfile
                from django.conf import settings
    from copy import deepcopy




class UserActivity:
    def process_request(self, req):
        if req.user.is_authenticated():
            #last = req.user.get_profile().last_activity
            try:
                try:
                    user_profile = req.user.dinetteuserprofile
                except DinetteUserProfile.DoesNotExist:
                    now = datetime.datetime.now()

                    user_profile, created = DinetteUserProfile.objects.get_or_create(user = req.user, last_activity = now, last_session_activity = now)