Ejemplo n.º 1
0
	def get(self, login):
		#	Validate User exists
		user, error = User.queryByLogin(login)
		if error:
			return res.resourceMissing(error)

		#	Retrieving user projects
		records = Mentor.query.filter_by(id_user42=user['id_user42']).all()
		if not records:
			res.resourceMissing("No projects found for user {}".format(login))
		
		#	Retrieving online users
		onlineUsers = Api42.onlineUsers()

		returnList = []
		for rec in records:
			data = mentor_schema.dump(rec).data

			#	Retrieve registered mentors for project in 'rec'
			query = Mentor.query.filter_by(id_project42=data['id_project42'], active=True).all()

			#	Ensure each mentor doesn't have a pending appointment
			goodMentors = []
			for q in query:
				appointment = Appointment.query.filter(Appointment.id_mentor==q.id, Appointment.status==Status['Pending']).first()
				if not appointment:
					goodMentors.append(q)

			queryData = mentors_schema.dump(goodMentors).data

			#	Matching only online users and excluding self (user with login = login)
			tmpList = [q for q in queryData for o in onlineUsers if q['id_user42'] == o['id'] and o['login'] != login]
			data['project'] = {'name': rec.project.name, 'id': rec.project.id, 'onlineMentors': len(tmpList)}
			returnList.append(data)
		return res.getSuccess(data=returnList)
Ejemplo n.º 2
0
    def get(self, projectId):

        #   Validating project exists
        queryProject = Project.query.filter_by(id_project42=projectId).first()
        if not queryProject:
            return res.resourceMissing("Project {} not found.".format(projectId))
        
        standingsList = []
        
        #   * Querying by order DESCENDING to retrieve top mentors for the specified project in each category *
        #   Top 5 ordering by rating
        topByRating = MentorStat.query \
            .join(Mentor) \
            .filter(Mentor.id_project42==projectId, Mentor.abletomentor==True) \
            .order_by(MentorStat.rating.desc(), MentorStat.totalappointments.desc()) \
            .limit(5)
        
        standingsList.append(str('topByRating'))
        standingsList.append(topByRating)

        #   Top 5 ordering by totalappointments
        topByAppointments = MentorStat.query \
            .join(Mentor) \
            .filter(Mentor.id_project42==projectId, Mentor.abletomentor==True) \
            .order_by(MentorStat.totalappointments.desc()) \
            .limit(5)
        standingsList.append(str('topByAppointments'))
        standingsList.append(topByAppointments)
        
        #   Top 5 ordering by cancelled appointments
        topByCancelledAppointments = MentorStat.query \
            .join(Mentor) \
            .filter(Mentor.id_project42==projectId, Mentor.abletomentor==True) \
            .order_by(MentorStat.cancelledappointments.desc()) \
            .limit(5)
        standingsList.append(str('topByCancelledAppointments'))
        standingsList.append(topByCancelledAppointments)

        standings = {}
        for st in standingsList:
            if type(st) is str:
                name = st
            else:
                objList = []
                for m in st:
                    mentor = mentor_schema.dump(getattr(m, 'mentor')).data
                    user = user_schema.dump(getattr(getattr(m, 'mentor'), 'user')).data
                    stats = mentorstatschema.dump(m).data
                    obj = {
                        'mentor': mentor,
                        'user': user,
                        'stats': stats
                    }
                    objList.append(obj)
                standings[name] = objList
        
        return res.getSuccess('Top 5 standings for project {}'.format(queryProject.name), standings)
Ejemplo n.º 3
0
    def get(self, mentorId):
        query = Mentor.query.filter_by(id=mentorId).first()
        if not query:
            return res.badRequestError(
                'no mentor record with id {} exists'.format(mentorId))

        data = mentor_schema.dump(query).data
        if not data:
            return res.internalServiceError('failed to build mentor data')

        return res.getSuccess(
            'mentor data retrieved for user {} on project {}'.format(
                query.user.login, query.project.name), data)