Ejemplo n.º 1
0
def send_password_verify_code(request):
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    # 用户 user 存在性验证
    username = json_data['username']
    user = account_models.User.objects.filter(
        Q(username=username) | Q(userinfo__email=username)).first()
    if not user:
        return process_response(request,
                                ResponseStatus.USERNAME_NOT_EXISTED_ERROR)

    # 生成随机数字验证码
    verify_code = generate_string(5, Pattern.Digits)

    # 填充邮件内容
    message = settings.VERIFY_CODE_MAIL_MESSAGE.format(code=verify_code,
                                                       username=username)

    email = user.info.email
    send(email, message)

    # 将验证码存入缓存 10 min 过期
    cache = get_redis_connection()
    cache.set('verify_code_' + email, verify_code, 10 * settings.MINUTE)

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 2
0
def login(request):
    # 经过处理的 JSON 数据
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    username = json_data['username']
    password = json_data['password']

    if request.session.get('username', '') == username:
        return process_response(request, ResponseStatus.OK)

    # 用户 user 存在性验证
    user = account_models.User.objects.filter(
        Q(username=username) | Q(userinfo__email=username)).first()
    if not user:
        return process_response(request,
                                ResponseStatus.USERNAME_NOT_EXISTED_ERROR)

    # 密码 password 验证
    if check_password(password, user.password) is False:
        return process_response(request,
                                ResponseStatus.PASSWORD_NOT_MATCH_ERROR)

    # 设置登陆状态
    request.session['username'] = user.username

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 3
0
def change_password(request):
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    # 新密码 new_password 格式验证
    new_password = json_data['new_password']
    status = validate_password(new_password)
    if status is not None:
        return process_response(request, status)

    # 用户 user 存在性验证
    username = json_data['username']
    user = account_models.User.objects.filter(
        Q(username=username) | Q(userinfo__email=username)).first()
    if not user:
        return process_response(request,
                                ResponseStatus.USERNAME_NOT_EXISTED_ERROR)

    # 密码 password 验证
    password = json_data['password']
    if check_password(password, user.password) is False:
        return process_response(request,
                                ResponseStatus.PASSWORD_NOT_MATCH_ERROR)

    # 修改密码 password
    user.password = make_password(new_password)
    user.save()

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 4
0
def get_my_gallery(request):
    num = request.GET.get('num')
    if not num:
        num = 1
    else:
        status = ValueErrorStatus.check_value_type({'num': num})
        if status is not None:
            return process_response(request, status)
        num = int(num)

    user = account_models.User.objects.filter(
        username=request.session.get('username')).first()
    photos = gallery_models.Photo.objects.filter(user=user).order_by('-id')

    paginator = Paginator(photos, settings.PHOTOS_PER_PAGE)
    total = paginator.num_pages

    if not 1 <= num <= total:
        return process_response(request, ResponseStatus.NUM_OUT_OF_RANGE_ERROR)

    page = paginator.page(num)

    photos_path = []
    for one in page.object_list:
        photos_path.append(one.path)

    request.data = {
        'photo': photos_path,
        'count': len(photos_path),
        'num': num,
        'has_next': page.has_next()
    }

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 5
0
def get_all_post(request):
    num = request.GET.get('num')
    if not num:
        num = 1
    else:
        status = ValueErrorStatus.check_value_type({'num': num})
        if status is not None:
            return process_response(request, status)
        num = int(num)

    posts = post_models.Post.objects.all().order_by('-id')

    paginator = Paginator(posts, settings.POST_PER_PAGE)
    total = paginator.num_pages

    if not 1 <= num <= total:
        return process_response(request, ResponseStatus.NUM_OUT_OF_RANGE_ERROR)

    page = paginator.page(num)

    user = request.session.get('username', None)
    if user is not None:
        user = account_models.User.objects.filter(username=user).first()

    post_list = []
    for one in page.object_list:
        post_list.append({
            'username':
            one.user.info.nickname
            if one.user.info.nickname else one.user.username,
            'avatar':
            one.user.info.avatar.url,
            'content':
            one.content,
            'path':
            one.photo.path,
            'create_time':
            one.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'address':
            one.photo.address,
            'like':
            one.like,
            'post_id':
            one.id
        })

        if user:
            post_list[-1][
                'is_liked'] = True if post_models.LikeRecord.objects.filter(
                    user=user, post=one) else False

    request.data = {
        'post': post_list,
        'count': len(post_list),
        'num': num,
        'has_next': page.has_next()
    }

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 6
0
def change_nickname(request):
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    nickname = json_data['nickname']
    if len(nickname) > 50:
        return process_response(request,
                                ResponseStatus.NICKNAME_LENGTH_TOO_LARGE_ERROR)

    user_info = account_models.User.objects.filter(
        username=request.session.get('username')).first().info
    user_info.nickname = nickname
    user_info.save()

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 7
0
def change_avatar(request):
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    avatar = json_data['avatar']
    if len(avatar) > 100 or re.search(r'\.\.', avatar) or avatar[:8] != '/' + settings.AVATAR_PATH \
            or not os.path.exists('.' + avatar):
        return process_response(request,
                                ResponseStatus.IMAGE_PATH_NOT_FOUND_ERROR)

    user_info = account_models.User.objects.filter(
        username=request.session.get('username')).first().info
    user_info.avatar = avatar
    user_info.save()

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 8
0
def save_in_gallery(request):
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    path = json_data['path']
    if len(path) > 100 or re.search(r'\.\.', path) or path[:9] != '/' + settings.PICTURE_PATH \
            or not os.path.exists('.' + path):
        return process_response(request,
                                ResponseStatus.IMAGE_PATH_NOT_FOUND_ERROR)

    longitude = json_data['longitude']
    latitude = json_data['latitude']
    address = ''
    if longitude != 0.0 and latitude != 0.0:
        try:
            result = requests.get(
                settings.BAIDU_ADDRESS_API_URL.format(longitude=longitude,
                                                      latitude=latitude))
            if result.status_code == 200:
                address = json.loads(
                    result.text)['result']['formatted_address']
        except Exception:
            address = ''

    user = account_models.User.objects.filter(
        username=request.session.get('username')).first()
    report = prediction_models.Report.objects.filter(path=path).first()

    if gallery_models.Photo.objects.filter(user=user, path=path):
        return process_response(request, ResponseStatus.PHOTO_EXISTED_ERROR)

    photo = gallery_models.Photo(user=user,
                                 path=path,
                                 report=report,
                                 address=address,
                                 latitude=latitude,
                                 longitude=longitude)
    photo.save()

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 9
0
def register(request):
    # 经过处理的 JSON 数据
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    # 用户名 username 格式检验
    username = json_data['username']
    status = validate_username(username)
    if status is not None:
        return process_response(request, status)

    # 密码 password 格式验证
    password = json_data['password']
    status = validate_password(password)
    if status is not None:
        return process_response(request, status)

    # 邮箱 email 格式验证
    email = json_data['email']
    status = validate_email(email)
    if status is not None:
        return process_response(request, status)

    # 用户名 username 存在性验证
    if account_models.User.objects.filter(username=username):
        return process_response(request, ResponseStatus.USERNAME_EXISTED_ERROR)

    # 邮箱 email 存在性验证
    if account_models.UserInfo.objects.filter(email=email):
        return process_response(request, ResponseStatus.EMAIL_EXISTED_ERROR)

    # 创建用户 user 和 用户信息 user_info
    user = account_models.User(username=username,
                               password=make_password(password))
    user.save()
    user_info = account_models.UserInfo(user=user, email=email)
    user_info.save()

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 10
0
def like_post(request):
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    post_id = json_data['post_id']
    post = post_models.Post.objects.filter(id=post_id).first()
    if not post:
        return process_response(request, ResponseStatus.POST_NOT_FOUND_ERROR)

    user = account_models.User.objects.filter(
        username=request.session.get('username')).first()
    if post_models.LikeRecord.objects.filter(user=user, post=post):
        return process_response(request, ResponseStatus.LIKE_ALREADY_ERROR)

    post.like += 1
    post.save()

    like = post_models.LikeRecord(user=user, post=post)
    like.save()

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 11
0
def give_post(request):
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    content = json_data['content']
    if len(content) > 400:
        return process_response(request,
                                ResponseStatus.CONTENT_LENGTH_TOO_LARGE_ERROR)

    user = account_models.User.objects.filter(
        username=request.session.get('username')).first()
    path = json_data['path']
    photo = gallery_models.Photo.objects.filter(user=user, path=path).first()
    if not photo:
        return process_response(request,
                                ResponseStatus.IMAGE_PATH_NOT_FOUND_ERROR)

    post = post_models.Post(user=user, photo=photo, content=content)
    post.save()

    return process_response(request, ResponseStatus.OK)
Ejemplo n.º 12
0
def change_forget_password(request):
    json_data = request.json_data

    status = ValueErrorStatus.check_value_type(json_data)
    if status is not None:
        return process_response(request, status)

    # 用户 user 存在性验证
    username = json_data['username']
    user = account_models.User.objects.filter(
        Q(username=username) | Q(userinfo__email=username)).first()
    if not user:
        return process_response(request,
                                ResponseStatus.USERNAME_NOT_EXISTED_ERROR)

    # 新密码 new_password 格式验证
    new_password = json_data['new_password']
    status = validate_password(new_password)
    if status is not None:
        return process_response(request, status)

    # 验证码匹配
    verify_code = json_data['verify_code']

    cache = get_redis_connection()
    cached_code = cache.get('verify_code_' + user.info.email)
    if verify_code != cached_code:
        return process_response(request,
                                ResponseStatus.VERIFY_CODE_NOT_MATCH_ERROR)
    cache.delete('verify_code_' + user.info.email)

    # 修改密码 password
    user.password = make_password(new_password)
    user.save()

    return process_response(request, ResponseStatus.OK)