This repository was archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_controllers.py
More file actions
194 lines (160 loc) · 7.34 KB
/
_controllers.py
File metadata and controls
194 lines (160 loc) · 7.34 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""PytSite Content Plugin Controllers
"""
__author__ = 'Oleksandr Shepetko'
__email__ = 'a@shepetko.com'
__license__ = 'MIT'
from datetime import datetime
from pytsite import router, metatag, lang, routing, tpl, events
from plugins import auth, odm, taxonomy, hreflang, widget
from plugins.odm_auth import PERM_MODIFY, PERM_DELETE
from . import _model
from ._constants import CONTENT_PERM_VIEW, CONTENT_STATUS_UNPUBLISHED, CONTENT_STATUS_WAITING
class Index(routing.Controller):
"""Content Entities Index
"""
def exec(self):
# Delayed import to prevent circular dependency
from . import _api
# Checking if the model is registered
model = self.arg('model')
if not _api.is_model_registered(model):
raise self.not_found()
# Getting finder
f = _api.find(model)
# Breadcrumb
breadcrumb = widget.select.Breadcrumb('content-index-breadcrumb')
breadcrumb.append_item(lang.t('content@home_page'), router.base_url())
# Filter by term
term_field_name = self.arg('term_field')
term_alias = self.arg('term_alias')
term = None
if term_field_name and f.mock.has_field(term_field_name):
term_field = f.mock.get_field(term_field_name) # type: odm.field.Ref
if term_alias and term_field.model:
for term_model in term_field.model:
term = taxonomy.find(term_model).eq('alias', term_alias).first()
if term:
self.args['term'] = term
if isinstance(f.mock.fields[term_field_name], odm.field.Ref):
f.eq(term_field_name, term)
elif isinstance(f.mock.fields[term_field_name], odm.field.RefsList):
f.inc(term_field_name, term)
metatag.t_set('title', term.title)
breadcrumb.append_item(term.title)
else:
raise self.not_found()
else:
raise self.not_found()
# Filter by author
author_nickname = self.arg('author')
if author_nickname:
try:
author = auth.get_user(nickname=author_nickname)
f.eq('author', author.uid)
self.args['author'] = author
metatag.t_set('title', lang.t('content@articles_of_author', {'name': author.first_last_name}))
if term:
breadcrumb.pop_item()
breadcrumb.append_item(term.title, router.rule_url('content@index', {
'model': model,
'term_field': term_field_name,
'term_alias': term_alias,
}))
breadcrumb.append_item(author.first_last_name)
except auth.error.UserNotFound:
raise self.not_found()
self.args.update({
'finder': f,
'breadcrumb': breadcrumb,
})
try:
# Call a controller provided by application
return router.call('content_index', self.args)
except routing.error.RuleNotFound:
# Render a template provided by application
return tpl.render('content/index', self.args)
class View(routing.Controller):
"""Content Entity View
"""
def exec(self):
from . import _api
model = self.arg('model')
entity = _api.find(model, status='*', check_publish_time=False) \
.eq('_id', self.arg('eid')) \
.first() # type: _model.ContentWithURL
# Check entity existence
if not entity:
raise self.not_found()
# Check permissions
if not entity.odm_auth_check_entity_permissions(CONTENT_PERM_VIEW):
raise self.not_found()
# Show non-published entities only to authors and users who can edit or delete them
c_user = auth.get_current_user()
if (entity.has_field('publish_time') and entity.publish_time > datetime.now()) or \
(entity.has_field('status') and entity.status in (CONTENT_STATUS_UNPUBLISHED, CONTENT_STATUS_WAITING)):
if not (entity.author == c_user or entity.odm_auth_check_entity_permissions([PERM_MODIFY, PERM_DELETE])):
raise self.not_found()
# Show warnings about unpublished entities
if entity.has_field('publish_time') and entity.publish_time > datetime.now():
router.session().add_warning_message(lang.t('content@content_warning_future_publish_time'))
if entity.has_field('status') and entity.status in (CONTENT_STATUS_UNPUBLISHED, CONTENT_STATUS_WAITING):
router.session().add_warning_message(lang.t('content@content_status_warning_{}'.format(entity.status)))
# Breadcrumb
breadcrumb = widget.select.Breadcrumb('content-index-breadcrumb')
breadcrumb.append_item(lang.t('content@home_page'), router.base_url())
entity.content_breadcrumb(breadcrumb)
# Meta title
if entity.has_field('title'):
title = entity.title
metatag.t_set('title', title)
metatag.t_set('og:title', title)
metatag.t_set('twitter:title', title)
# Meta description
if entity.has_field('description'):
description = entity.f_get('description')
metatag.t_set('description', description)
metatag.t_set('og:description', description)
metatag.t_set('twitter:description', description)
# Meta keywords
if entity.has_field('tags'):
metatag.t_set('keywords', entity.f_get('tags', as_string=True))
# Meta image
if entity.has_field('images') and entity.images:
metatag.t_set('twitter:card', 'summary_large_image')
image_w = 900
image_h = 500
image_url = entity.images[0].get_url(width=image_w, height=image_h)
metatag.t_set('og:image', image_url)
metatag.t_set('og:image:width', str(image_w))
metatag.t_set('og:image:height', str(image_h))
metatag.t_set('twitter:image', image_url)
else:
metatag.t_set('twitter:card', 'summary')
# Various metatags
metatag.t_set('og:type', 'article')
metatag.t_set('og:url', entity.url)
metatag.t_set('article:publisher', entity.url)
# 'Author' metatag
if entity.has_field('author') and entity.author:
metatag.t_set('author', entity.author.first_last_name)
metatag.t_set('article:author', entity.author.first_last_name)
# Alternate languages URLs
for lng in lang.langs(False):
f_name = 'localization_' + lng
if entity.has_field(f_name) and entity.f_get(f_name):
hreflang.put(lng, entity.f_get(f_name).url)
else:
hreflang.remove(lng)
# Update args
self.args.update({
'entity': entity,
'breadcrumb': breadcrumb,
})
# Notify listeners
events.fire('content@view', entity=entity)
try:
# Call a controller provided by application
return router.call('content_view', self.args)
except routing.error.RuleNotFound:
# Render a template provided by application
return tpl.render('content/view', self.args)