Changeset 59a438a


Ignore:
Timestamp:
Apr 9, 2012, 1:27:28 PM (14 years ago)
Author:
Alex Dehnert <adehnert@…>
Branches:
master, space-access, stable, stage
Children:
0c0dbf4
Parents:
c3cd6c2
git-author:
Alex Dehnert <adehnert@…> (04/09/12 13:22:20)
git-committer:
Alex Dehnert <adehnert@…> (04/09/12 13:27:28)
Message:

Enhance readability of manage_officers view

Try to break it up a bit more so it's a *little* bit easier to read. Also,
hopefully, it'll be a bit easier to extend when fixing ASA Trac #90 et al.

Fixes ASA Trac #87.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asadb/groups/views.py

    rc3cd6c2 r59a438a  
    210210    return render_to_response('groups/group_change_main.html', context, context_instance=RequestContext(request), )
    211211
    212 def load_officers(group, ):
     212# Helper for manage_officers view
     213def manage_officers_load_officers(group, ):
    213214    officers = group.officers()
    214215    people = list(set([ officer.person for officer in officers ]))
     
    225226    return people, roles, name_map, officers_map
    226227
     228# Helper for manager_officers view
     229def manage_officers_load_accounts(max_new, people, request, msgs, ):
     230    new_people = {}
     231    moira_accounts = {}
     232
     233    for i in range(max_new):
     234        key = "extra.%d" % (i, )
     235        if key in request.POST and request.POST[key] != "":
     236            username = request.POST[key]
     237            try:
     238                moira_accounts[username] = groups.models.AthenaMoiraAccount.active_accounts.get(username=username)
     239                new_people[i] = username
     240            except groups.models.AthenaMoiraAccount.DoesNotExist:
     241                msgs.append('Athena account "%s" appears not to exist. Changes involving them have been ignored.' % (username, ))
     242    for person in people:
     243        try:
     244            moira_accounts[person] = groups.models.AthenaMoiraAccount.active_accounts.get(username=person)
     245        except groups.models.AthenaMoiraAccount.DoesNotExist:
     246            msgs.append('Athena account "%s" appears not to exist. They can not be added to new roles. You should remove them from any roles they hold, if you have not already.' % (person, ))
     247
     248    return new_people, moira_accounts
     249
     250# Helper for manager_officers view
     251def manage_officers_sync_role_people(
     252    group, role, new_holders,
     253    msgs, changes,
     254    officers_map, people, moira_accounts, new_people, max_new, ):
     255    """
     256    Sync a set of new holders of a role with the database.
     257
     258    Arguments:
     259    Function-specific:
     260        role: the role object the changes center around
     261        new_holders: The desired final set of people who should have the role
     262    Output arguments --- information messages
     263        msgs: warning message list. Output argument.
     264        changes: list of changes made. [(verb, color, person, role)]
     265    Background info arguments:
     266        officers_map: (username, role) -> OfficeHolder
     267        people: list of all potentially-affected people (who were previously involved)
     268        moira_accounts: username -> AthenaMoiraAccount
     269        new_people: potentially-affected people (who are newly involved) --- key -> username
     270        max_new: highest index to use in keys for new_people
     271    """
     272
     273    kept = 0
     274    kept_not = 0
     275
     276    for person in people:
     277        if person in new_holders:
     278            if (person, role) in officers_map:
     279                if person not in moira_accounts:
     280                    pass # already errored above
     281                elif role.require_student and not moira_accounts[person].is_student():
     282                    msgs.append('Only students can have the %s role, and %s does not appear to be a student. (If this is not the case, please contact us.) You should replace this person ASAP.' % (role, person, ))
     283                #changes.append(("Kept", "yellow", person, role))
     284                kept += 1
     285            else:
     286                if person not in moira_accounts:
     287                    pass # already errored above
     288                elif role.require_student and not moira_accounts[person].is_student():
     289                    msgs.append('Only students can have the %s role, and %s does not appear to be a student. (If this is not the case, please contact us.)' % (role, person, ))
     290                else:
     291                    holder = groups.models.OfficeHolder(person=person, role=role, group=group,)
     292                    holder.save()
     293                    changes.append(("Added", "green", person, role))
     294        else:
     295            if (person, role) in officers_map:
     296                officers_map[(person, role)].expire()
     297                changes.append(("Removed", "red", person, role))
     298            else:
     299                kept_not += 1
     300                pass
     301    for i in range(max_new):
     302        if "extra.%d" % (i, ) in new_holders:
     303            if i in new_people:
     304                person = new_people[i]
     305                assert person in moira_accounts
     306                if role.require_student and not moira_accounts[person].is_student():
     307                    msgs.append('Only students can have the %s role, and %s does not appear to be a student.' % (role, person, ))
     308                else:
     309                    holder = groups.models.OfficeHolder(person=person, role=role, group=group,)
     310                    holder.save()
     311                    changes.append(("Added", "green", person, role))
     312
     313    return kept, kept_not
     314
    227315@login_required
    228316def manage_officers(request, pk, ):
     
    234322    max_new = 4
    235323
    236     people, roles, name_map, officers_map = load_officers(group)
     324    people, roles, name_map, officers_map = manage_officers_load_officers(group)
    237325
    238326    msgs = []
     
    244332        edited = True
    245333
    246         new_people = {}
    247         moira_accounts = {}
    248         for i in range(max_new):
    249             key = "extra.%d" % (i, )
    250             if key in request.POST and request.POST[key] != "":
    251                 username = request.POST[key]
    252                 try:
    253                     moira_accounts[username] = groups.models.AthenaMoiraAccount.active_accounts.get(username=username)
    254                     new_people[i] = username
    255                 except groups.models.AthenaMoiraAccount.DoesNotExist:
    256                     msgs.append('Athena account "%s" appears not to exist. Changes involving them have been ignored.' % (username, ))
    257         for person in people:
    258             try:
    259                 moira_accounts[person] = groups.models.AthenaMoiraAccount.active_accounts.get(username=person)
    260             except groups.models.AthenaMoiraAccount.DoesNotExist:
    261                 msgs.append('Athena account "%s" appears not to exist. They can not be added to new roles. You should remove them from any roles they hold, if you have not already.' % (person, ))
     334        # Fill out moira_accounts with AthenaMoiraAccount objects for relevant people
     335        new_people, moira_accounts = manage_officers_load_accounts(max_new, people, request, msgs)
     336
     337        # Process changes
    262338        for role in roles:
    263339            key = "holders.%s" % (role.slug, )
     
    270346                )
    271347            else:
    272                 for person in people:
    273                     if person in new_holders:
    274                         if (person, role) in officers_map:
    275                             if person not in moira_accounts:
    276                                 pass # already errored above
    277                             elif role.require_student and not moira_accounts[person].is_student():
    278                                 msgs.append('Only students can have the %s role, and %s does not appear to be a student. (If this is not the case, please contact us.) You should replace this person ASAP.' % (role, person, ))
    279                             #changes.append(("Kept", "yellow", person, role))
    280                             kept += 1
    281                         else:
    282                             if person not in moira_accounts:
    283                                 pass # already errored above
    284                             elif role.require_student and not moira_accounts[person].is_student():
    285                                 msgs.append('Only students can have the %s role, and %s does not appear to be a student. (If this is not the case, please contact us.)' % (role, person, ))
    286                             else:
    287                                 holder = groups.models.OfficeHolder(person=person, role=role, group=group,)
    288                                 holder.save()
    289                                 changes.append(("Added", "green", person, role))
    290                     else:
    291                         if (person, role) in officers_map:
    292                             officers_map[(person, role)].expire()
    293                             changes.append(("Removed", "red", person, role))
    294                         else:
    295                             kept_not += 1
    296                             pass
    297                 for i in range(max_new):
    298                     if "extra.%d" % (i, ) in new_holders:
    299                         if i in new_people:
    300                             person = new_people[i]
    301                             assert person in moira_accounts
    302                             if role.require_student and not moira_accounts[person].is_student():
    303                                 msgs.append('Only students can have the %s role, and %s does not appear to be a student.' % (role, person, ))
    304                             else:
    305                                 holder = groups.models.OfficeHolder(person=person, role=role, group=group,)
    306                                 holder.save()
    307                                 changes.append(("Added", "green", person, role))
     348                kept_delta, kept_not_delta = manage_officers_sync_role_people(
     349                    group, role, new_holders,   # input arguments
     350                    msgs, changes,              # output arguments
     351                    officers_map, people, moira_accounts,   # ~background data
     352                    new_people, max_new,                    # new people data
     353                )
     354                kept += kept_delta
     355                kept_not += kept_not_delta
    308356
    309357        # mark as changed and reload the data
     
    311359            group.set_updater(request.user)
    312360            group.save()
    313         people, roles, name_map, officers_map = load_officers(group)
     361        people, roles, name_map, officers_map = manage_officers_load_officers(group)
    314362
    315363    officers_data = []
Note: See TracChangeset for help on using the changeset viewer.