Added newsletters
This commit is contained in:
parent
e2a2a320d9
commit
258b38cd96
@ -31,15 +31,16 @@ ALLOWED_HOSTS = ['*']
|
|||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'charter_members.apps.CharterMembersConfig',
|
|
||||||
'main.apps.MainConfig',
|
|
||||||
'bootstrap_admin',
|
'bootstrap_admin',
|
||||||
|
'charter_members.apps.CharterMembersConfig',
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'main.apps.MainConfig',
|
||||||
|
'newsletter.apps.NewsletterConfig'
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
0
newsletter/__init__.py
Normal file
0
newsletter/__init__.py
Normal file
46
newsletter/admin.py
Normal file
46
newsletter/admin.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
from django.contrib.auth.admin import User
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from .models import Newsletter, Subscriber
|
||||||
|
|
||||||
|
|
||||||
|
class SubscriberInline(admin.StackedInline):
|
||||||
|
can_delete = False
|
||||||
|
fields = ['subscribed']
|
||||||
|
max_num = 1
|
||||||
|
model = Subscriber
|
||||||
|
verbose_name = 'Newsletter'
|
||||||
|
|
||||||
|
|
||||||
|
class ExtendUserAdmin(admin.ModelAdmin):
|
||||||
|
inlines = [SubscriberInline]
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.unregister(User)
|
||||||
|
admin.site.register(User, ExtendUserAdmin)
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(Newsletter)
|
||||||
|
class NewsletterAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ['subject', 'created', 'publish']
|
||||||
|
search_fields = ['subject', 'created', 'publish']
|
||||||
|
|
||||||
|
def get_form(self, request, obj=None, **kwargs):
|
||||||
|
if obj:
|
||||||
|
self.fields = ['creator', 'subject', 'body', 'publish']
|
||||||
|
if obj.publish < timezone.now():
|
||||||
|
self.readonly_fields = ['creator', 'subject', 'body', 'publish']
|
||||||
|
else:
|
||||||
|
self.readonly_fields = ['creator']
|
||||||
|
else:
|
||||||
|
self.fields = ['subject', 'body', 'publish']
|
||||||
|
self.readonly_fields = []
|
||||||
|
return super(NewsletterAdmin, self).get_form(request, obj, **kwargs)
|
||||||
|
|
||||||
|
def save_model(self, request, instance, form, change):
|
||||||
|
user = request.user
|
||||||
|
instance = form.save(commit=False)
|
||||||
|
instance.creator = user
|
||||||
|
instance.save()
|
||||||
|
return instance
|
5
newsletter/apps.py
Normal file
5
newsletter/apps.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class NewsletterConfig(AppConfig):
|
||||||
|
name = 'newsletter'
|
22
newsletter/models.py
Normal file
22
newsletter/models.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from django.db import models
|
||||||
|
from django.contrib.auth.admin import User
|
||||||
|
from django.utils.timezone import now
|
||||||
|
|
||||||
|
|
||||||
|
class Newsletter(models.Model):
|
||||||
|
body = models.TextField()
|
||||||
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
|
creator = models.ForeignKey(User)
|
||||||
|
publish = models.DateTimeField(default=now())
|
||||||
|
subject = models.CharField(max_length=255)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.subject
|
||||||
|
|
||||||
|
|
||||||
|
class Subscriber(models.Model):
|
||||||
|
user = models.OneToOneField(User)
|
||||||
|
subscribed = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return ''
|
3
newsletter/tests.py
Normal file
3
newsletter/tests.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
3
newsletter/views.py
Normal file
3
newsletter/views.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
Loading…
Reference in New Issue
Block a user