~fabrixxm/confy

ref: 3711232f069768031fd703392d6fc7635fa708da confy/src/models.py -rw-r--r-- 12.5 KiB
3711232ffabrixxm Fix typo 4 months ago
                                                                                
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# models.py
#
# Copyright 2020 Fabio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import datetime
import time

from gi.repository import GObject

from . import local
from .fetcher import Fetcher

## models
def _build_filter(day=None, room=None, track=None, event=None, **kwargs):
        filterdata = []
        wherestm = []

        def __f(col, value):
            if value is None:
                wherestm.append("{} is null".format(col))
            else:
                wherestm.append("{} = ?".format(col))
                filterdata.append(value)

        if day is not None:
            __f("date", day.date)
        if room is not None:
            __f("room", room.name)
        if track is not None:
            __f("track", track.name)
        if event is not None:
            __f("id", event.id)

        for k,v in kwargs.items():
            __f(k, v)

        return (filterdata, wherestm)


class Conference:
    """A conference in menu files (ggmenu.json and usermenu.json)"""
    def _init(self, obj):
        self.metadata = {}
        self.user = False
        for k, v in obj.items():
            setattr(self, k, v)

        self.start = datetime.date(*[int(s) for s in self.start.split("-")])
        self.end = datetime.date(*[int(s) for s in self.end.split("-")])

        return self

    def get_map_links(self):
        maps = [ l for l in self.metadata.get('links',[]) if l['title']=="Map" ]
        print("Conference maps links:", maps)
        return maps

    def get_map_files(self, cbk):
        r = []
        for link in self.get_map_links():
            r.append(local.get_image_async(link['url'], cbk))
        return r

    def get_logo_file(self, cbk):
        logourl = self.metadata.get('icon', None)
        return local.get_image_async(logourl, cbk)

    def __repr__(self):
        return f"<{self.__class__.__name__} {self.__dict__}>"

    @classmethod
    def all(cls):
        # from giggity menu
        return [ cls()._init(s) for s in local.get_menu() ]

    @classmethod
    def by_url(cls, url):
        ss = [ s for s in local.get_menu() if url in s['url'] ]
        if len(ss) == 0:
            return None
        return cls()._init(ss[0])

    def get_meta(self):
        """Return metadata from db. This will close any opened db connection."""
        local.close()
        local.opendb(self.url, False)
        return Meta()
        local.close()

    def to_json(self):
        """Export conference meta as json for user menu"""
        d = self.__dict__.copy()
        for k in d:
            if isinstance(d[k], (datetime.date, datetime.datetime)):
                d[k] = d[k].strftime("%Y-%m-%d")
        return d


class Meta:
    """Conference metadata from "meta" table in db"""
    def __init__(self):
        _c = local.getDb().execute("SELECT key, value FROM meta")
        _d = [(row['key'], row['value']) for row in _c]
        self._data = dict(_d)

    def _fmtd_value(self, k):
        v = self._data.get(k, None)
        if k == "start" or k == "end":
            if v is not None and v != "":
                if ":" in v:
                    v = datetime.datetime.strptime(v, "%Y-%m-%d %H:%M:%S")
                else:
                    v = datetime.datetime.strptime(v, "%Y-%m-%d")
            else:
                v = None
        if k == "last_update":
            v = 0 if v is None else float(v)
        return v

    def __getattr__(self, name):
        if name.startswith("_"):
            return super().__getattr__(name)
        return self._fmtd_value(name)

    def __setattr__(self, name, value):
        if name.startswith("_"):
            return super().__setattr__(name, value)
        self._data[name] = value

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.save()

    def __repr__(self):
        return f"<{self.__class__.__name__} {self.__dict__}>"

    def save(self):
        db = local.getDb()
        if db is None:
            local.opendb(self.url, False)
            db = local.getDb()
        db.executemany("INSERT OR REPLACE INTO meta (key, value) VALUES (?,?)", self._data.items())
        db.commit()

    def to_json(self):
        """Export conference meta as json for user menu"""
        return  {
            "url": self.url,
            "title": self.title if self.title else "",
            "start": self.start.strftime("%Y-%m-%d"),
            "end": self.end.strftime("%Y-%m-%d"),
            "user": True
        }


class Day:
    def _init(self, data):
        self.date = data['date']
        return self

    def __repr__(self):
        return "<{} {!r}>".format(self.__class__.__name__, self.date)

    def __str__(self):
        return self.date.strftime("%x")

    @classmethod
    def all(cls):
        for row in local.getDb().execute("SELECT date FROM events GROUP BY date"):
            yield cls()._init(row)

    @classmethod
    def count(cls):
        for row in local.getDb().execute("SELECT count(DISTINCT date) FROM events"):
            return row[0]

    @classmethod
    def filter(cls, room=None, track=None, event=None):
        filterdata, wherestm = _build_filter(room=room, track=track, event=event)

        query = """SELECT date FROM events
                    WHERE {}
                    GROUP BY date""".format(" AND ".join(wherestm))

        for row in local.getDb().execute(query, filterdata):
            yield cls()._init(row)



class Room:
    def _init(self, data):
        self.name = data['room']
        return self

    def __repr__(self):
        return "<{} {!r}>".format(self.__class__.__name__, self.name)

    def __str__(self):
        return self.name or "- no room -"

    @classmethod
    def all(cls):
        for row in local.getDb().execute("SELECT room FROM events GROUP BY room"):
            yield cls()._init(row)

    @classmethod
    def count(cls):
        for row in local.getDb().execute("SELECT count(DISTINCT room) FROM events"):
            return row[0]

    @classmethod
    def filter(cls, day=None, track=None, event=None):
        filterdata, wherestm = _build_filter(day=day, track=track, event=event)

        query = """SELECT room FROM events
                    WHERE {}
                    GROUP BY room""".format(" AND ".join(wherestm))

        for row in local.getDb().execute(query, filterdata):
            yield cls()._init(row)


class Track:
    def _init(self, data):
        self.name = data['track']
        self.room = list(Room.filter(track=self))
        self.date = list(Day.filter(track=self))
        return self

    def __repr__(self):
        return "<{} {!r} in {!r} @ {}>".format(
            self.__class__.__name__, self.name, self.room, self.date)

    def __str__(self):
        return self.name or "- no track -"

    @classmethod
    def all(cls):
        for row in local.getDb().execute(
                "SELECT track, room, date FROM events WHERE track != '' GROUP BY track ORDER BY date, track"):
            yield cls()._init(row)

    @classmethod
    def count(cls):
        for row in local.getDb().execute("SELECT count(DISTINCT track) FROM events"):
            return row[0]



class Person:
    def _init(self, data):
        self.id = data['id']
        self.name = data['name']
        return self

    def events(self):
        for row in local.getDb().execute(
                """SELECT e.* FROM events as e
                    LEFT JOIN event_person AS ep ON e.id = ep.event_id
                    WHERE ep.person_id = ?""", (self.id,)):
            yield Event()._init(row)

    def __repr__(self):
        return "<{} {!r}>".format(self.__class__.__name__, self.name)

    def __str__(self):
        return self.name

    @classmethod
    def all(cls):
        for row in local.getDb().execute(
                "SELECT id, name FROM persons"):
            yield cls()._init(row)


class Link:
    def _init(self, event, data):
        self.event = event
        self.href = data['href']
        self.name = data['name']
        return self

    def __repr__(self):
        return "<{} for {!r} {!r} {!r}>".format(
            self.__class__.__name__, self.event, self.name, self.href)

    def __str__(self):
        return '<a href="{}">{}</a>'.format(self.href, self.name)


class Event(GObject.GObject):
    __gsignals__ = {
        'update': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ())
    }

    def _init(self, data):
        self.id = data['id']
        self.date = data['date']
        self.start = data['start']
        self.end = data['end']
        self.room = data['room']
        self.slug = data['slug']
        self.title = data['title']
        self.subtitle = data['subtitle'] or ""
        self.track = data['track']
        self.evtype = data['type']
        self.abstract = data['abstract'] or ""
        self.description = data['description'] or ""
        self.starred = data['starred'] == 1
        self.notified = data['notified'] == 1
        return self

    def set_star(self, starred):
        local.getDb().execute("UPDATE events SET starred=? WHERE id=?",(starred,self.id,))
        local.getDb().commit()
        self.starred = starred
        self.emit('update')

    def set_notified(self, notified):
        local.getDb().execute("UPDATE events SET notified=? WHERE id=?",(notified,self.id,))
        local.getDb().commit()
        self.notified = notified
        self.emit('update')

    def persons(self):
        #print("event", self.id, "persons()")
        for row in local.getDb().execute(
                """SELECT p.id, p.name FROM persons as p
                    LEFT JOIN event_person AS ep ON p.id = ep.person_id
                    WHERE ep.event_id = ?""", (self.id,)):
            yield Person()._init(row)

    def links(self):
        for row in local.getDb().execute(
            """SELECT href, name FROM links
                WHERE event_id=?""", (self.id,)):
            yield Link()._init(self, row)

    def __repr__(self):
        return "<{} {!r} in {!r} @ {!r}>".format(self.__class__.__name__, self.title, self.room, self.start)

    def __str__(self):
        return self.title

    def get_conflicts(self):
        query = """SELECT * FROM events
                    WHERE starred = 1 AND id != ? AND date = ?
                    AND end > ? AND start < ?
                """
        for row in local.getDb().execute(query, (self.id, self.date, self.start, self.end)):
            yield Event()._init(row)

    @classmethod
    def all(cls):
        for row in local.getDb().execute("SELECT * FROM events"):
            yield cls()._init(row)

    @classmethod
    def filter(cls, day:Day = None, room:Room = None, track:Track = None, **kwargs):
        filterdata, wherestm = _build_filter(day=day, room=room, track=track, **kwargs)

        query = """SELECT * FROM events
                    WHERE {}
                    ORDER BY start, room""".format(" AND ".join(wherestm))
        for row in local.getDb().execute(query, filterdata):
            yield cls()._init(row)

    @classmethod
    def nextup(cls, minutes):
        """get events starting in next `minutes`"""
        minutes = min(int(minutes), 30)  # some sanity check
        now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000")
        # now = "now" # is "now" somehow broken?
        query = """SELECT * FROM events
                    WHERE starred=1 AND notified=0
                        AND datetime(start) >= datetime("{1}")
                        AND datetime(start) <= datetime("{1}", "+{0} minutes");"""
        for row in local.getDb().execute(query.format(minutes, now)):
            yield cls()._init(row)

    @classmethod
    def search(cls, term, starred=False):
        """Search events for 'term'"""

        starfilter = ""
        if starred:
            starfilter = "events.starred = 1 AND"

        query = """SELECT events.* FROM fts_event
                    JOIN events ON events.id = fts_event.event_id
                    WHERE {} fts_event = ?
                    GROUP BY events.id
                    ORDER BY rank""".format(starfilter)

        for row in local.getDb().execute(query, (term,)):
            yield cls()._init(row)