Ignore:
Timestamp:
Mar 12, 2012, 6:57:29 AM (14 years ago)
Author:
Alex Dehnert <adehnert@…>
Branches:
master, space-access, stable, stage
Children:
f1e4d44
Parents:
0a0a96c
git-author:
Alex Dehnert <adehnert@…> (02/23/12 03:21:00)
git-committer:
Alex Dehnert <adehnert@…> (03/12/12 06:57:29)
Message:

office access: initial diff script

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asadb/space/models.py

    r6ff04b1 rbec7760  
     1import collections
    12import datetime
    23
    34from django.db import models
     5from django.db.models import Q
    46
    57import reversion
     
    2022            asa_str = "Non-ASA"
    2123        return u"%s (%s)" % (self.number, asa_str)
     24
     25    def build_access(self, time=None, group=None, ):
     26        """Assemble a list of who had access to this Space.
     27
     28        time:
     29            optional; indicate that you want access as of a particular time.
     30            If omitted, uses the present.
     31        group:
     32            optional; indicates that you want access via a particular group.
     33            If omitted, finds access via any group.
     34
     35        Return value:
     36            tuple (access, assignments, aces, errors)
     37            access is the main field that matters, but the others are potentially useful supplementary information
     38
     39            access:
     40                Group.pk -> (ID -> Set name)
     41                Indicates who has access. Grouped by group and ID number.
     42                Usually, the sets will each have one member, but ID 999999999 is decently likely to have several.
     43                The SpaceAccessListEntrys will be filtered to reflect assignments as of that time.
     44            assignments:
     45                [SpaceAssignment]
     46                QuerySet of all SpaceAssignments involving the space and group at the time
     47            aces:
     48                [SpaceAccessListEntry]
     49                QuerySet of all SpaceAccessListEntrys involving the space and group at the time.
     50                This is not filtered for the ace's group having a relevant SpaceAssignment.
     51            errors:
     52                [String]
     53                errors/warnings that occurred.
     54                Includes messages about groups no longer having access.
     55        """
     56
     57        if time is None:
     58            time = datetime.datetime.now()
     59        errors = []
     60        time_q = Q(end__gte=time, start__lte=time)
     61        assignments = SpaceAssignment.objects.filter(time_q, space=self)
     62        aces = SpaceAccessListEntry.objects.filter(time_q, space=self)
     63        if group:
     64            assignments = assignments.filter(group=group)
     65            aces = aces.filter(group=group)
     66        access = {}    # Group.pk -> (ID -> Set name)
     67        for assignment in assignments:
     68            if assignment.group.pk not in access:
     69                access[assignment.group.pk] = collections.defaultdict(set)
     70        for ace in aces:
     71            if ace.group.pk in access:
     72                access[ace.group.pk][ace.card_number].add(ace.name)
     73            else:
     74                # This group appears to no longer have access...
     75                errors.append("Group %s no longer has access to %s, but has live ACEs." % (ace.group, self, ))
     76        return access, assignments, aces, errors
     77
    2278reversion.register(Space)
    2379
Note: See TracChangeset for help on using the changeset viewer.