-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathviews.py
53 lines (42 loc) · 1.45 KB
/
views.py
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
from cbv_utils.mixins import context_mixin_factory
from datetime import datetime
from django.views.generic import DetailView
from hero_content.models import Hero
from posts.models import Post
from django.conf import settings
def posts():
return Post.objects.all()[:5]
PostsMixin = context_mixin_factory(callback=posts)
CompiledStaticMixin = context_mixin_factory({
"USE_COMPILED_STATIC": settings.USE_COMPILED_STATIC,
})
SettingsContextMixin = context_mixin_factory({
'SITE_NAME': settings.SITE_NAME,
})
class IndexView(PostsMixin,
CompiledStaticMixin,
SettingsContextMixin,
DetailView):
model = Hero
queryset = Hero.objects.all()
template_name = "index.html"
def get_queryset(self, queryset=None):
if queryset is None:
queryset = self.queryset
return queryset.filter(pub_date__lte=datetime.now)
def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()
try:
return queryset.all()[0]
except IndexError:
return _fake_hero()
def _fake_hero():
""" Generate a stub hero for first-time peeps. """
return Hero(title="No Hero Content!",
summary="Add a hero to the database to set it up.",
pub_date=datetime.today(),
action_text="Action Link Here",
action_url="#action-link",
location="Location Here",
datetime=datetime.now())