Example #1
0
class WishManagement:
    """
        Takes care of wish management (adding, removing, updating ...)
    """

    def __init__(self):
        self.user_management = UserManagement()
        self.tag_management = TagManagement()

    def addWish(self, student, tags):
        """
            Add a user
            @param student: the student's thats register a wish
            @param tags: wish tags
        """

        if not tags:
            print "Please specify at least one tag"
            return

        if student.__class__ == str:
            student = student.strip()
            student = self.user_management.getStudent(student)

        w = self.getWish(student, tags)

        if w != None:
            print "Wish for: %s exist" % student
            return w

        w = Wish()
        w.student=student
        w.save()

        for t in tags:
            try:
                t = self.tag_management.addTag(t)
                w.tags.add(t)
            except Tag.DoesNotExist:
                print "Tag %s does not exist" % t

        print "Wish added for user %s" % student

        return w

    def deleteWish(self, wish):
        """
            Delete a wish
        """

        wish.delete()

    def getWish(self, student, tags):
        """
            get a wish (and your dream will come true)
            @param student: the students username
            @param tags: a list with tag names
        """

        if student.__class__ == str:
            student = self.user_management.getStudent(student)

        wishes = Wish.objects.filter(student=student)

        for w in wishes:
            wishtags = [t.name_of_tag for t in w.tags.all()]

            if set (wishtags) == set (tags):
                return w

        #If no wish found
        return None

    def getAStudentWishes(self, student):
        """
            Get all the wishes for a given student
            @param student: the student
            @return: list of wishes
        """

        if student.__class__ == str:
            student = self.user_management.getStudent(student)

        return Wish.objects.filter(student=student)

    def getAllWishesWithTag(self, tag):
        """
            Get all wishes with a given tag
            @param tag: the tag
            @return: a list of wishes
        """

        if tag.__class__ == str:
            tag = self.tag_management.getTag(tag)

        if tag == None:
            return []

        return [wish for wish in Wish.objects.all() for t in wish.tags.all() if t == tag]

    def flush(self):
        """
            Removes all entries in the wish table
        """

        Wish.objects.all().delete()
        print "Wish table flushed"
class GroupManagement:
    """
        Takes care of group management
    """

    def __init__(self):
        self.user_management = UserManagement()

    def addGroup(self, tags=[], students=[], oracle=None):
        """
            Create a new group
            @param tags: the group tags
            @param students: the students belonging to this group
            @param oracle: the oracle tutoring this group
        """
        group = Group()
        group.save()

        for tag in tags:
            group.tags.add(tag)

        for student in students:
            if not student == None:
                group.students.add(student)

        if not oracle == None:
            group.oracle = oracle

        return group

    def setOracle(self, oracle, group):
        """
            Assign an oracle to this group
            @param oracle: the oracle
            @param group: the group
        """
        try:
            group.oracle = oracle
        except:
            pass

    def getOracle(self, group):
        """
            Get a groups oracle
            @param group: the group
            @return: the oracle
        """

        return group.oracle

    def getStudents(self, group):
        """
            Get the students on a group
            @param group: the group
            @return: the students
        """

        return group.students

    def getTags(self, group):
        """
            Get the tags on a group
            @param group: the group
            @return: the tags
        """

        return group.tags

    def getGroups(self, student):
        """
            Get all groups that a student is part of
            @param student: the student
            @return: a set of groups
        """

        # if student is a string, get the student object
        student = self.user_management.getStudent(student)
        print student
        groups = []

        return [group for group in Group.objects.all() if student in group.students.all()]

    def flushGroups(self):
        """
            Delete all group objects
        """
        Group.objects.all().delete()