Example #1
0
	def post(self, request):
		try:
			# 请求格式错误处理
			request_msg = json.loads(request.body)
			if not isinstance(request_msg, dict):
				return JsonResponse({"msg": "typeErr_dict"})
		except Exception as e:
			request_msg = {}

		# 用户登录状态校验
		user = checkStatus(request.COOKIES)
		if not isinstance(user, User):
			return JsonResponse({"msg": "userErr_unsignIn"})

		# 未激活用户不能修改
		if not user.is_active:
			return JsonResponse({"msg": "userErr_unActive"})

		# 修改信息
		user.nickname = request_msg.get("nickname", "")
		user.gender = int(request_msg.get("gender", 1))
		user.age = int(request_msg.get("age", 18))
		user.head_img = request_msg.get("head_img", "")
		user.self_desc = request_msg.get("self_desc", "")

		# 修改入库
		user.save()

		return JsonResponse({"msg": "change successfully"})
Example #2
0
    def post(self, request):
        try:
            # 请求格式错误处理
            request_msg = json.loads(request.body)
            if not isinstance(request_msg, dict):
                return JsonResponse({"msg": "typeErr_dict"})
        except Exception as e:
            request_msg = {}

        # 用户登录状态校验
        user = checkStatus(request.COOKIES)
        if not isinstance(user, User):
            return JsonResponse({"msg": "userErr_unsignIn"})
        object_list = Activity.objects.filter(
            owner_id=user.id).order_by("-activity_time")

        # 将全部结果分页
        paginator = Paginator(object_list, 4)
        page = int(request_msg.get("page"))
        try:
            activities = paginator.page(page)
        except PageNotAnInteger:
            activities = paginator.page(1)
        except EmptyPage:
            return JsonResponse({"msg": "numErr_Empty"})

        # response 拼接
        response = response_construct(activities, user)

        return JsonResponse({"activities": response, "msg": "paginatorSuc"})
Example #3
0
    def post(self, request):
        try:
            # 请求格式错误处理
            request_msg = json.loads(request.body)
            if not isinstance(request_msg, dict):
                return JsonResponse({"msg": "typeErr_dict"})
        except Exception as e:
            request_msg = {}

        # 用户登录状态校验
        user = checkStatus(request.COOKIES)
        if not isinstance(user, User):
            return JsonResponse({"msg": "userErr_unsignIn"})

        activity = Activity()
        activity.activity_name = request_msg.get("activity_name", "")
        activity.activity_desc = request_msg.get("activity_desc", "")
        activity_time = request_msg.get("activity_time", "").strip()
        ymd, hms = activity_time.split(" ")
        year, month, day = list(map(int, ymd.split("-")))
        hour, minute = list(map(int, hms.split(":")))
        activity.activity_time = datetime(year, month, day, hour, minute, 0)
        activity.activity_site = request_msg.get("activity_site", "")
        activity.limit_num = request_msg.get("limit_num", 0)
        activity.owner_id = user.id
        # activity.limit_requirement = request_msg.get("limit_requirement", "")
        activity.activity_type = request_msg.get("activity_type", "")

        # 各字段缺失情况处理
        if not all([
                activity.activity_name, activity.activity_desc,
                activity.activity_time, activity.activity_site,
                activity.limit_num, activity.owner_id, activity.activity_type
        ]):
            return JsonResponse({"msg": "fieldErr_lose"})

        # 数据入库
        activity.save()

        # 创建用户和活动关系映射
        uamap = UserActivityMap()
        uamap.user_id = user.id
        uamap.activity_id = activity.id
        uamap.save()

        return JsonResponse({"msg": "createActivity Successfully"})
Example #4
0
    def post(self, request):
        try:
            # 请求格式错误处理
            request_msg = json.loads(request.body)
            if not isinstance(request_msg, dict):
                return JsonResponse({"msg": "typeErr_dict"})
        except Exception as e:
            request_msg = {}

        # 用户登录状态校验
        user = checkStatus(request.COOKIES)
        if not isinstance(user, User):
            return JsonResponse({"msg": "userErr_unsignIn"})

        activity_id = request_msg.get("id", 0)
        try:
            activity = Activity.objects.get(id=activity_id)
        except Exception as e:
            return JsonResponse({"msg": "activityErr_id"})

        try:
            uamap = UserActivityMap.objects.get(user_id=user.id,
                                                activity_id=activity.id)
        except Exception as e:
            return JsonResponse({"msg": "uamapErr_notExist"})
        uamap.delete()

        user.credit_score -= 5
        if user.id != activity.owner_id:
            score = user.credit_score
            user.save()
            return JsonResponse({
                "msg": "quit successfully",
                "credit_score": score
            })

        activity.delete()
        user.credit_score -= 5
        score = user.credit_score
        return JsonResponse({
            "msg": "dissolution successfully",
            "credit_score": score
        })
Example #5
0
    def post(self, request):
        try:
            # 请求格式错误处理
            request_msg = json.loads(request.body)
            if not isinstance(request_msg, dict):
                return JsonResponse({"msg": "typeErr_dict"})
        except Exception as e:
            request_msg = {}

        # 用户登录状态校验
        user = checkStatus(request.COOKIES)
        if not isinstance(user, User):
            return JsonResponse({"msg": "userErr_unsignIn"})

        uamap = UserActivityMap()
        activity_id = int(request_msg.get("activity_id", 0))
        if not activity_id:
            return JsonResponse({"msg": "activityErr_notExist"})
        uamap.activity_id = activity_id
        uamap.user_id = user.id
        uamap.save()
        return JsonResponse({"msg": "join successfully"})
Example #6
0
    def post(self, request):
        try:
            # 请求格式错误处理
            request_msg = json.loads(request.body)
            if not isinstance(request_msg, dict):
                return JsonResponse({"msg": "typeErr_dict"})
        except Exception as e:
            request_msg = {}

        # 用户登录状态校验
        user = checkStatus(request.COOKIES)
        if not isinstance(user, User):
            return JsonResponse({"msg": "userErr_unsignIn"})

        # 查询该用户相关的所有活动
        activities = [
            map.activity_id
            for map in UserActivityMap.objects.filter(user_id=user)
        ]

        # 筛选出活动时间早于当前的
        now = datetime.now()
        object_list = Activity.objects.filter(activity_time__gt=now).filter(
            id__in=activities).order_by("activity_time")

        # 将全部结果分页
        paginator = Paginator(object_list, 4)
        page = int(request_msg.get("page"))
        try:
            activities = paginator.page(page)
        except PageNotAnInteger:
            activities = paginator.page(1)
        except EmptyPage:
            return JsonResponse({"msg": "numErr_Empty"})

        # response 拼接
        response = response_construct(activities, user)

        return JsonResponse({"activities": response, "msg": "paginatorSuc"})
Example #7
0
    def post(self, request):
        """修改活动"""
        try:
            # 请求格式错误处理
            request_msg = json.loads(request.body)
            if not isinstance(request_msg, dict):
                return JsonResponse({"msg": "typeErr_dict"})
        except Exception as e:
            request_msg = {}

        # 用户登录状态校验
        user = checkStatus(request.COOKIES)
        if not isinstance(user, User):
            return JsonResponse({"msg": "userErr_unsignIn"})

        activity_id = request_msg.get("id", 0)
        try:
            activity = Activity.objects.get(id=activity_id)
        except Exception as e:
            return JsonResponse({"msg": "activityErr_id"})

        activity.activity_name = request_msg.get("activity_name", "")
        activity.activity_desc = request_msg.get("activity_desc", "")
        activity_time = request_msg.get("activity_time", "").strip()
        ymd, hms = activity_time.split(" ")
        year, month, day = list(map(int, ymd.split("-")))
        hour, *minutes = list(map(int, hms.split(":")))
        if len(minutes) == 2:
            minute, second = minutes
        else:
            minute = minutes[0]
        activity.activity_time = datetime(year, month, day, hour, minute, 0)
        activity.activity_site = request_msg.get("activity_site", "")
        activity.limit_num = request_msg.get("limit_num", 0)

        # 数据入库
        activity.save()

        return JsonResponse({"msg": "change activity successfully"})
Example #8
0
    def post(self, request):
        # 用户登录状态校验
        user = checkStatus(request.COOKIES)
        if not isinstance(user, User):
            return JsonResponse({"msg": "userErr_unsignIn"})
        # 筛选出活动时间早于当前的
        now = datetime.now()
        count = Activity.objects.filter(activity_time__gt=now).exclude(
            owner_id=user.id).count()
        start = randint(0, count - 4)
        start = start if start > 0 else 0
        activities = Activity.objects.filter(activity_time__gt=now).exclude(
            owner_id=user.id)[start:start + 4]

        # response 拼接
        response = dict()
        for activity in activities:
            if activity.useractivitymap_set.all().count() > activity.limit_num:
                continue
            activity_name = activity.activity_name
            activity_site = activity.activity_site
            activity_type = activity.activity_type
            activity_desc = activity.activity_desc
            activity_time = activity.activity_time
            owner_id = activity.owner_id
            owner = User.objects.get(id=owner_id)
            owner_nickname = owner.nickname
            response[activity.id] = {
                "activity_name": activity_name,
                "activity_desc": activity_desc,
                "activity_time": activity_time,
                "activity_site": activity_site,
                "activity_type": activity_type,
                "owner_id": owner_id,
                "owner_nickname": owner_nickname
            }

        return JsonResponse({"response": response})
Example #9
0
	def post(self, request):
		try:
			# 请求格式错误处理
			request_msg = json.loads(request.body)
			if not isinstance(request_msg, dict):
				return JsonResponse({"msg": "typeErr_dict"})
		except Exception as e:
			request_msg = {}

		# 用户登录状态校验
		user = checkStatus(request.COOKIES)
		if not isinstance(user, User):
			return JsonResponse({"msg": "userErr_unsignIn"})

		# 未激活用户不能修改
		if not user.is_active:
			return JsonResponse({"msg": "userErr_unActive"})

		# 修改信息
		password = request_msg.get("password", "")
		user.set_password(password)
		user.save()

		return JsonResponse({"msg": "change successfully"})