Temporary admin messages (MOTD)

 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
61
62
63
64
65
#models.py
from datetime import datetime
from django.db import models
from django.utils.translation import gettext_lazy as _

class MotdMessageManager(models.Manager):
    def active_messages(self):
        dtnow = datetime.utcnow()
        return super(MotdMessageManager, self).get_query_set().filter(
            enabled=True, start_time__lte=dtnow, end_time__gte=dtnow)

class MotdMessage(models.Model):
    enabled = models.BooleanField(default=True)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    name = models.CharField(_('name'), unique=True, max_length=100)
    content = models.TextField(_('content'), blank=True)
    
    objects = MotdMessageManager()
    
    class Meta:
        ordering = ['start_time']
    
    def __unicode__(self):
        return self.name


#views.py
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from models import MotdMessage

def motd(request, template_name='motd/messages.html', redirect_field_name=REDIRECT_FIELD_NAME):
    redirect_to = request.REQUEST.get(redirect_field_name, '')
    if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
        redirect_to = settings.LOGIN_REDIRECT_URL
    messages = MotdMessage.objects.active_messages()
    if not messages:
        return HttpResponseRedirect(redirect_to)
    return render_to_response(template_name, {redirect_field_name: redirect_to,
                'messages': messages}, context_instance=RequestContext(request))

#admin.py
from django.contrib import admin
import models

admin.site.register(models.MotdMessage)

#templates/motd/messages.html
{% extends "base.html" %}

{% block content %}

{% for message in messages %}
<h2>{{ message.name }}</h2>
<p>{{ message.content|safe }}</p>
<br/>
{% endfor %}

<a href="{{ next }}">Continue</a>

{% endblock %}

More like this

  1. Django csrf_token Template Tag Fix by Reustle 1 year, 6 months ago
  2. load m2m fields objects by dirol 1 year, 8 months ago
  3. Dynamically change a form select widget to a hidden widget by epicserve 2 years, 7 months ago
  4. Fieldsets for Views by Nad/ 2 years ago
  5. Hidden Forms by insin 4 years, 7 months ago

Comments

jamesgpearce (on June 25, 2009):

Useful, but a bit tricky to wireup:

1) mention you need to add it to your urls.py

2) the {% url %} parameter needs to match your own module name

3) the messages.html template needs to be created (or just use that instead of base.html)

4) 'messages' is already used in admin context for something else (the update alerts), so you need to change that to a different name

Works OK in the end, though.

#

(Forgotten your password?)