comparison libervia/pages/events/admin/page_meta.py @ 1216:b2d067339de3

python 3 port: /!\ Python 3.6+ is now needed to use libervia /!\ instability may occur and features may not be working anymore, this will improve with time /!\ TxJSONRPC dependency has been removed The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs for details). Removed now deprecated code (Pyjamas compiled browser part, legacy blog, JSON RPC related code). Adapted code to work without `html` and `themes` dirs.
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:12:31 +0200
parents 0f37b65fe7c2
children acec8e9185db
comparison
equal deleted inserted replaced
1215:f14ab8a25e8b 1216:b2d067339de3
1 #!/usr/bin/env python2.7 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 from libervia.server.constants import Const as C 4 from libervia.server.constants import Const as C
5 from twisted.internet import defer 5 from twisted.internet import defer
6 from twisted.words.protocols.jabber import jid 6 from twisted.words.protocols.jabber import jid
10 import time 10 import time
11 import cgi 11 import cgi
12 import math 12 import math
13 import re 13 import re
14 14
15 name = u"event_admin" 15 name = "event_admin"
16 access = C.PAGES_ACCESS_PROFILE 16 access = C.PAGES_ACCESS_PROFILE
17 template = u"event/admin.html" 17 template = "event/admin.html"
18 log = getLogger(__name__) 18 log = getLogger(__name__)
19 REG_EMAIL_RE = re.compile(C.REG_EMAIL_RE, re.IGNORECASE) 19 REG_EMAIL_RE = re.compile(C.REG_EMAIL_RE, re.IGNORECASE)
20 20
21 21
22 def parse_url(self, request): 22 def parse_url(self, request):
34 data = self.getRData(request) 34 data = self.getRData(request)
35 template_data = request.template_data 35 template_data = request.template_data
36 36
37 ## Event ## 37 ## Event ##
38 38
39 event_service = template_data[u"event_service"] = data["event_service"] 39 event_service = template_data["event_service"] = data["event_service"]
40 event_node = template_data[u"event_node"] = data["event_node"] 40 event_node = template_data["event_node"] = data["event_node"]
41 event_id = template_data[u"event_id"] = data["event_id"] 41 event_id = template_data["event_id"] = data["event_id"]
42 profile = self.getProfile(request) 42 profile = self.getProfile(request)
43 event_timestamp, event_data = yield self.host.bridgeCall( 43 event_timestamp, event_data = yield self.host.bridgeCall(
44 u"eventGet", 44 "eventGet",
45 event_service.userhost() if event_service else "", 45 event_service.userhost() if event_service else "",
46 event_node, 46 event_node,
47 event_id, 47 event_id,
48 profile, 48 profile,
49 ) 49 )
51 background_image = event_data.pop("background-image") 51 background_image = event_data.pop("background-image")
52 except KeyError: 52 except KeyError:
53 pass 53 pass
54 else: 54 else:
55 template_data["dynamic_style"] = safe( 55 template_data["dynamic_style"] = safe(
56 u""" 56 """
57 html { 57 html {
58 background-image: url("%s"); 58 background-image: url("%s");
59 background-size: 15em; 59 background-size: 15em;
60 } 60 }
61 """ 61 """
62 % cgi.escape(background_image, True) 62 % cgi.escape(background_image, True)
63 ) 63 )
64 template_data["event"] = event_data 64 template_data["event"] = event_data
65 invitees = yield self.host.bridgeCall( 65 invitees = yield self.host.bridgeCall(
66 u"eventInviteesList", 66 "eventInviteesList",
67 event_data["invitees_service"], 67 event_data["invitees_service"],
68 event_data["invitees_node"], 68 event_data["invitees_node"],
69 profile, 69 profile,
70 ) 70 )
71 template_data["invitees"] = invitees 71 template_data["invitees"] = invitees
72 invitees_guests = 0 72 invitees_guests = 0
73 for invitee_data in invitees.itervalues(): 73 for invitee_data in invitees.values():
74 if invitee_data.get("attend", "no") == "no": 74 if invitee_data.get("attend", "no") == "no":
75 continue 75 continue
76 try: 76 try:
77 invitees_guests += int(invitee_data.get("guests", 0)) 77 invitees_guests += int(invitee_data.get("guests", 0))
78 except ValueError: 78 except ValueError:
79 log.warning( 79 log.warning(
80 _(u"guests value is not valid: {invitee}").format(invitee=invitee_data) 80 _("guests value is not valid: {invitee}").format(invitee=invitee_data)
81 ) 81 )
82 template_data["invitees_guests"] = invitees_guests 82 template_data["invitees_guests"] = invitees_guests
83 template_data["days_left"] = int( 83 template_data["days_left"] = int(
84 math.ceil((event_timestamp - time.time()) / (60 * 60 * 24)) 84 math.ceil((event_timestamp - time.time()) / (60 * 60 * 24))
85 ) 85 )
86 86
87 ## Blog ## 87 ## Blog ##
88 88
89 data[u"service"] = jid.JID(event_data[u"blog_service"]) 89 data["service"] = jid.JID(event_data["blog_service"])
90 data[u"node"] = event_data[u"blog_node"] 90 data["node"] = event_data["blog_node"]
91 data[u"allow_commenting"] = u"simple" 91 data["allow_commenting"] = "simple"
92 92
93 # we now need blog items, using blog common page 93 # we now need blog items, using blog common page
94 # this will fill the "items" template data 94 # this will fill the "items" template data
95 blog_page = self.getPageByName(u"blog_view") 95 blog_page = self.getPageByName("blog_view")
96 yield blog_page.prepare_render(self, request) 96 yield blog_page.prepare_render(self, request)
97 97
98 98
99 @defer.inlineCallbacks 99 @defer.inlineCallbacks
100 def on_data_post(self, request): 100 def on_data_post(self, request):
101 profile = self.getProfile(request) 101 profile = self.getProfile(request)
102 if not profile: 102 if not profile:
103 log.error(u"got post data without profile") 103 log.error("got post data without profile")
104 self.pageError(request, C.HTTP_INTERNAL_ERROR) 104 self.pageError(request, C.HTTP_INTERNAL_ERROR)
105 type_ = self.getPostedData(request, "type") 105 type_ = self.getPostedData(request, "type")
106 if type_ == "blog": 106 if type_ == "blog":
107 service, node, title, body, lang = self.getPostedData( 107 service, node, title, body, lang = self.getPostedData(
108 request, (u"service", u"node", u"title", u"body", u"language") 108 request, ("service", "node", "title", "body", "language")
109 ) 109 )
110 110
111 if not body.strip(): 111 if not body.strip():
112 self.pageError(request, C.HTTP_BAD_REQUEST) 112 self.pageError(request, C.HTTP_BAD_REQUEST)
113 data = {u"content": body} 113 data = {"content": body}
114 if title: 114 if title:
115 data[u"title"] = title 115 data["title"] = title
116 if lang: 116 if lang:
117 data[u"language"] = lang 117 data["language"] = lang
118 try: 118 try:
119 comments = bool(self.getPostedData(request, u"comments").strip()) 119 comments = bool(self.getPostedData(request, "comments").strip())
120 except KeyError: 120 except KeyError:
121 pass 121 pass
122 else: 122 else:
123 if comments: 123 if comments:
124 data[u"allow_comments"] = C.BOOL_TRUE 124 data["allow_comments"] = C.BOOL_TRUE
125 125
126 try: 126 try:
127 yield self.host.bridgeCall(u"mbSend", service, node, data, profile) 127 yield self.host.bridgeCall("mbSend", service, node, data, profile)
128 except Exception as e: 128 except Exception as e:
129 if u"forbidden" in unicode(e): 129 if "forbidden" in str(e):
130 self.pageError(request, C.HTTP_FORBIDDEN) 130 self.pageError(request, C.HTTP_FORBIDDEN)
131 else: 131 else:
132 raise e 132 raise e
133 elif type_ == "event": 133 elif type_ == "event":
134 service, node, event_id, jids, emails = self.getPostedData( 134 service, node, event_id, jids, emails = self.getPostedData(
135 request, (u"service", u"node", u"event_id", u"jids", u"emails") 135 request, ("service", "node", "event_id", "jids", "emails")
136 ) 136 )
137 for invitee_jid_s in jids.split(): 137 for invitee_jid_s in jids.split():
138 try: 138 try:
139 invitee_jid = jid.JID(invitee_jid_s) 139 invitee_jid = jid.JID(invitee_jid_s)
140 except RuntimeError as e: 140 except RuntimeError as e:
141 log.warning( 141 log.warning(
142 _(u"this is not a valid jid: {jid}").format(jid=invitee_jid_s) 142 _("this is not a valid jid: {jid}").format(jid=invitee_jid_s)
143 ) 143 )
144 continue 144 continue
145 yield self.host.bridgeCall( 145 yield self.host.bridgeCall(
146 "eventInvite", invitee_jid.userhost(), service, node, event_id, profile 146 "eventInvite", invitee_jid.userhost(), service, node, event_id, profile
147 ) 147 )
148 for email_addr in emails.split(): 148 for email_addr in emails.split():
149 if not REG_EMAIL_RE.match(email_addr): 149 if not REG_EMAIL_RE.match(email_addr):
150 log.warning( 150 log.warning(
151 _(u"this is not a valid email address: {email}").format( 151 _("this is not a valid email address: {email}").format(
152 email=email_addr 152 email=email_addr
153 ) 153 )
154 ) 154 )
155 continue 155 continue
156 yield self.host.bridgeCall( 156 yield self.host.bridgeCall(
158 service, 158 service,
159 node, 159 node,
160 event_id, 160 event_id,
161 email_addr, 161 email_addr,
162 {}, 162 {},
163 u"", 163 "",
164 u"", 164 "",
165 u"", 165 "",
166 u"", 166 "",
167 u"", 167 "",
168 u"", 168 "",
169 profile, 169 profile,
170 ) 170 )
171 171
172 else: 172 else:
173 log.warning(_(u"Unhandled data type: {}").format(type_)) 173 log.warning(_("Unhandled data type: {}").format(type_))