コード例 #1
0
def save_thumbnail(args):

    user_id = Auth.get_logged_in_user(request)[0]['data']['user_id']
    check_user = User.query.filter_by(id=user_id).first()
    if not check_user:
        response_object = create_response('fail', 'please login first')
        return response_object

    img = args['thumbnail']
    img_type = img.mimetype

    img_name = ''

    if img_type == 'image/jpeg':
        img_name = thumbnail_resize(img)

    if img_name:
        old_image = check_user.thumbnail
        check_user.thumbnail = img_name
        db.session.commit()
        if old_image:
            full_old_path = f"{thumbnail_loc}\\{old_image}"
            if os.path.isfile(full_old_path):
                os.remove(full_old_path)
        response_object = create_response('success', 'your thumbnail updated')
        return response_object

    response_object = create_response(
        'fail', 'please make shoure ur image type is jpg/jpeg')
    return response_object
コード例 #2
0
def save_new_user(data):
    # validate inputs
    if (not valid.validate_exist(data['email'])
            or not valid.validate_length(data['username'], 4)
            or not valid.validate_length(data['password'], 7)):
        response_object = create_response(
            'fail', 'Please Check Your Inputs Meet Our Needs')
        return response_object

    # check if email valid
    if not valid.validate_email(data['email']):
        response_object = create_response('fail', 'Please Enter Valid Email.')
        return response_object

    user = User.query.filter_by(email=data['email']).first()

    if not user:
        new_user = User(public_id=str(uuid.uuid4()),
                        email=data['email'],
                        username=data['username'],
                        password=data['password'],
                        registered_on=datetime.datetime.utcnow())
        save_changes(new_user)
        return generate_token(new_user)
    else:
        response_object = create_response(
            'fail', 'User already exists. Please Log in.')
        return response_object, 409
コード例 #3
0
def create_post(data):
    # check if author token exists
    user = Auth.get_logged_in_user(request)
    if (not valid.validate_length(data['title'], 10) or
            not valid.validate_length(data['body'], 30)):
        response_object = create_response('fail', 'The Minimum Length For Title 10 And body 30')
        return response_object

    post = Post.query.filter_by(title=data['title']).first()
    if not post:
        new_post = Post(
            title=data['title'],
            body=data['body'],
            author=user[0]['data']['user_id'])

        db.session.add(new_post)
        db.session.commit()

        if data['category']:
            for cat in data['category']:
                n_cat = add_category(cat)
                n_cat.categories.append(new_post)

            db.session.commit()
        response_object = create_response('success', 'your post created.')
        return response_object, 200
    else:
        response_object = create_response(
            'fail', 'post already exists. Please Choose Unique Title.')

    return response_object, 400
コード例 #4
0
def add_remove_like(post_id):
    post = get_a_post(post_id)
    user_id = Auth.get_logged_in_user(request)[0]['data']['user_id']

    if post.author == user_id:
        response_object = create_response(
            'fail', 'You Cant Like Your Own Post')
        return response_object

    like = Like.query.filter_by(post_id=post.id, user_id=user_id).first()

    if not like:
        like = Like(post_id=post.id, user_id=user_id)
        db.session.add(like)
    else:
        db.session.delete(like)
    db.session.commit()

    if not like:
        response_object = create_response(
            'fail', 'Somthing Whent Rong When we Trying To Add Your Support For This Post')
    else:
        response_object = create_response('success', 'Every Thing Done')

    return response_object
コード例 #5
0
def remove_post(post_id):
    post = get_a_post(post_id)

    if not post:
        response_object = create_response('fail', 'This Post Not Exist')
        return response_object

    db.session.delete(post)
    db.session.commit()

    response_object = create_response('Success', 'post deleted')
    return response_object
コード例 #6
0
def delete_a_comment(comment_id):
    user = Auth.get_logged_in_user(request)

    check_comment = Comment.query.filter_by(
        id=comment_id, user_id=user[0]['data']['user_id']).first()

    if not check_comment:
        response_object = create_response('fail', 'this comment not exist')
        return response_object

    db.session.delete(check_comment)
    db.session.commit()

    response_object = create_response('success', 'comment deleted')
    return response_object
コード例 #7
0
    def logout_user(data):
        if data:
            auth_token = data.split(" ")[1]
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                # mark the token as blacklisted
                return save_token(token=auth_token)
            else:
                response_object = create_response('fail', resp)
                return response_object, 401
        else:
            response_object = create_response('fail',
                                              'Provide a valid auth token.')

            return response_object, 403
コード例 #8
0
    def login_user(data):
        try:
            # fetch the user data
            user = User.query.filter_by(email=data.get('email')).first()
            if user and user.check_password(data.get('password')):
                auth_token = user.encode_auth_token(user.id)
                if auth_token:
                    response_object = create_response(
                        'success', 'Successfully logged in.',
                        {'Authorization': auth_token.decode()})
                    return response_object, 200
            else:
                response_object = create_response(
                    'fail', 'email or password does not match.')
                return response_object, 401

        except Exception as e:
            response_object = create_response('fail', 'Try again')
            return response_object, 500
コード例 #9
0
def get_post_comment(post_id):
    check_post = Post.query.filter_by(id=post_id).first()

    if not check_post:
        response_object = create_response('fail', 'this post not exist')
        return response_object

    comment = Comment.query.filter_by(post_id=check_post.id, parent_id=0).all()

    return comment
コード例 #10
0
def create_comment(data, post_id):
    # check if author token exists
    user = Auth.get_logged_in_user(request)
    if user[1] != 200:
        response_object = create_response('fail', 'Auth Token Required.')
        return response_object
    else:
        if (not valid.validate_exist(post_id) or
                not valid.validate_length(data.get('body'), 5)):
            response_object = create_response(
                'fail', 'please make sure everything exist')
            return response_object

        # check if post exsit
        check_post = Post.query.filter_by(id=post_id).first()
        if not check_post:
            response_object = create_response('fail', 'This Post Not Exist')
            return response_object

        parent = 0
        user = user[0]['data']['user_id']

        if (valid.validate_exist(data.get('parent_id')) and
                data.get('parent_id') > 0):
            # check if parent have parent
            parent = data.get('parent_id')
            pComment = Comment.query.filter_by(id=parent).first()
            if pComment.parent_id > 0:
                response_object = create_response('fail', 'sorry we cant now')
                return response_object

        comment = Comment(post_id=post_id,
                          user_id=user,
                          body=data.get('body'),
                          parent_id=parent)
        db.session.add(comment)
        db.session.commit()

        response_object = create_response('success',
                                          'your comment added successfully')
        return response_object
コード例 #11
0
def add_category(cat):
    if not valid.validate_length(cat, 2):
        response_object = create_response('fail', 'Category Minimum Length 2.')
        return response_object
    check_cat = Category.query.filter_by(cat=cat).first()
    if check_cat:
        return check_cat
    else:
        new_cat = Category(cat=cat)
        db.session.add(new_cat)
        db.session.commit()
        return new_cat
コード例 #12
0
def generate_token(user):
    try:
        # generate the auth token
        auth_token = user.encode_auth_token(user.id)
        response_object = create_response(
            'success', 'Successfully registered.',
            {'Authorization': auth_token.decode()})
        return response_object, 201
    except Exception as e:
        response_object = {
            'status': 'fail',
            'message': 'Some error occurred. Please try again.'
        }
        return response_object, 401
コード例 #13
0
 def get_logged_in_user(new_request):
     # get the auth token
     auth_token = new_request.headers.get('Authorization')
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             user = User.query.filter_by(id=resp).first()
             data = {
                 'data': {
                     'user_id': user.id,
                     'email': user.email,
                     'admin': user.admin,
                     'registered_on': str(user.registered_on)
                 }
             }
             response_object = create_response('success', 'Welcome Back',
                                               data)
             return response_object, 200
         response_object = {'status': 'fail', 'message': resp}
         return response_object, 401
     else:
         response_object = create_response('fail',
                                           'Provide a valid auth token.')
         return response_object, 401