comparison libervia/web/pages/events/admin/page_meta.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents libervia/pages/events/admin/page_meta.py@106bae41f5c8
children
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3
4 from libervia.web.server.constants import Const as C
5 from twisted.words.protocols.jabber import jid
6 from libervia.backend.tools.common.template import safe
7 from libervia.backend.tools.common import data_format
8 from libervia.backend.core.i18n import _, D_
9 from libervia.backend.core.log import getLogger
10 import time
11 import html
12 import math
13 import re
14
15 name = "event_admin"
16 label = D_("Event Administration")
17 access = C.PAGES_ACCESS_PROFILE
18 template = "event/admin.html"
19 log = getLogger(__name__)
20 REG_EMAIL_RE = re.compile(C.REG_EMAIL_RE, re.IGNORECASE)
21
22
23 def parse_url(self, request):
24 self.get_path_args(
25 request,
26 ("event_service", "event_node", "event_id"),
27 min_args=2,
28 event_service="@jid",
29 event_id="",
30 )
31
32
33 async def prepare_render(self, request):
34 data = self.get_r_data(request)
35 template_data = request.template_data
36
37 ## Event ##
38
39 event_service = template_data["event_service"] = data["event_service"]
40 event_node = template_data["event_node"] = data["event_node"]
41 event_id = template_data["event_id"] = data["event_id"]
42 profile = self.get_profile(request)
43 event_timestamp, event_data = await self.host.bridge_call(
44 "eventGet",
45 event_service.userhost() if event_service else "",
46 event_node,
47 event_id,
48 profile,
49 )
50 try:
51 background_image = event_data.pop("background-image")
52 except KeyError:
53 pass
54 else:
55 template_data["dynamic_style"] = safe(
56 """
57 html {
58 background-image: url("%s");
59 background-size: 15em;
60 }
61 """
62 % html.escape(background_image, True)
63 )
64 template_data["event"] = event_data
65 invitees = await self.host.bridge_call(
66 "event_invitees_list",
67 event_data["invitees_service"],
68 event_data["invitees_node"],
69 profile,
70 )
71 template_data["invitees"] = invitees
72 invitees_guests = 0
73 for invitee_data in invitees.values():
74 if invitee_data.get("attend", "no") == "no":
75 continue
76 try:
77 invitees_guests += int(invitee_data.get("guests", 0))
78 except ValueError:
79 log.warning(
80 _("guests value is not valid: {invitee}").format(invitee=invitee_data)
81 )
82 template_data["invitees_guests"] = invitees_guests
83 template_data["days_left"] = int(
84 math.ceil((event_timestamp - time.time()) / (60 * 60 * 24))
85 )
86
87 ## Blog ##
88
89 data["service"] = jid.JID(event_data["blog_service"])
90 data["node"] = event_data["blog_node"]
91 data["allow_commenting"] = "simple"
92
93 # we now need blog items, using blog common page
94 # this will fill the "items" template data
95 blog_page = self.get_page_by_name("blog_view")
96 await blog_page.prepare_render(self, request)
97
98
99 async def on_data_post(self, request):
100 profile = self.get_profile(request)
101 if not profile:
102 log.error("got post data without profile")
103 self.page_error(request, C.HTTP_INTERNAL_ERROR)
104 type_ = self.get_posted_data(request, "type")
105 if type_ == "blog":
106 service, node, title, body, lang = self.get_posted_data(
107 request, ("service", "node", "title", "body", "language")
108 )
109
110 if not body.strip():
111 self.page_error(request, C.HTTP_BAD_REQUEST)
112 data = {"content": body}
113 if title:
114 data["title"] = title
115 if lang:
116 data["language"] = lang
117 try:
118 comments = bool(self.get_posted_data(request, "comments").strip())
119 except KeyError:
120 pass
121 else:
122 if comments:
123 data["allow_comments"] = True
124
125 try:
126 await self.host.bridge_call(
127 "mb_send", service, node, data_format.serialise(data), profile)
128 except Exception as e:
129 if "forbidden" in str(e):
130 self.page_error(request, C.HTTP_FORBIDDEN)
131 else:
132 raise e
133 elif type_ == "event":
134 service, node, event_id, jids, emails = self.get_posted_data(
135 request, ("service", "node", "event_id", "jids", "emails")
136 )
137 for invitee_jid_s in jids.split():
138 try:
139 invitee_jid = jid.JID(invitee_jid_s)
140 except RuntimeError:
141 log.warning(
142 _("this is not a valid jid: {jid}").format(jid=invitee_jid_s)
143 )
144 continue
145 await self.host.bridge_call(
146 "event_invite", invitee_jid.userhost(), service, node, event_id, profile
147 )
148 for email_addr in emails.split():
149 if not REG_EMAIL_RE.match(email_addr):
150 log.warning(
151 _("this is not a valid email address: {email}").format(
152 email=email_addr
153 )
154 )
155 continue
156 await self.host.bridge_call(
157 "event_invite_by_email",
158 service,
159 node,
160 event_id,
161 email_addr,
162 {},
163 "",
164 "",
165 "",
166 "",
167 "",
168 "",
169 profile,
170 )
171
172 else:
173 log.warning(_("Unhandled data type: {}").format(type_))