def create_undertake_need(self, request, team, **kwargs): if check_bad_words(kwargs["title"]) or check_bad_words( kwargs["description"]): abort(400, '含有非法词汇') team_needs = TeamNeed.objects.filter(team=team, type=2) if team_needs.count() == 0: team.score += get_score_stage(2) team.score_records.create(score=get_score_stage(2), type="初始数据", description="首次发布团队需求") team.save() n = team.needs.create(type=2) for k in kwargs: setattr(n, k, kwargs[k]) n.save() # 发布动态 action.send_member_need(team, n) # 增加积分 request.user.score += get_score_stage(1) request.user.score_records.create(score=get_score_stage(1), type="能力", description="发布一个团队需求") team.score += get_score_stage(1) team.score_records.create(score=get_score_stage(1), type="活跃度", description="发布一个团队需求") request.user.save() team.save() abort(200)
def get(self, request, need, offset=0, limit=10): """获取人员需求的加入申请列表 :param offset: 偏移量 :return: request.user 不为团队创始人时,404 :return: request.user 为团队创始人时 count: 申请总数 list: 申请列表 id: 申请者ID username: 申请者用户名 name: 申请者昵称 icon_url: 申请者头像 description: 备注 time_created: 申请时间 """ if request.user == need.team.owner: # 拉取人员需求下团队的加入申请信息 c = need.member_requests.count() qs = need.member_requests.all()[offset:offset + limit] l = [{ 'id': r.sender.id, 'username': r.sender.username, 'name': r.sender.name, 'icon_url': r.sender.icon, 'description': r.description, 'time_created': r.time_created } for r in qs] return JsonResponse({'count': c, 'list': l, 'code': 0}) abort(403, '只有队长可以操作')
def delete(self, request, comment): """删除用户评论""" if comment.entity == request.user or comment.author == request.user: comment.delete() abort(200) abort(403, '非法操作')
def get(self, request, competition): """判断当前用户是否关注了competition""" if request.user.followed_competitions.filter( followed=competition).exists(): abort(200) abort(403, '未关注该竞赛')
def post(self, request, competition, team, **kwargs): """上传文件""" if request.user != team.owner: abort(404, '只有队长才可以上传文件') if team.competitions.filter(competition=competition).count() == 0: abort(404, '未参加该竞赛') file = request.FILES.get('file') if not file: abort(400, '文件上传失败') filename = save_uploaded_file( file, competition.id, competition.status, team.id) if filename: try: competition_file = competition.team_files.get( status=competition.status, team=team, type=kwargs['type']) competition_file.file = filename competition_file.save() except ObjectDoesNotExist: competition.team_files.create( file=filename, status=competition.status, team=team, type=kwargs['type']) abort(200) abort(400, '文件保存失败')
def delete(self, request, exp): """删除用户的某条经历""" if exp.user != request.user: abort(403, '只能删除自己的经历') exp.delete() abort(200)
def get(self, request, team, offset=0, limit=10): """获取团队发出的的合作申请列表 :param offset: 偏移量 :return: request.user 不为团队创始人时,404 :return: request.user 为团队创始人时 count: 申请总数 list: 申请列表 team_id: 申请的团队ID need_id: 申请的需求ID title: 申请的需求标题 name: 申请团队名称 icon_url: 申请团队头像 time_created: 申请时间 """ if request.user == team.owner: # 拉取申请合作信息 c = team.cooperation_requests.count() qs = team.cooperation_requests.all()[offset:offset + limit] l = [{ 'team_id': r.need.team.id, 'id': r.need.id, 'name': r.need.team.name, 'title': r.need.title, 'icon_url': r.need.team.icon, 'time_created': r.time_created } for r in qs] return JsonResponse({'count': c, 'list': l, 'code': 0}) abort(403, '只有队长能操作')
def get(self, request, team, offset=0, limit=10): """获取当前团队的受邀列表 :param offset: 偏移量 :return: request.user 不为团队创始人时,404 :return: request.user 为团队创始人时 count: 邀请总数 list: 邀请列表 team_id: 邀请方团队ID need_id: 邀请方需求ID title: 邀请方需求标题 name: 邀请方团队名称 icon_url: 邀请方团队头像 time_created: 邀请时间 """ if request.user == team.owner: # 拉取邀请合作信息 c = team.cooperation_invitations.count() qs = team.cooperation_invitations.all()[offset:offset + limit] l = [{'team_id': r.inviter.id, 'need_id': r.need.id, 'title': r.need.title, 'name': r.inviter.name, 'icon_url': r.invitee.icon, 'time_created': r.time_created} for r in qs] return JsonResponse({'count': c, 'list': l}) abort(403, '只有队长可以操作')
def get(self, request, phone, **kwargs): qs = User.objects.filter(phone_number=phone) if not qs.exists(): abort(404, '用户不存在') return JsonResponse({ 'id': qs.first().id })
def post(self, request, competition, team, expert_id): expert = User.objects.filter(pk=expert_id).get() CompetitionTeamParticipator.objects.filter( competition=competition, team=team, ).update(rater=expert) abort(200)
def get(self, request, **kwargs): """对好友需求进行匹配""" goodat = request.user.goodat if not goodat: abort(403, '无擅长标签') tags = [tag.split("-")[-1] for tag in goodat.split(";")] needList = [] for friend in request.user.friends.all(): needList.append(friend.other_user.needs.all()) list = [] for item in needList: #一个好友发布的需求列表 for need in item: #需求 if need.competition != None or need.activity != None: #团队或活动需求跳过 continue field = need.field.replace(";", ";", 10) fields = field.split(";") count = 0 for field in fields: if field in tags: count += 1 if count != 0: list.append({"nums": count, "need": need_to_json(need)}) return JsonResponse({'count': len(list), 'list': list, 'code': 0})
def get(self, request, need, team, offset=0, limit=10): """获取需求的合作申请列表 :param offset: 偏移量 :return: request.user 不为团队创始人时,404 :return: request.user 为团队创始人时 count: 申请总数 list: 申请列表 id: 申请者ID team_id: 申请团队ID name: 申请团队名称 icon_url: 申请团队头像 time_created: 申请时间 """ if request.user == need.team.owner and need.team == team: # 拉取需求的申请合作信息 c = need.cooperation_requests.count() qs = need.cooperation_requests.all()[offset:offset + limit] l = [{ 'id': r.sender.owner.id, 'team_id': r.sender.id, 'name': r.sender.name, 'icon_url': r.sender.icon, 'time_created': r.time_created } for r in qs] return JsonResponse({'count': c, 'list': l, 'code': 0}) abort(403, '只有队长可以操作')
def post(self, request, team, **kwargs): """修改团队资料 :param team: 团队ID :param kwargs: name: 团队名 description: 团队简介 is_recruiting:是否招募新成员 url: 团队链接 advantage: 团队优势 business_stage: 工商阶段 financing_stage: 融资阶段 valuation: 团队估值 valuation_unit: 团队估值单位 province: city: county: fields: 团队领域,格式:'field1|field2' tags: 标签,格式:'tag1|tag2|tag3|...' """ if request.user != team.owner: abort(403, '只允许队长操作') fields = kwargs.pop('fields', None) tags = kwargs.pop('tags', None) for k in kwargs: if k == "name": name = kwargs['name'] # 昵称唯一性验证 if Team.enabled.filter(name=name).exclude( id=team.id).count() != 0: abort(403, '团队名已被注册') # 昵称非法词验证 if check_bad_words(name): abort(400, '团队名含非法词汇') if k == "description": if check_bad_words(kwargs['description']): abort(400, '含有非法词汇') setattr(team, k, kwargs[k]) if fields: fields = fields.split('|')[:2] if len(fields) < 2: fields.append('') team.field1, team.field2 = fields[0].strip(), fields[1].strip() if tags: tags = tags.split('|')[:5] with transaction.atomic(): team.tags.all().delete() order = 0 for tag in tags: tag = tag.strip() if tag: team.tags.create(name=tag, order=order) order += 1 team.save() abort(200)
def delete(self, request, need): """将需求标记成已删除""" if request.user != need.team.owner: abort(403, '只有队长可以操作') need.status = 2 need.save() abort(200)
def delete(self, request, need): """令当前用户取消关注需求""" qs = request.user.followed_need.filter(followed=need) if qs.exists(): qs.delete() abort(200) abort(403, '未关注过该需求')
def post(self, request, need): """令当前用户关注需求""" if request.user.followed_need.filter(followed=need).exists(): abort(403, '已经关注过该需求') request.user.followed_need.create(followed=need) request.user.save() abort(200)
def __add_invited_users(self, user, codes): for c in codes: u = User.enabled.filter(invitation_code=c) if not u: abort(404, '推荐码错误') user.invited.add(u) u.score_records.create(score=get_score_stage(4), type="活跃度", description="邀请码被使用")
def post(self, request, file, score='', comment=''): file.score = score file.comment = comment file.save() sum = 0 for f in File.objects.filter(competition=file.competition, team=file.team, status=file.status): sum += int(f.score) CompetitionTeamParticipator.objects.filter(competition=file.competition, team=file.team).update(score=sum) abort(200)
def delete(self, request, entity): """对某个对象取消点赞""" # 积分 request.user.score -= get_score_stage(1) request.user.score_records.create(score=-get_score_stage(1), type="活跃度", description="取消给他人点赞") request.user.save() entity.likers.filter(liker=request.user).delete() abort(200)
def inner(*args, **kwargs): if col in kwargs: try: v = kwargs.pop(col) kw = {col: int(v) if col == 'id' else v} obj = model.get(**kw) except ObjectDoesNotExist: abort(404) else: kwargs[object_name] = obj return function(*args, **kwargs)
def post(self, request, need): """令当前用户关注need""" if request.user.followed_needs.filter(followed=need).exists(): abort(403, '已经关注过该需求') request.user.followed_needs.create(followed=need) request.user.score += get_score_stage(1) request.user.score_records.create(score=get_score_stage(1), type="活跃度", description="增加一个关注") request.user.save() abort(200)
def delete(self, request, activity, **kwargs): if activity.owner_user != request.user: abort(400, '活动创建者才能删除活动') ActivitySign.objects.filter(activity=activity).delete() ActivityUserParticipator.objects.filter(activity=activity).delete() ActivityLiker.objects.filter(liked=activity).delete() ActivityFollower.objects.filter(followed=activity).delete() ActivityFavorer.objects.filter(favored=activity).delete() ActivityComment.objects.filter(entity=activity).delete() ActivityStageModel.objects.filter(activity=activity).delete() activity.delete() return HttpResponse()
def post(self, request, activity): """令当前用户关注activity""" if request.user.followed_activities.filter(followed=activity).exists(): abort(403, '已经关注过该活动') request.user.followed_activities.create(followed=activity) request.user.score += get_score_stage(1) request.user.score_records.create(score=get_score_stage(1), type="活跃度", description="增加一个关注") request.user.save() abort(200)
def delete(self, request, competition): """令当前用户取消关注competition""" qs = request.user.followed_competitions.filter(followed=competition) if qs.exists(): # 积分 request.user.score -= get_score_stage(1) request.user.score_records.create(score=-get_score_stage(1), type="活跃度", description="取消关注") qs.delete() abort(200) abort(403, '未关注过该竞赛')
def post(self, request, competition): """令当前用户关注competition""" if request.user.followed_competitions.filter( followed=competition).exists(): abort(403, '已经关注过该竞赛') request.user.followed_competitions.create(followed=competition) request.user.score += get_score_stage(1) request.user.score_records.create(score=get_score_stage(1), type="活跃度", description="增加一个关注") request.user.save() abort(200)
def delete(self, request, activity): """令当前用户取消关注activity""" qs = request.user.followed_activities.filter(followed=activity) if qs.exists(): # 积分 request.user.score -= get_score_stage(1) request.user.score_records.create(score=-get_score_stage(1), type="活跃度", description="取消关注") qs.delete() abort(200) abort(403, '未关注过该活动')
def delete(self, request, need): """令当前用户取消关注need""" qs = request.user.followed_needs.filter(followed=need) if qs.exists(): # 积分 request.user.score -= get_score_stage(1) request.user.score_records.create(score=-get_score_stage(1), type="活跃度", description="取消关注") qs.delete() abort(200) abort(403, '未关注过该需求')
def post(self, request, **kwargs): """新建团队,同时调用融云接口为该团队创建一个对应的群聊 :param kwargs: name: 团队名称 description: 团队描述 url: 团队链接 province: city: county: fields: 团队领域,格式:'field1|field2' tags: 标签,格式:'tag1|tag2|tag3|...' :return: team_id: 团队id """ name = kwargs.pop('name') fields = kwargs.pop('fields', None) tags = kwargs.pop('tags', None) # 昵称唯一性验证 if Team.enabled.filter(name=name).count() != 0: abort(403, '团队名已被注册') # 昵称非法词验证 if check_bad_words(name): abort(403, '团队名含非法词汇') team = Team(owner=request.user, name=name) team.save() for k in kwargs: setattr(team, k, kwargs[k]) fields = fields.split('|')[:2] if fields is not None else ('', '') team.field1, team.field2 = fields[0].strip(), fields[1].strip() if tags: tags = tags.split('|')[:5] with transaction.atomic(): team.tags.all().delete() order = 0 for tag in tags: tag = tag.strip() if tag: team.tags.create(name=tag, order=order) order += 1 team.save() action.create_team(request.user, team) request.user.score += get_score_stage(2) request.user.score_records.create( score=get_score_stage(2), type="能力", description="成功创建一个团队") request.user.save() return JsonResponse({'team_id': team.id})
def post(self, request, content): """用户意见反馈 :param content: 反馈内容 :return: 200 """ if request.user.feedback.count() == 0: request.user.score += get_score_stage(2) request.user.score_records.create( score=get_score_stage(2), type="活跃度", description="增加一条反馈") request.user.save() request.user.feedback.create(content=content) abort(200)
def post(self, request, need, team): """同意加入申请并将创始人加入自己团队(对方需发送过合作申请)""" if request.user != need.team.owner: abort(403, '只有队长能操作') if need.cooperation_requests.filter(sender=team).exists(): # 在事务中建立关系,并删除对应的申请合作 with transaction.atomic(): need.cooperation_requests.filter(sender=team).delete() if need.team.members.filter(user=team.owner).exists(): abort(200) # 保存需求的加入团队Id if len(need.members) > 0: need.members = need.members + "|" + str(team.id) else: need.members = str(team.id) need.save() need.team.members.create(user=team.owner) action.join_team(team.owner, need.team) request.user.score += get_score_stage(1) request.user.score_records.create(score=get_score_stage(1), type="能力", description="与其他团队合作") team.score += get_score_stage(1) team.score_records.create(score=get_score_stage(1), type="能力", description="与其他团队合作") request.user.save() team.save() abort(200) abort(403, '对方未发送过申请合作')