0

Intro: I have a 3 models user, post, group. User is able to make posts however each post has to belong to a group. Users have to choose from the existing groups for their posts. Users cannot add, delete, update group's.

Furthermore:

Users can become a member of groups and when they click on a certain group. They see all the posts in that group.

What I want When Users come on the home page they see posts that were added since the last time they logged in

enter image description here

My Models

class Post(models.Model):
    user = models.ForeignKey(User, related_name='posts')
    group = models.ForeignKey(Group, related_name='posts')
    title = models.CharField(max_length=250, unique=True)
    message = models.TextField()
    created_at = models.DateTimeField(auto_now=True)

My Views

class Homepage(TemplateView):
    template_name = 'home.html'

    def get_context_data(self, **kwargs):
        context = super(Homepage, self).get_context_data(**kwargs)  
        if self.request.user.is_authenticated():
            context['object_list'] = Group.objects.filter(members=self.request.user)

            #The last login actually shows the users current login time as the last login. Which makes the below line of code useless
            new_posts = Post.objects.filter(created_at__gt=self.request.user.last_login).count()
            context['new_posts'] = new_posts
         else:
              context['object_list'] = Group.objects.all()
        return context

In my templates I have

<div class="list-group">
   {% for group in object_list %}        
       {% if not new_posts %}
               {{group.post.count}}
        {% else %}
                {{new_posts}}
        {% endif %}
    {% endfor %}
</div>

The Issue: The last login actually shows the users current login time as the last login. Is there a way to get users last logout time instead of last login time example user.last_logout

I have the below answer but it doesn't seem to work for me Django created_at__gt=self.request.user.last_login workinly only on users already signed in.

My profile create function

def signup(request):
    if request.method == 'POST':
        form = UserCreateForm(request.POST or None)
        if form.is_valid():
            new_user = form.save()
            Profile.objects.create(user=new_user)
            return redirect('accounts:edit_profile')
Samir Tendulkar
  • 1,151
  • 3
  • 19
  • 47

1 Answers1

1

here is an example

 class ProfileModel(models.Model):
     #fields
     last_time_logout = models.DateTimeField() 

In a custom view, record the last logout

 import datetime

 def signout(request):
     profile = reques.user.profile # it depends 
     if request.is_ajax(): # Do it with ajax
          logout(request)
          profile.last_time_logout = datetime.datetime.now()
          profile.save()
          return redirect("signin")
     raise PermissionDenied
Lemayzeur
  • 8,297
  • 3
  • 23
  • 50
  • do you think it should be `def signout(request):` ''' logout user ''' `request.user.profile.last_time_logout = datetime.datetime.now()` `request.user.profile.save()` – Samir Tendulkar Dec 18 '18 at 16:00
  • Also will this always record the users logout time. Is `def signout` a django function – Samir Tendulkar Dec 18 '18 at 16:01
  • I name it that way to avoid confusion with the built-in function `logout` of Django. If you use that view to end the session, yes it will always record the last time log out – Lemayzeur Dec 18 '18 at 16:02
  • ok if I want to use it with the built in log out function how will the code look like. Example is it `def logout` or `def signout` – Samir Tendulkar Dec 18 '18 at 16:05
  • I have upvoted your answer. but it did not work for me. Maybe I am doing something wrong, This solution worked for me https://stackoverflow.com/questions/53837536/how-to-save-the-users-last-logout-time. – Samir Tendulkar Dec 18 '18 at 17:30