Templatetags to output users online, last registered users, and the last logins.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from django import template
from django.contrib.auth.models import User
import datetime

register = template.Library()

"""
namefile: usertags.py
You would need a template nest for every method, for example to online_users.
/templates/tag/online_users.html

        {% if users %}
        <ul>
        {% for user in users %}
            <li>{{user.username}}</li>
        {% endfor %}
        </ul>
        {% endif %}

to load

{% load usertags %}
{% online_users 5 %}
{% last_registers 5 %}
{% last_logins 5 %}

"""

@register.inclusion_tag('tags/online_users.html')
def online_users(num):
    """
    Show user that has been login an hour ago.
    """
    one_hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1)
    sql_datetime = datetime.datetime.strftime(one_hour_ago, '%Y-%m-%d %H:%M:%S')
    users = User.objects.filter(last_login__gt=sql_datetime,
                                is_active__exact=1).order_by('-last_login')[:num]
    return {
            'users': users,
    }

@register.inclusion_tag('tags/last_registers.html')
def last_registers(num):
    """
    Show last registered users.
    """
    users = User.objects.filter(is_active__exact=1).order_by('-date_joined')[:num]
    return {
            'users': users,
    }

@register.inclusion_tag('tags/last_logins.html')
def last_logins(num):
    """
    Show last logins ...
    """
    users = User.objects.filter(is_active__exact=1).order_by('-last_login')[:num]
    return {
            'users': users,
    }

More like this

  1. Template tag: Last x twitter msgs of user by coulix 4 years, 9 months ago
  2. DaGood breadcrumbs by drozzy 5 years, 3 months ago
  3. A simple rest template filter by marinho 6 years, 5 months ago
  4. Username genrator by gustavo80br 6 years ago
  5. Default to a static template. by arthurfurlan 4 years, 11 months ago

Comments

(Forgotten your password?)