Mercurial > libervia-web
comparison src/pages/events/admin/page_meta.py @ 1111:4d1c4bd4931a
pages (events): added root, admin, new, rsvp and view pages
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 21 Jun 2018 01:19:57 +0200 |
parents | |
children | cdd389ef97bc |
comparison
equal
deleted
inserted
replaced
1110:045e8bdaed4f | 1111:4d1c4bd4931a |
---|---|
1 #!/usr/bin/env python2.7 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 from libervia.server.constants import Const as C | |
5 from twisted.internet import defer | |
6 from twisted.words.protocols.jabber import jid | |
7 from sat.tools.common.template import safe | |
8 from sat.core.i18n import _ | |
9 from sat.core.log import getLogger | |
10 import time | |
11 import cgi | |
12 import math | |
13 import re | |
14 | |
15 name = u'event_admin' | |
16 access = C.PAGES_ACCESS_PROFILE | |
17 template = u"event/admin.html" | |
18 log = getLogger(u'pages/' + name) | |
19 REG_EMAIL_RE = re.compile(C.REG_EMAIL_RE, re.IGNORECASE) | |
20 | |
21 | |
22 def parse_url(self, request): | |
23 self.getPathArgs(request, ("event_service", "event_node", "event_id"), min_args=2, event_service="@jid", event_id='') | |
24 | |
25 @defer.inlineCallbacks | |
26 def prepare_render(self, request): | |
27 data = self.getRData(request) | |
28 template_data = request.template_data | |
29 | |
30 ## Event ## | |
31 | |
32 event_service = template_data[u'event_service'] = data['event_service'] | |
33 event_node = template_data[u'event_node'] = data['event_node'] | |
34 event_id = template_data[u'event_id'] = data['event_id'] | |
35 profile = self.getProfile(request) | |
36 event_timestamp, event_data = yield self.host.bridgeCall(u"eventGet", event_service.userhost() if event_service else '', event_node, event_id, profile) | |
37 try: | |
38 background_image = event_data.pop('background-image') | |
39 except KeyError: | |
40 pass | |
41 else: | |
42 template_data['dynamic_style'] = safe(u""" | |
43 html { | |
44 background-image: url("%s"); | |
45 background-size: 15em; | |
46 } | |
47 """ % cgi.escape(background_image, True)) | |
48 template_data['event'] = event_data | |
49 invitees = yield self.host.bridgeCall( | |
50 u"eventInviteesList", | |
51 event_data['invitees_service'], | |
52 event_data['invitees_node'], | |
53 profile) | |
54 template_data['invitees'] = invitees | |
55 invitees_guests = 0 | |
56 for invitee_data in invitees.itervalues(): | |
57 if invitee_data.get('attend', 'no') == 'no': | |
58 continue | |
59 try: | |
60 invitees_guests += int(invitee_data.get('guests', 0)) | |
61 except ValueError: | |
62 log.warning(_(u"guests value is not valid: {invitee}").format(invitee=invitee_data)) | |
63 template_data['invitees_guests'] = invitees_guests | |
64 template_data['days_left'] = int(math.ceil((event_timestamp - time.time()) / (60 * 60 * 24))) | |
65 | |
66 ## Blog ## | |
67 | |
68 data[u'service'] = jid.JID(event_data[u'blog_service']) | |
69 data[u'node'] = event_data[u'blog_node'] | |
70 data[u'allow_commenting'] = u'simple' | |
71 | |
72 # we now need blog items, using blog common page | |
73 # this will fill the "items" template data | |
74 blog_page = self.getPageByName(u'blog_view') | |
75 yield blog_page.prepare_render(self, request) | |
76 | |
77 @defer.inlineCallbacks | |
78 def on_data_post(self, request): | |
79 profile = self.getProfile(request) | |
80 if not profile: | |
81 log.error(u"got post data without profile") | |
82 self.pageError(request, C.HTTP_INTERNAL_ERROR) | |
83 type_ = self.getPostedData(request, 'type') | |
84 if type_ == 'blog': | |
85 service, node, title, body, lang = self.getPostedData( | |
86 request, (u'service', u'node', u'title', u'body', u'language')) | |
87 | |
88 if not body.strip(): | |
89 self.pageError(request, C.HTTP_BAD_REQUEST) | |
90 data = {u"content": body} | |
91 if title: | |
92 data[u'title'] = title | |
93 if lang: | |
94 data[u'language'] = lang | |
95 try: | |
96 comments = bool(self.getPostedData(request, u'comments').strip()) | |
97 except KeyError: | |
98 pass | |
99 else: | |
100 if comments: | |
101 data[u'allow_comments'] = C.BOOL_TRUE | |
102 | |
103 try: | |
104 yield self.host.bridgeCall(u'mbSend', service, node, data, profile) | |
105 except Exception as e: | |
106 if u"forbidden" in unicode(e): | |
107 self.pageError(request, C.HTTP_UNAUTHORIZED) | |
108 else: | |
109 raise e | |
110 elif type_ == 'event': | |
111 service, node, event_id, jids, emails = self.getPostedData(request, (u'service', | |
112 u'node', | |
113 u'event_id', | |
114 u'jids', | |
115 u'emails')) | |
116 for invitee_jid_s in jids.split(): | |
117 try: | |
118 invitee_jid = jid.JID(invitee_jid_s) | |
119 except RuntimeError as e: | |
120 log.warning(_(u"this is not a valid jid: {jid}").format(jid=invitee_jid_s)) | |
121 continue | |
122 yield self.host.bridgeCall('eventInvite', invitee_jid.userhost(), | |
123 service, | |
124 node, | |
125 event_id, | |
126 profile) | |
127 for email_addr in emails.split(): | |
128 if not REG_EMAIL_RE.match(email_addr): | |
129 log.warning(_(u"this is not a valid email address: {email}").format(email=email_addr)) | |
130 continue | |
131 yield self.host.bridgeCall('eventInviteByEmail', | |
132 service, | |
133 node, | |
134 event_id, | |
135 email_addr, | |
136 {}, | |
137 u'', | |
138 u'', | |
139 u'', | |
140 u'', | |
141 u'', | |
142 u'', | |
143 profile) | |
144 | |
145 | |
146 else: | |
147 log.warning(_(u"Unhandled data type: {}").format(type_)) |