def create_app_objects(app): for task in app.tasks: if not ScheduledTask.objects.filter(function=task['function']): # we only consider the function as these tasks should # be unique in the database ScheduledTask.objects.create(**task) logger.info("Created task '%s'" % task['function']) for perm in app.permissions: if not UrlPermission.objects.filter(pattern=perm): newPattern = UrlPermission.objects.create(pattern=perm) directors = get_directors_group() newPattern.groups.add(directors) logger.info("Created UrlPermission r'%s'" % perm) for name, value in app.settings.items(): if not Setting.objects.filter(name=name): Setting.objects.create(name=name, value=repr(value)) logger.info("Created Setting %s=%s" % (repr(name), repr(value))) for share in app.shared_data: url = share['url'] if not url.startswith('/'): url = '/%s/' % app.app_prefix + url handler = share['handler'] if not SharedData.objects.filter(url=url): SharedData.objects.create(url=url, handler=handler) logger.info("Created SharedData %r" % url) else: SharedData.objects.filter(url=url).update(handler=handler)
def update_all_users_accesses(): """ This function will update django users' accesses (that is which groups they are in). It must be executed *AFTER* ecm.apps.hr.tasks.titles.update() which creates all Group objects synchronized with in-game Titles. This function also adds 2 more Groups: "Members" and "Directors" which are not in-game titles but can be usefull for handling accesses to some parts of the application. After the execution, all django users will have been put into the Groups that match their owned characters' in-game titles. @see: ecm.apps.common.models.UrlPermission (django model) @see: ecm.views.decorators.check_user_access (decorator) """ try: t = ScheduledTask.objects.get( function__contains='update_all_character_associations') if not t.is_last_exec_success: raise RuntimeWarning("Last character associations update failed. " "Skipping user access update.") except ScheduledTask.DoesNotExist: pass corp_members_group = get_members_group() directors_group = get_directors_group() my_corp = Corporation.objects.mine() LOG.info("Updating user accesses from their in-game roles...") for user in User.objects.filter(is_active=True): update_user_accesses(user, my_corp, corp_members_group, directors_group) LOG.info("User accesses updated")
def create_app_objects(app): for task in app.tasks: if not ScheduledTask.objects.filter(function=task['function']): # we only consider the function as these tasks should # be unique in the database ScheduledTask.objects.create(**task) logger.info("Created task '%s'" % task['function']) for name, value in app.settings.items(): if not Setting.objects.filter(name=name): Setting.objects.create(name=name, value=repr(value)) logger.info("Created Setting %s=%s" % (repr(name), repr(value))) for perm in app.permissions: if not UrlPermission.objects.filter(pattern=perm): try: directors = get_directors_group() newPattern = UrlPermission.objects.create(pattern=perm) newPattern.groups.add(directors) logger.info("Created UrlPermission r'%s'" % perm) except: # The init order is such that with a fresh DB the director's group is unknown until the HR settings are loaded, which is later in the app list # Ignore and this will get hit later, hopefully. pass for share in app.shared_data: url = share['url'] if not url.startswith('/'): url = '/%s/' % app.app_prefix + url handler = share['handler'] if not SharedData.objects.filter(url=url): SharedData.objects.create(url=url, handler=handler) logger.info("Created SharedData %r" % url) else: SharedData.objects.filter(url=url).update(handler=handler)
def update_all_users_accesses(): """ This function will update django users' accesses (that is which groups they are in). It must be executed *AFTER* ecm.apps.hr.tasks.titles.update() which creates all Group objects synchronized with in-game Titles. This function also adds 2 more Groups: "Members" and "Directors" which are not in-game titles but can be usefull for handling accesses to some parts of the application. After the execution, all django users will have been put into the Groups that match their owned characters' in-game titles. @see: ecm.apps.common.models.UrlPermission (django model) @see: ecm.views.decorators.check_user_access (decorator) """ try: t = ScheduledTask.objects.get(function__contains='update_all_character_associations') if not t.is_last_exec_success: raise RuntimeWarning("Last character associations update failed. " "Skipping user access update.") except ScheduledTask.DoesNotExist: pass corp_members_group = get_members_group() directors_group = get_directors_group() my_corp = Corporation.objects.mine() LOG.info("Updating user accesses from their in-game roles...") for user in User.objects.filter(is_active=True): update_user_accesses(user, my_corp, corp_members_group, directors_group) LOG.info("User accesses updated")
def update_user_accesses(user, my_corp=None, corp_members_group=None, directors_group=None, allies_plus_5_group=None, allies_plus_10_group=None): """ Synchronizes a user's groups with his/hers owned characters' in-game titles. """ my_corp = my_corp or Corporation.objects.mine() corp_members_group = corp_members_group or get_members_group() directors_group = directors_group or get_directors_group() allies_plus_5_group = allies_plus_5_group or get_allies_plus_5_group() allies_plus_10_group = allies_plus_10_group or get_allies_plus_10_group() owned_characters = user.characters.all() titles = Title.objects.none() # we start with an empty QuerySet director = False contact_ids = set() for char in owned_characters: # store all possible contacts to query standings later contact_ids.add(char.characterID) if char.corp: contact_ids.add(char.corp_id) if char.corp.alliance: contact_ids.add(char.corp.alliance.allianceID) director = char.is_director or director titles |= char.titles.all( ) # the "|" operator concatenates django QuerySets all_titles = titles.distinct( ) # to remove duplicates if the same title is assigned to multiple characters user.groups.clear() if owned_characters.filter(corp=my_corp): user.groups.add(corp_members_group) for titleID in all_titles.values_list("titleID", flat=True): user.groups.add(Group.objects.get(id=titleID)) if director: user.groups.add(directors_group) standings = my_corp.standings.filter(contactID__in=contact_ids) # first we test if the contacts of the user do not have negative standings if not standings.filter(value__lte=0): if standings.filter(value__gt=5): # if there are contacts with standings in ]5, 10], we add the user to the +10 allies group user.groups.add(allies_plus_10_group) elif standings.filter(value__gt=0): # if there are contacts with standings in ]0, 5], we add the user to the +5 allies group user.groups.add(allies_plus_5_group)
def update_user_accesses(user, my_corp=None, corp_members_group=None, directors_group=None, allies_plus_5_group=None, allies_plus_10_group=None): """ Synchronizes a user's groups with his/hers owned characters' in-game titles. """ my_corp = my_corp or Corporation.objects.mine() corp_members_group = corp_members_group or get_members_group() directors_group = directors_group or get_directors_group() allies_plus_5_group = allies_plus_5_group or get_allies_plus_5_group() allies_plus_10_group = allies_plus_10_group or get_allies_plus_10_group() owned_characters = user.characters.all() titles = Title.objects.none() # we start with an empty QuerySet director = False contact_ids = set() for char in owned_characters: # store all possible contacts to query standings later contact_ids.add(char.characterID) if char.corp: contact_ids.add(char.corp_id) if char.corp.alliance: contact_ids.add(char.corp.alliance.allianceID) director = char.is_director or director titles |= char.titles.all() # the "|" operator concatenates django QuerySets all_titles = titles.distinct() # to remove duplicates if the same title is assigned to multiple characters user.groups.clear() if owned_characters.filter(corp=my_corp): user.groups.add(corp_members_group) try: for titleID in all_titles.values_list("titleID", flat=True): user.groups.add(Group.objects.get(id=titleID+100)) except: pass # Ignore any missing Groups if director: user.groups.add(directors_group) standings = my_corp.standings.filter(contactID__in=contact_ids) # first we test if the contacts of the user do not have negative standings if not standings.filter(value__lte=0): if standings.filter(value__gt=5): # if there are contacts with standings in ]5, 10], we add the user to the +10 allies group user.groups.add(allies_plus_10_group) elif standings.filter(value__gt=0): # if there are contacts with standings in ]0, 5], we add the user to the +5 allies group user.groups.add(allies_plus_5_group)