def __init__(self, user_id, role, anonymous=False): self.user_id = user_id self.role = role self.anonymous = anonymous self.timestamp_now = int(datetime.datetime.now().strftime('%s')) self.classroom_helper = ClassroomHelper() self.classroom_name_dict = dict() self.profile_dict = dict()
def send_request_to_join(request): user_id = request.user.username req_data = json.loads(request.body) role = req_data['role'] classroom_uuid = req_data['classroom_uuid'] comment = req_data['comment'] if 'comment' in req_data else None classroom_helper = ClassroomHelper(user_id, role) request_uuid = classroom_helper.send_request_to_join(classroom_uuid, comment=comment) # update cache UpdateHelper(user_id, role, timestamp=classroom_helper.timestamp_now).new_pending_request( user_id, classroom_uuid, request_uuid) res_data = dict(error=0, request_uuid=request_uuid) return Response(data=res_data, status=status.HTTP_200_OK)
def search(request): user_id = request.user.username role = request.GET['role'] keyword = request.GET['keyword'] classrooms = ClassroomHelper(user_id, role).search_classrooms(keyword) res_data = dict(error=0, classrooms=classrooms) return Response(data=res_data, status=status.HTTP_200_OK)
def close(request): user_id = request.user.username req_data = json.loads(request.body) role = req_data['role'] classroom_uuid = req_data['classroom_uuid'] success = ClassroomHelper(user_id, role).close_classroom(classroom_uuid) res_data = dict(success=success) return Response(data=res_data, status=status.HTTP_200_OK)
def new_homework(self, classroom_uuid): """T & S""" classroom_helper = ClassroomHelper() members = classroom_helper.get_members(classroom_uuid) classroom_name = classroom_helper.get_classroom_name_by_uuid(classroom_uuid) item_dict = dict( classroom_uuid=classroom_uuid, classroom_name=classroom_name, timestamp=self.timestamp ) for member in members: user_id = member['user_id'] role = member['role'] key = self.__get_key(user_id, role, UPDATE_KEY_TYPES['homeworks']) self.__update_value(key, item_dict) # send message message = MESSAGE['homeworks'] tasks.send_apns_notification.delay(user_id, message)
def school_close(request): user_id = request.user.username req_data = json.loads(request.body) school_uuid = req_data['school_uuid'] school_helper = SchoolHelper(user_id) success = school_helper.close_school(school_uuid) if success: ClassroomHelper( user_id=user_id).close_classrooms_by_school(school_uuid) res_data = dict(success=success, timestamp=school_helper.timestamp_now) return Response(data=res_data, status=status.HTTP_200_OK)
def create(request): user_id = request.user.username req_data = json.loads(request.body) role = req_data['role'] name = req_data['name'] introduction = req_data['introduction'] school_uuid = req_data['school_uuid'] members = req_data['members'] if 'members' in req_data else None classroom_helper = ClassroomHelper(user_id, role) result_tup = classroom_helper.create_new_classroom(name, school_uuid, introduction, members=members) classroom_uuid = result_tup[0] classroom_code = result_tup[1] res_data = dict(error=0, classroom_uuid=classroom_uuid, code=classroom_code, timestamp=classroom_helper.timestamp_now) return Response(data=res_data, status=status.HTTP_200_OK)
def update(request): user_id = request.user.username req_data = json.loads(request.body) role = req_data['role'] classroom_uuid = req_data['classroom_uuid'] name = req_data['classroom_name'] if 'classroom_name' in req_data else None introduction = req_data[ 'introduction'] if 'introduction' in req_data else None members = req_data['members'] if 'members' in req_data else None success = ClassroomHelper(user_id, role).update_classroom_info( classroom_uuid, name=name, introduction=introduction, members=members) res_data = dict(error=success) return Response(data=res_data, status=status.HTTP_200_OK)
def approve_request(request): user_id = request.user.username req_data = json.loads(request.body) role = req_data['role'] request_uuid = req_data['request_uuid'] print request_uuid success, requester_user_id, requester_role, classroom_uuid = ClassroomHelper( user_id, role).approve_request(request_uuid) # update cache UpdateHelper(user_id, role).request_approved(requester_user_id, requester_role, classroom_uuid) res_data = dict(success=success) return Response(data=res_data, status=status.HTTP_200_OK)
def request_approved(self, requester_user_id, requester_role, classroom_uuid): """T & S""" approver = self.user_id approver_profile_info = ProfileHelper(approver).get_profile() classroom_info = ClassroomHelper(user_id=requester_user_id, role=requester_role).get_classroom_info(classroom_uuid) item_dict = dict( approver_profile_info=approver_profile_info, classroom_info=classroom_info, timestamp=self.timestamp ) key = self.__get_key(requester_user_id, requester_role, UPDATE_KEY_TYPES['approvals']) self.__update_value(key, item_dict) # send message classroom_name = classroom_info['classroom_name'] if 'classroom_name' in classroom_info else "" message = MESSAGE['approvals'].format(classroom_name) tasks.send_apns_notification.delay(requester_user_id, message)
def new_pending_request(self, requester, classroom_uuid, request_uuid): """T & S""" creator_user_id = ClassroomHelper().get_creator_by_classroom(classroom_uuid) requester_profile_info = ProfileHelper(requester).get_profile() item_dict = dict( requester_profile=requester_profile_info, classroom_uuid=classroom_uuid, request_uuid=request_uuid, requester_role=self.role, timestamp=self.timestamp ) key = self.__get_key(creator_user_id, 't', UPDATE_KEY_TYPES['requests']) self.__update_value(key, item_dict) # send message message = MESSAGE['requests'].format(requester_profile_info['nickname']) tasks.send_apns_notification.delay(creator_user_id, message)
class QAHelper(object): def __init__(self, user_id, role, anonymous=False): self.user_id = user_id self.role = role self.anonymous = anonymous self.timestamp_now = int(datetime.datetime.now().strftime('%s')) self.classroom_helper = ClassroomHelper() self.classroom_name_dict = dict() self.profile_dict = dict() def create_question(self, classroom_uuid, content): question_uuid = uuid.uuid1() school_uuid = self.classroom_helper.get_school_uuid_by_classroom_uuid( classroom_uuid) Question(uuid=question_uuid, classroom_uuid=classroom_uuid, school_uuid=school_uuid, creator=self.user_id, role=self.role, anonymous=self.anonymous, active=True, answer_count=0, content=content, created_timestamp=self.timestamp_now, updated_timestamp=self.timestamp_now).save() def close_question(self, question_uuid): try: row = Question.objects.get(uuid=question_uuid) creator = row.creator if creator == self.user_id: row.active = False row.save() success = True else: success = False except Question.DoesNotExist: success = False return success def create_answer(self, question_uuid, classroom_uuid, content): success = True answer_uuid = uuid.uuid1() Answer( uuid=answer_uuid, question_uuid=question_uuid, classroom_uuid=classroom_uuid, creator=self.user_id, role=self.role, anonymous=self.anonymous, content=content, agree_count=0, disagree_count=0, created_timestamp=self.timestamp_now, ).save() try: row = Question.objects.get(uuid=question_uuid) row.answer_count = F('answer_count') + 1 row.updated_timestamp = self.timestamp_now row.save() except Question.DoesNotExist: success = False return success def agree_answer(self, answer_uuid): Answer.objects.filter(uuid=answer_uuid).update( agree_count=F('agree_count') + 1) def disagree_answer(self, answer_uuid): Answer.objects.filter(uuid=answer_uuid).update( disagree_count=F('disagree_count') + 1) def get_question_list(self, filter_type, page_number, school_uuid, per_page=20): questions = [] if filter_type == 'new': p = Paginator( Question.objects.filter( active=True).order_by('-updated_timestamp'), per_page) elif filter_type == 'popular': p = Paginator( Question.objects.filter(active=True).order_by( '-answer_count', '-updated_timestamp'), per_page) elif filter_type == 'me': p = Paginator( Question.objects.filter( active=True, creator=self.user_id).order_by('-updated_timestamp'), per_page) else: p = Paginator( Question.objects.filter( active=True).order_by('-updated_timestamp'), per_page) num_pages = p.num_pages if page_number <= num_pages: page_objects = p.page(page_number).object_list for row in page_objects: classroom_uuid = row.classroom_uuid classroom_name = self.__get_classroom_name(classroom_uuid) anonymous = row.anonymous creator = row.creator profile = self.__get_profile(creator, anonymous) nickname = profile['nickname'] if profile else None img_url = profile['img_url'] if profile else None questions.append( dict(question_uuid=row.uuid, classroom_uuid=classroom_uuid, classroom_name=classroom_name, school_uuid=school_uuid, creator=creator, nickname=nickname, img_url=img_url, role=row.role, anonymous=anonymous, answer_count=row.answer_count, content=row.content, created_timestamp=row.created_timestamp, updated_timestamp=row.updated_timestamp)) return questions def get_answer_list(self, question_uuid, page_number, per_page=20): answers = [] p = Paginator( Answer.objects.filter(question_uuid=question_uuid).order_by( '-agree_count', '-created_timestamp'), per_page) num_pages = p.num_pages if page_number <= num_pages: page_objects = p.page(page_number).object_list for row in page_objects: classroom_uuid = row.classroom_uuid classroom_name = self.__get_classroom_name(classroom_uuid) anonymous = row.anonymous creator = row.creator profile = self.__get_profile(creator, anonymous) nickname = profile['nickname'] if profile else None img_url = profile['img_url'] if profile else None answers.append( dict(answer_uuid=row.uuid, question_uuid=row.question_uuid, classroom_uuid=classroom_uuid, classroom_name=classroom_name, creator=creator, nickname=nickname, img_url=img_url, role=row.role, anonymous=anonymous, content=row.content, agree_count=row.agree_count, disagree_count=row.disagree_count, created_timestamp=row.created_timestamp)) return answers def __get_classroom_name(self, classroom_uuid): if classroom_uuid in self.classroom_name_dict: classroom_name = self.classroom_name_dict[classroom_uuid] else: classroom_name = self.classroom_helper.get_classroom_name_by_uuid( classroom_uuid) self.classroom_name_dict[classroom_uuid] = classroom_name return classroom_name def __get_profile(self, user_id, anonymous): if anonymous: profile = None elif user_id in self.profile_dict: profile = self.profile_dict[user_id] else: profile = ProfileHelper(user_id).get_profile() self.profile_dict[user_id] = profile return profile
def get_list(request): user_id = request.user.username role = request.GET['role'] classrooms = ClassroomHelper(user_id, role).get_classroom_list() res_data = dict(error=0, classrooms=classrooms) return Response(data=res_data, status=status.HTTP_200_OK)