- {% if messages %} + {% if tip %} +
+
Tip: {{ tip.text|safe }}
+
+ {% endif %} + + {% if messages %}
{% for message in messages %}
{{ message }}
diff --git a/totd/__init__.py b/totd/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/totd/admin.py b/totd/admin.py new file mode 100644 index 0000000..66eaf44 --- /dev/null +++ b/totd/admin.py @@ -0,0 +1,32 @@ +from django.contrib import admin +from django.urls.resolvers import get_resolver +from django import forms + +from .models import Tip + +def url_choices(): + choices = [('', '-- All Pages --')] + for entry in get_resolver(None).url_patterns: + if entry.pattern.name: + choices.append((entry.pattern.name, entry.pattern.name)) + return choices + +# Register your models here. +class TipForm(forms.ModelForm): + class Meta: + model = Tip + fields = '__all__' + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['view'].widget = forms.Select(choices=url_choices()) + +class TipAdmin(admin.ModelAdmin): + #raw_id_fields = ('seen_by',) + list_filter =('level', 'view') + list_display = ('name', 'level', 'view', 'run_start', 'run_end') + search_fields = ('name', 'view') + form = TipForm + + +admin.site.register(Tip, TipAdmin) diff --git a/totd/apps.py b/totd/apps.py new file mode 100644 index 0000000..9c13575 --- /dev/null +++ b/totd/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class TotdConfig(AppConfig): + name = 'totd' diff --git a/totd/context_processors.py b/totd/context_processors.py new file mode 100644 index 0000000..4dc57cf --- /dev/null +++ b/totd/context_processors.py @@ -0,0 +1,25 @@ +from django.db.models import Q +from .models import Tip + +import datetime + +def tips(request): + """ + Adds a list of tips for the current request + """ + if not request.user.is_authenticated: + return {} + + #import pdb; pdb.set_trace() + + tips = Tip.objects.filter(run_start__lte=datetime.datetime.now()) + tips = tips.filter(Q(run_end__isnull=True) | Q(run_end__gte=datetime.datetime.now())) + tips = tips.filter(Q(view='') | Q(view=request.resolver_match.url_name)).exclude(seen_by=request.user) + if len(tips) > 0: + tips[0].seen_by.add(request.user) + + return { + 'tip': tips[0], + } + + return {} \ No newline at end of file diff --git a/totd/migrations/0001_Initial_models.py b/totd/migrations/0001_Initial_models.py new file mode 100644 index 0000000..43ead9a --- /dev/null +++ b/totd/migrations/0001_Initial_models.py @@ -0,0 +1,32 @@ +# Generated by Django 2.0 on 2018-09-22 05:16 + +from django.conf import settings +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('sites', '0002_alter_domain_unique'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Tip', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=128)), + ('text', models.TextField()), + ('level', models.SmallIntegerField(choices=[(10, 'debug'), (20, 'info'), (25, 'success'), (30, 'warning'), (40, 'error')], default=20)), + ('run_start', models.DateTimeField(default=django.utils.timezone.now)), + ('run_end', models.DateTimeField(blank=True, null=True)), + ('view', models.CharField(blank=True, max_length=256, null=True)), + ('seen_by', models.ManyToManyField(blank=True, related_name='seen_tips', to=settings.AUTH_USER_MODEL)), + ('sites', models.ManyToManyField(to='sites.Site')), + ], + ), + ] diff --git a/totd/migrations/__init__.py b/totd/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/totd/models.py b/totd/models.py new file mode 100644 index 0000000..01ffab9 --- /dev/null +++ b/totd/models.py @@ -0,0 +1,22 @@ +from django.db import models +from django.contrib.sites.models import Site +from django.contrib.messages.constants import DEFAULT_TAGS, INFO +from django.conf import settings +from django.utils import timezone + +# Create your models here. +class Tip(models.Model): + name = models.CharField(max_length=128, null=False, blank=False) + text = models.TextField() + level = models.SmallIntegerField(choices=DEFAULT_TAGS.items(), default=INFO) + + run_start = models.DateTimeField(default=timezone.now) + run_end = models.DateTimeField(null=True, blank=True) + + view = models.CharField(max_length=256, blank=True, null=True) + sites = models.ManyToManyField(Site) + + seen_by = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='seen_tips', blank=True) + + def tags(self): + return settings.MESSAGE_TAGS[self.level] \ No newline at end of file diff --git a/totd/tests.py b/totd/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/totd/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/totd/views.py b/totd/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/totd/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.