Example #1
0
    def find_by_anchor_perms(self, Model, user, verb, qsc=None):
        """
        Fetch a list of relevant items for a given user
        and verb in a module that has an anchor foreign
        key into the DataTree
        """

        user_cache_key = 'fbap__model__%s.%s__%s,%s,%s' % (
            Model.__module__, Model.__name__, hasattr(user, 'id') and user.id
            or user, hasattr(verb, 'id') and verb.id
            or verb, hasattr(qsc, 'id') and qsc.id or qsc)

        retVal = self.cache(user)[user_cache_key]

        if retVal is not None: return retVal

        q_list = self.bits_get_qsc(user, verb)
        #q_list = self.filter(id__in=(x.id for x in q_list)).select_related('qsc')
        query_list = []
        for bit in q_list:
            if bit.recursive:
                query_list.append(QTree(anchor__below=bit.qsc_id))
            else:
                query_list.append(Q(anchor=bit.qsc_id))

        query = Model.objects.filter(reduce(operator.or_, query_list,
                                            Q(id=-1))).distinct()
        if qsc is not None:
            query = query.filter(QTree(anchor__below=qsc))

        self.cache(user)[user_cache_key] = query

        # Operation Complete!
        return query
Example #2
0
 def get_regdate(self, ordering='startdate'):
     reg_verb = GetNode('V/Flags/Registration/Enrolled')
     reg_node_parent = self.program.anchor['Classes']
     bits = UserBit.valid_objects().filter(
         user=self.user,
         verb=reg_verb).filter(QTree(qsc__below=reg_node_parent))
     if bits.exists():
         return bits.order_by(ordering).values_list(
             'startdate', flat=True)[0].strftime("%Y-%m-%d %H:%M:%S")
Example #3
0
    def studentDesc(self):
        """ Return a description for each line item type that students can be filtered by. """
        student_desc = {}
        treeq = QTree(anchor__below=self.program_anchor_cached(parent=True))
        for i in LineItemType.objects.filter(treeq):
            student_desc[
                'extracosts_%d' %
                i.id] = """Students who have opted for '%s'""" % i.text

        return student_desc
Example #4
0
    def get_under_bit(userbit):
        """ Return all implications under a userbit.
        That is, the set of all A ==> B such that A is true
        because of userbit. """
        Q_qsc_null = Q(qsc_original__isnull=True)
        Q_verb_null = Q(verb_original__isnull=True)

        if not userbit.recursive:
            Q_qsc = Q(qsc_original=userbit.qsc)
            Q_verb = Q(verb_original=userbit.verb)
        else:
            Q_qsc = QTree(qsc_original__below=userbit.qsc_id)

            Q_verb = QTree(verb_original__below=userbit.verb_id)

        # if one of the two are null, the other one can match and it'd be fine.
        Q_match = (Q_qsc & Q_verb) | (Q_qsc_null & Q_verb) | (Q_qsc
                                                              & Q_verb_null)

        return UserBitImplication.objects.filter(Q_match).distinct()
Example #5
0
    def apply(self):
        " This will generate the userbits for this implication. "
        if self.qsc_original_id is None and self.verb_original_id is None:
            return
        if self.qsc_original_id is not None:
            Q_qsc = (QTree(qsc__above = self.qsc_original) &\
                     Q(recursive       = True)) \
                     | \
                     Q(qsc = self.qsc_original_id)

        if self.verb_original_id is not None:
            Q_verb = (QTree(verb__above = self.verb_original) &\
                      Q(recursive       = True)) \
                      | \
                      Q(verb = self.verb_original_id)

        userbits = UserBit.objects

        if self.qsc_original_id is not None:
            userbits = userbits.filter(Q_qsc)
        if self.verb_original_id is not None:
            userbits = userbits.filter(Q_verb)

        for userbit in userbits:
            # for each userbit we're going to create the correct
            # corresponding userbits.

            newbit = self.impliedBit(userbit)  # get this bit this implies

            bits = UserBit.objects.filter(user=newbit.user,
                                          qsc=newbit.qsc,
                                          verb=newbit.verb)
            if self.recursive:
                bits.filter(recursive=True)

            if len(bits.values('id')[:1]) == 0:
                newbit.save()
                self.created_bits.add(newbit)
Example #6
0
    def students(self, QObject=False):
        """ Return the useful lists of students for the Extra Costs module. """

        student_lists = {}
        treeq = QTree(anchor__below=self.program_anchor_cached(parent=True))

        # Get all the line item types for this program.
        for i in LineItemType.objects.filter(treeq):
            if QObject:
                student_lists['extracosts_%d' % i.id] = self.getQForUser(
                    Q(accounting_lineitem__li_type=i))
            else:
                student_lists['extracosts_%d' % i.id] = ESPUser.objects.filter(
                    accounting_lineitem__li_type=i).distinct()

        return student_lists
Example #7
0
    def user_has_TYPE(self, user, node, node_type='qsc'):
        """
        Returns true if the user has the verb anywhere. False otherwise.
        """
        node_id = getattr(node, 'id', node)
        col_filter = '%s__above' % node_type
        cache_key = 'has_%s__%s' % (node_type, node_id)

        retVal = self.cache(user)['has_%s__%s' % (node_type, node_id)]

        if retVal is not None:
            return retVal
        else:
            now = datetime.datetime.now()
            retVal = bool(
                self.filter(QTree(**{col_filter: node_id}),
                            user=user,
                            startdate__lte=now,
                            enddate__gte=now))
            self.cache(user)[cache_key] = retVal

        return retVal