[IMP] Update redis links in linkTo/unlinkTo
[cds-indico.git] / indico / util / redis / avatar_links.py
blob9a63e80c173cf28eed540c83a50686da0ebdb10c
1 # -*- coding: utf-8 -*-
2 ##
3 ##
4 ## This file is part of Indico.
5 ## Copyright (C) 2002 - 2013 European Organization for Nuclear Research (CERN).
6 ##
7 ## Indico is free software; you can redistribute it and/or
8 ## modify it under the terms of the GNU General Public License as
9 ## published by the Free Software Foundation; either version 3 of the
10 ## License, or (at your option) any later version.
12 ## Indico is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with Indico;if not, see <http://www.gnu.org/licenses/>.
20 from collections import defaultdict, OrderedDict
21 import MaKaC
22 from indico.util.redis import scripts
25 def add_link(client, avatar, event, role):
26 scripts.avatar_event_links_add_link(avatar.getId(), event.getId(), event.getUnixStartDate(), role, client=client)
29 def del_link(client, avatar, event, role):
30 scripts.avatar_event_links_del_link(avatar.getId(), event.getId(), role, client=client)
33 def get_links(client, avatar):
34 return OrderedDict((eid, set(roles)) for eid, roles in
35 scripts.avatar_event_links_get_links(avatar.getId(), client=client).iteritems())
38 def merge_avatars(client, destination, source):
39 scripts.avatar_event_links_merge_avatars(destination.getId(), source.getId(), client=client)
42 def delete_avatar(client, avatar):
43 scripts.avatar_event_links_delete_avatar(avatar.getId(), client=client)
46 def update_event_time(client, event):
47 scripts.avatar_event_links_update_event_time(event.getId(), event.getUnixStartDate(), client=client)
50 def delete_event(client, event):
51 scripts.avatar_event_links_delete_event(event.getId(), client=client)
54 def init_links(client, avatar):
55 """Initializes the links based on the existing linked_to data."""
57 all_events = set()
58 event_roles = defaultdict(set)
59 for key, roleDict in avatar.linkedTo.iteritems():
60 for role, items in roleDict.iteritems():
61 for item in items:
62 event = event_from_obj(item)
63 if event:
64 all_events.add(event)
65 event_roles[event].add(key + '_' + role)
67 # Add avatar to event avatar lists
68 for event in all_events:
69 client.sadd('avatar-event-links/event_avatars:%s' % event.getId(), avatar.getId())
70 # Add events to avatar event list
71 zdata = dict((e.getId(), e.getUnixStartDate()) for e in all_events)
72 if zdata:
73 client.zadd('avatar-event-links/avatar_events:%s' % avatar.getId(), **zdata)
74 # Add roles to avatar-event role lists
75 for event, roles in event_roles.iteritems():
76 client.sadd('avatar-event-links/avatar_event_roles:%s:%s' % (avatar.getId(), event.getId()), *roles)
79 def event_from_obj(obj):
80 event = None
81 if isinstance(obj, MaKaC.conference.Conference):
82 event = obj
83 elif hasattr(obj, 'getConference'):
84 event = obj.getConference()
85 if event and event.getId() != 'default':
86 return event