def __create_hackathon(self, creator, context): """Insert hackathon and creator(admin of course) to database We enforce that default config are used during the creation :type context: Context :param context: context of the args to create a new hackathon :rtype: Hackathon :return hackathon instance """ new_hack = Hackathon( name=context.name, display_name=context.display_name, ribbon=context.get("ribbon"), description=context.get("description"), short_description=context.get("short_description"), location=context.get("location"), banners=context.get("banners", []), status=HACK_STATUS.INIT, creator=creator, type=context.get("type", HACK_TYPE.HACKATHON), config=context.get("config", Context()).to_dict(), tags=context.get("tags", []), event_start_time=context.get("event_start_time"), event_end_time=context.get("event_end_time"), registration_start_time=context.get("registration_start_time"), registration_end_time=context.get("registration_end_time"), judge_start_time=context.get("judge_start_time"), judge_end_time=context.get("judge_end_time") ) # basic xss prevention if new_hack.description: # case None type new_hack.description = self.cleaner.clean_html(new_hack.description) # insert into table hackathon new_hack.save() # add the current login user as admin and creator try: admin = UserHackathon(user=creator, hackathon=new_hack, role=HACK_USER_TYPE.ADMIN, status=HACK_USER_STATUS.AUTO_PASSED, remark='creator') admin.save() except Exception as ex: # TODO: send out a email to remind administrator to deal with this problems self.log.error(ex) raise InternalServerError("fail to create the default administrator") return new_hack
def __create_hackathon(self, creator, context): """Insert hackathon and creator(admin of course) to database We enforce that default config are used during the creation :type context: Context :param context: context of the args to create a new hackathon :rtype: Hackathon :return hackathon instance """ new_hack = Hackathon( name=context.name, display_name=context.display_name, ribbon=context.get("ribbon"), description=context.get("description"), short_description=context.get("short_description"), location=context.get("location"), banners=context.get("banners", []), status=HACK_STATUS.INIT, creator=creator, type=context.get("type", HACK_TYPE.HACKATHON), config=context.get("config", Context()).to_dict(), tags=context.get("tags", []), event_start_time=context.get("event_start_time"), event_end_time=context.get("event_end_time"), registration_start_time=context.get("registration_start_time"), registration_end_time=context.get("registration_end_time"), judge_start_time=context.get("judge_start_time"), judge_end_time=context.get("judge_end_time")) # basic xss prevention if new_hack.description: # case None type new_hack.description = self.cleaner.clean_html( new_hack.description) new_hack.save() # add the current login user as admin and creator try: admin = UserHackathon(user=creator, hackathon=new_hack, role=HACK_USER_TYPE.ADMIN, status=HACK_USER_STATUS.AUTO_PASSED, remark='creator') admin.save() except Exception as ex: # TODO: send out a email to remind administrator to deal with this problems self.log.error(ex) raise InternalServerError( "fail to create the default administrator") return new_hack
def like_hackathon(self, user, hackathon): user_hackathon = UserHackathon.objects(hackathon=hackathon, user=user).first() if not user_hackathon: user_hackathon = UserHackathon(hackathon=hackathon, user=user, role=HACK_USER_TYPE.VISITOR, status=HACK_USER_STATUS.UNAUDIT, like=True, remark="") user_hackathon.save() if not user_hackathon.like: user_hackathon.like = True user_hackathon.save() # increase the count of users that like this hackathon self.increase_hackathon_stat(hackathon, HACKATHON_STAT.LIKE, 1) return ok()
def add_admin(self, args): """Add a new administrator on a hackathon :type args: dict :param args: http request body in json format :return hackathon response 'ok' if successfully added. 'not_found' if email is invalid or user not found. 'internal_server_error' if any other unexpected exception caught """ user = self.user_manager.get_user_by_id(args.get("id")) if user is None: return not_found("user not found") if self.register_manager.is_user_registered(user.id, g.hackathon): return precondition_failed( "Cannot add a registered user as admin", friendly_message="该用户已报名参赛,不能再被选为裁判或管理员。请先取消其报名") try: user_hackathon = UserHackathon.objects( user=user.id, hackathon=g.hackathon.id).first() if user_hackathon is None: uhl = UserHackathon(user=user, hackathon=g.hackathon, role=args.get("role"), status=HACK_USER_STATUS.AUTO_PASSED, remark=args.get("remark")) uhl.save() else: user_hackathon.role = args.get("role") user_hackathon.remark = args.get("remark") user_hackathon.save() return ok() except Exception as e: self.log.error(e) return internal_server_error("create admin failed")
def like_hackathon(self, user, hackathon): user_hackathon = UserHackathon.objects(hackathon=hackathon, user=user).first() if user_hackathon and user_hackathon.like: return ok() if not user_hackathon: user_hackathon = UserHackathon(hackathon=hackathon, user=user, role=HACK_USER_TYPE.VISITOR, status=HACK_USER_STATUS.UNAUDIT, like=True, remark="") user_hackathon.save() if not user_hackathon.like: user_hackathon.like = True user_hackathon.save() # increase the count of users that like this hackathon self.increase_hackathon_stat(hackathon, HACKATHON_STAT.LIKE, 1) return ok()
def add_admin(self, args): """Add a new administrator on a hackathon :type args: dict :param args: http request body in json format :return hackathon response 'ok' if successfully added. 'not_found' if email is invalid or user not found. 'internal_server_error' if any other unexpected exception caught """ user = self.user_manager.get_user_by_id(args.get("id")) if user is None: return not_found("user not found") if self.register_manager.is_user_registered(user.id, g.hackathon): return precondition_failed("Cannot add a registered user as admin", friendly_message="该用户已报名参赛,不能再被选为裁判或管理员。请先取消其报名") try: user_hackathon = UserHackathon.objects(user=user.id, hackathon=g.hackathon.id).first() if user_hackathon is None: uhl = UserHackathon( user=user, hackathon=g.hackathon, role=args.get("role"), status=HACK_USER_STATUS.AUTO_PASSED, remark=args.get("remark") ) uhl.save() else: user_hackathon.role = args.get("role") user_hackathon.remark = args.get("remark") user_hackathon.save() return ok() except Exception as e: self.log.error(e) return internal_server_error("create admin failed")