Example #1
0
    def _persist_group(self, group):
        """Save any group changes to the database"""
        group_repo = GroupRepository()
        group = group_repo.persist(group)
        group_repo.close()

        return group
Example #2
0
    def get_root_group(self):
        # TODO(roh7): this is a hack
        uiuc_name = "University of Illinois at Urbana-Champaign"
        ir = InstitutionRepository()
        institutions = ir.fetch_all()
        uiuc = None
        for inst in institutions:
            if inst.name == uiuc_name:
                uiuc = inst
                break
        ir.close()

        assert uiuc is not None, 'institution for UIUC group not found...'
        # with the UIUC group, find the term. we currently want UIUC Spring 2014.
        tr = TermRepository()
        gr = GroupRepository()
        term = tr.fetch_term_by_year_index(uiuc, 2014, 0)
        assert term is not None, 'Spring 2014 not found...'

        if term.group:
            root = gr.fetch(term.group)
        else:
            # create UIUC group
            root = Group()
            root.name = "UIUC " + term.name
            root.description = uiuc_name
            root.type = 0
            # make sure bidirectional references work
            root.academic_entity_id = term.id
            root = gr.persist(root)
            term.group = root.id
            tr.persist(term)
        tr.close()
        gr.close()
        return root
Example #3
0
    def _valid_request(self, user, prefix, group_id):
        group_repo = GroupRepository()
        group = group_repo.fetch(group_id)
        group_repo.close()

        if not group:
            print "Group doesn't exist."
            return False

        user_repo = UserRepository()
        member_list = user_repo.get_members_of_group(group_id)
        user_repo.close()

        if not any(member.id == user.id for member in member_list):
            print "User is not a member of the associated group."
            return False

        group_repo = GroupRepository()
        parent_group = group_repo.get_supergroup_of_group(group_id)
        group_repo.close()
        if not group_repo:
            print "Attempting to add membrer to the root."
            return False

        user_repo = UserRepository()
        parent_member_list = user_repo.get_members_of_group(parent_group.id)
        user_repo.close()
        if not any(member.id == user.id for member in parent_member_list):
            print "New user is not a member of the supergroup."
            return False

        return True
Example #4
0
    def _valid_request(self, user, attr, deadline_id, value):
        # User parameters must be defined
        if not user or not attr or not value or not deadline_id:
            print "Invalid Request. Parameters Missing"
            return False

        notes = value[0].decode("utf-8")

        # The deadline associated with the deadline_id must exist
        deadline_repo = DeadlineRepository()
        dead = deadline_repo.deadline_for_user(user.id, deadline_id)
        deadline_repo.close()

        if not dead or not dead.meta or not dead.group_id:
            print "Something is wrong with the deadline object."
            return False

        group_repo = GroupRepository()
        group = group_repo.fetch(dead.group_id)
        group_repo.close()

        if not group:
            print "Associated group doesn't exist."
            return False

        if not (dead.type == "PER" or
                (group.maintainerId == dead.group_id and dead.type == "END")):
            print "User cannot modify this deadline"
            return False

        return True
Example #5
0
    def _in_group(self, user, group):
        group_repo = GroupRepository()
        user_groups = group_repo.get_groups_of_user(user.id)
        group_repo.close()

        if not user_groups or not any(g.id == group.id for g in user_groups):
            print "User is not a member of group (" + group.id + ")"
            return False

        return True
Example #6
0
    def _perform_request(self, user, prefix, group_id):
        group_repo = GroupRepository()
        group = group_repo.fetch(group_id)
        group_repo.close()

        deadline_repo = DeadlineRepository()
        deadline_list = deadline_repo.find_deadlines_with_name_prefix(group_id, prefix)
        deadline_repo.close()

        formatted_names = [{"value": deadline.name} for deadline in deadline_list]

        return json.dumps(formatted_names)
Example #7
0
    def _perform_request(self, user, prefix, group_id):
        group_repo = GroupRepository()
        parent = group_repo.get_supergroup_of_group(group_id)
        group_repo.close()

        user_repo = UserRepository()
        user_list = user_repo.find_users_with_email_prefix(user.id, parent.id, prefix)
        user_repo.close()

        formatted_names = [{"value": u.email} for u in user_list]

        return json.dumps(formatted_names)
Example #8
0
    def _user_exists(self, user_id):
        # User must exist
        user_repo = GroupRepository()
        user = user_repo.fetch(user_id)
        user_repo.close()

        if not user:
            print "User (" + str(user_id) + ") doesn't exist."
            return False 

        return True


        pass
Example #9
0
    def _group_exists(self, group_id):
        # Group id is invalid
        if not group_id:
            print "Group ID (" + group_id + ") is invalid."
            return False

        group_repo = GroupRepository()
        group = group_repo.fetch(group_id)
        group_repo.close()

        if not group:
            print "Group (" + str(group_id) + ") doesn't exist."
            return False 

        return True
Example #10
0
 def _perform_request(self, user, name, values):
     gr = GroupRepository()
     group = gr.fetch(values['group'])
     gr.close()
     if group is None or group.academic_entity_type != 'term':
         print "Invalid group id."
         return
     tr = TermRepository()
     term = tr.fetch(group.academic_entity_id)
     tr.close()
     if term is None:
         print "Requested term not found."
         return
     cr = ClassRepository()
     classes = cr.find_classes_with_name_prefix(term, values['query'])
     cr.close()
     self.write(json.dumps([{"value":klass.name, "class_id":klass.id}
                           for klass in classes]))
Example #11
0
    def _valid_request(self, user, prefix, group_id):
        group_repo = GroupRepository()
        group = group_repo.fetch(group_id)
        group_repo.close()

        if not group:
            print "Group doesn't exist."
            return False

        user_repo = UserRepository()
        member_list = user_repo.get_members_of_group(group_id)
        user_repo.close()

        if not any(member.id == user.id for member in member_list):
            print "User is not a member of the associated group."
            return False

        return True
Example #12
0
    def _perform_request(self, user, name, values):
        class_id = values[u"class_id"]

        # get the appropriate class
        cr = ClassRepository()
        klass = cr.fetch(class_id)
        cr.close()

        if not klass:
            print "Invalid class id"
            return

        root_group = self.get_root_group()
        gr = GroupRepository()
        if not klass.group:
            # create the group
            group = Group()
            group.name = klass.name
            group.type = 0
            group.academic_entity_id = class_id
            group = gr.persist(group)
            gr.add_group_as_subgroup(root_group.id, group.id)
            klass.group = group.id
            # associate with the class
            cr = ClassRepository()
            cr.persist(klass)
            cr.close()
        else:
            group = gr.fetch(klass.group)
        gr.close()

        # assign the user as a member of the subgroup
        user.groups = user.groups + [group.id]
        self._persist_user(user)

        result = {}

        result['id'] = group.id
        result['name'] = group.name
        result['maintainer'] = ""

        self.write(json.dumps(result))
Example #13
0
    def _perform_request(self, user, name, values):
        meeting_time  = values[u"meeting_time"]
        meeting_message = values[u"meeting_message"]
        group_id = values[u"group_id"]
        curr_user = self.get_current_user()

        ur = UserRepository()
        users = ur.get_members_of_group(group_id)        
        ur.close()

        gr = GroupRepository()
        group = gr.fetch(group_id)
        gr.close()

        cm = CukeMail()
        cm.subject(group.name + " meeting @ " + meeting_time)
        cm.message(meeting_message)
        cm.send([user.email for user in users])

        self._persist_user(curr_user)
Example #14
0
    def _perform_request(self, user, name, values):
        group_id = values[u"group_id"]
        new_group_name = values[u"group_name"]
        new_group_desc = values[u"group_description"]

        curr_user = self.get_current_user()

        gr = GroupRepository()
        new_group = Group()
        new_group.name = new_group_name
        new_group.description = new_group_desc
        new_group.type = 0 # private group
        new_group.maintainerId = curr_user.id
        new_group = gr.persist(new_group)

        # assign the subgroup as a child of the parent group
        gr.add_group_as_subgroup(group_id, new_group.id)
        gr.close()

        # assign the user as a member of the subgroup
        user_repo = UserRepository()
        user_repo.add_user_to_group(curr_user, new_group)
        user_repo.close()

        self._persist_user(curr_user)

        result = {}

        result['id'] = new_group.id
        result['name'] = new_group.name
        user_repo2 = UserRepository()
        user = user_repo2.fetch(new_group.maintainerId)
        user_repo2.close()
        result['maintainer'] = user.name
        
        self.write(json.dumps(result))
        self.flush
        self.finish
Example #15
0
    def _valid_request(self, user, attr, deadline_id, value):
        # User parameters must be defined
        if not user or not attr or not value or not deadline_id:
            print "Invalid Request. Parameters Missing"
            return False

        new_time = value[0].decode("utf-8")

        # Strip the 0000- year off the time.
        new_time = datetime.strptime(new_time[5:], "%m-%d %H:%M")
        # Add the correct year
        new_time = new_time + (date(datetime.now().year, 1, 1) - date(new_time.year, 1, 1))
        if new_time < datetime.now():
            print "Invalid time"
            return False

        # The deadline associated with the deadline_id must exist
        deadline_repo = DeadlineRepository()
        dead = deadline_repo.deadline_for_user(user.id, deadline_id)
        deadline_repo.close()

        if not dead or not dead.name or not dead.group_id:
            print "Something is wrong with the deadline object."
            return False

        group_repo = GroupRepository()
        group = group_repo.fetch(dead.group_id)
        group_repo.close()
        if not group:
            print "Associated group doesn't exist."
            return False

        if not (dead.type == "PER" or
                (group.maintainerId == dead.group_id and dead.type == "END")):
            print "User cannot modify this deadline"
            return False

        return True
Example #16
0
    def _perform_request(self, user, name, values):
        group_id = values[u"group_id"]
        new_user_email = values[u"user_email"]

        group_repo = GroupRepository()
        group = group_repo.fetch(group_id)
        group_repo.close()

        new_user_repo = UserRepository()
        new_user = new_user_repo.get_user_by_email(new_user_email)
        new_user_repo.add_user_to_group(new_user, group)
        new_user_repo.close()

        self._persist_user(new_user)

        result = {}

        result['name'] = new_user.name
        result['email'] = new_user.email
        result['iconURL'] = new_user.iconSmallURL
 
        self.write(json.dumps(result))
        self.flush
        self.finish
Example #17
0
    def _valid_request(self, curr_user, name, values):
        # Malformed request
        if u"group_id" not in values or u"user_email" not in values:
            print "Malformed request detected."
            return False

        # Malformed request
        group_id = values[u"group_id"]
        new_user_email = values[u"user_email"]
        if not group_id or not new_user_email:
            print "Required data is missing."
            return False

        user_repo = UserRepository()
        member_list = user_repo.get_members_of_group(group_id)
        user_repo.close()
        if not any(member.id == curr_user.id for member in member_list):
            print "User is not a member of the associated group."
            return False

        group_repo = GroupRepository()
        parent_group = group_repo.get_supergroup_of_group(group_id)
        group_repo.close()
        if not group_repo:
            print "Cannot add member to the root group."
            return False

        user_repo = UserRepository()
        parent_member_list = user_repo.get_members_of_group(parent_group.id)
        user_repo.close()
        if not any(member.id == curr_user.id for member in parent_member_list):
            print "New user is not a member of the supergroup."
            return False

        # New user is already a member of the group
        new_user_repo = UserRepository()
        new_user = new_user_repo.get_user_by_email(new_user_email)
        new_user_repo.close()
        if not new_user:
            print "New user doesn't exist."
            return False

        if not new_user.groups:
            print "New user isn't a member of any groups."
            return False

        if group_id in new_user.groups:
            print "New user is already already a member of the group."
            return False

        group_repo = GroupRepository()
        group = group_repo.fetch(group_id)
        group_repo.close()
        if not group:
            print "Group doesn't exist"
            return False

        # 1 => Public
        if group.type == 1:
            print "Cannot add members to public groups"
            return False

        return True