def login(request): """ 登录 :param request: :return: """ # token = request.META.get("HTTP_TOKEN") # if not token or token != LOGIN_TOKEN: # return json_success_response(msg="未授权用户") data = request.POST username = data.get("username") password = data.get("password") try: employee, errmsg = ucenter.employee_login(username, password) if not employee: msg = u"用户名或者密码错误" logger.error( "login error.error username:%s password:%s error message:%s" % (username, password, errmsg)) return json_forbidden_response(msg=msg, json_data={}) else: user = User.update_cds_employee(employee) return json_success_response(json_data={"user_id": user.id}) except Exception as e: logger.info("login ucenter catch error%s" % traceback.format_exc()) msg = u"login when catch error" return json_error_response(msg=msg, json_data={})
def dispatch(self, request, *args, **kwargs): passed = self._check_permission(request, *args, **kwargs) if not passed: logger.error("permission denied", exc_info=True, extra={ "request": request, }) return json_forbidden_response({}) return super(PermissionRequiredMixin, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs): model_service = self.get_model_service() obj = model_service.get_obj(**kwargs) has_permissions = self.has_get_permissions(request, obj, *args, **kwargs) if not has_permissions: return json_forbidden_response(json_data={}, msg=u"您没有读取权限") json_data = {model_service.obj_name: model_service.obj_to_json(obj)} return json_success_response(json_data=json_data)
def delete(self, request, *args, **kwargs): model_service = self.get_model_service() obj = model_service.get_obj(**kwargs) has_permissions = self.has_delete_permissions(request, obj, *args, **kwargs) if not has_permissions: return json_forbidden_response(json_data={}, msg=u"您没有操作权限") obj.delete() logger.info("delete model %s, model_id %s" % (obj.__class__.__name__, obj.id)) return json_success_response(json_data={})
def post(self, request, *args, **kwargs): model_service = self.get_model_service() paras = json.loads(request.body) paras['request'] = request has_permissions = self.has_post_permissions(request, paras, *args, **kwargs) if not has_permissions: return json_forbidden_response(json_data={}, msg=u"您没有新建权限") obj = model_service.create(**paras) json_data = {model_service.obj_name: model_service.obj_to_json(obj)} return json_success_response(json_data=json_data)
def post(self, request, *args, **kwargs): model_service = self.get_model_service() paras = json.loads(request.body) paras['request'] = request has_permissions = self.has_post_permissions(request, paras, *args, **kwargs) if not has_permissions: return json_forbidden_response(json_data={}, msg=u"您没有新建权限") obj = model_service.create(**paras) json_data = { model_service.obj_name: model_service.obj_to_json(obj) } return json_success_response(json_data=json_data)
def get(self, request, *args, **kwargs): params = self.get_params(request, *args, **kwargs) page = params.pop("page", "") model_service = self.get_model_service(**params) pages = model_service.get_total_pages() has_permissions = self.has_get_queryset_permissions(request, params, *args, **kwargs) if not has_permissions: return json_forbidden_response(json_data={}, msg=u"您没有接口权限") current_page = self.get_current_page(page, pages) queryset = model_service.get_queryset(page=current_page) queryset_json = self.queryset_to_dict(request, model_service, queryset, *args, **kwargs) json_data = { model_service.queryset_name: queryset_json, "current_page": current_page, "pages": pages, "total_count": model_service.get_total_count(), } return json_success_response(json_data=json_data)
def get(self, request, *args, **kwargs): params = self.get_params(request, *args, **kwargs) page = params.pop("page", "") model_service = self.get_model_service(**params) pages = model_service.get_total_pages() has_permissions = self.has_get_queryset_permissions( request, params, *args, **kwargs) if not has_permissions: return json_forbidden_response(json_data={}, msg=u"您没有接口权限") current_page = self.get_current_page(page, pages) queryset = model_service.get_queryset(page=current_page) queryset_json = self.queryset_to_dict(request, model_service, queryset, *args, **kwargs) json_data = { model_service.queryset_name: queryset_json, "current_page": current_page, "pages": pages, "total_count": model_service.get_total_count(), } return json_success_response(json_data=json_data)
def bet(request): """ 下注 :return: """ try: data = request.POST game_id = data.get("game_id") money = abs(float(data.get("money", 5))) if money < 5: money = 5 elif money > 500: money = 500 user_id = data.get("user_id", 1) user = User.objects.get(id=user_id) user_choice = int(data.get("is_host", 0)) # 0: 战平 1: 主队, 2 客队 game = Game.objects.get(id=game_id) team_id = 0 if user_choice == 1: team_id = game.host_team_id else: team_id = game.other_team_id if not game.can_add or not game.user_can_odd: # 无法下注的情况, 比赛已经开始, 不能下注 return json_forbidden_response(json_data={}, msg="time_over") if not UserGameShip.can_start_bet( user_id=user_id, game_id=game_id, money=money, team_id=team_id): logger.info("user:%s can not bet that bet is more than 500 in %s" % (user_id, game_id)) return json_forbidden_response(json_data={}, msg="can not bet") game_stat_exists = GameStat.objects.filter(game_id=game_id).exists() if not game_stat_exists: # 不存在 game_stat = GameStat(game_id=game_id, host_win_add=game.host_win, other_win_add=game.other_win, equal_win_add=game.equal) else: game_stat = GameStat.objects.filter(game_id=game_id)[0] game_stat.host_win_add = game.host_win game.other_win_add = game.other_win game.equal_win_add = game.equal # game.success = u"win" user_choice_team_id = None if user_choice == 0: # 战平 # game.success = "equal" win_add = game.equal game_stat.equal_win_people += 1 game_stat.equal_win_money += money elif user_choice == 1: # 主队获胜 win_add = game.host_win game.win_id = game.host_team.id user_choice_team_id = game.host_team.id game_stat.host_win_people += 1 game_stat.host_win_money += money else: # 客队获胜 win_add = game.other_win game.win_id = game.other_team.id user_choice_team_id = game.other_team.id game_stat.other_win_people += 1 game_stat.other_win_money += money game_stat.save() logger.info(u"用户%s 在 比赛%s 中压%s元, %s胜利!" % (user.username, game_id, money, user_choice)) user.money -= int(money) user.save() logger.info("用户%s 扣去 %s元" % (user.username, money)) logger.info("start add user game ship") if user_choice_team_id: user_game_ship = UserGameShip(user=user, game_id=game_id, money=money, user_choice_team=user_choice_team_id, win_odd=win_add, extra_goal=game.extra_goal) else: user_game_ship = UserGameShip(user=user, game_id=game_id, money=money, win_odd=win_add, extra_goal=game.extra_goal) user_game_ship.save() return json_success_response(json_data={}) except Exception as e: logger.info("user:%s add %s money is team:%s in game:%s" % (user_id, money, user_choice, game_id)) logger.info("do it catch exception:%s" % str(e)) return json_error_response(json_data={}, msg="error")