예제 #1
0
파일: posts.py 프로젝트: Edin93/old_Mynd
def post_topics(username, post_id):
    """ get topics of a post, add one topic to a post """
    user = storage.get_user_by_username(username)
    is_me = current_identity['username'] == username
    if not user:
        return ClientError(404, 'User not found', 'Not Found')
    post = storage.get(Post, post_id)
    if not post:
        return ClientError(404, 'Post not found', 'Not Found')
    if post not in user.posts:
        return ClientError(404, 'Post not found for this user', 'Not Found')
    if request.method == 'GET':
        topic_list = [topic.title for topic in post.topics]
        return jsonify(topic_list)
    if not is_me:
        return ClientError(401, 'Access denied', 'Unauthorized')
    if 'topic_id' in request.get_json():
        topic = storage.get(Topic, request.get_json()['topic_id'])
    elif 'topic_title' in request.get_json():
        topic = storage.get_topic_by_title(request.get_json()['topic_title'])
    if not topic:
        return ClientError(404, 'Topic not found', 'Not Found')
    post.topics.append(topic)
    storage.save()
    return {"status_code": 1, "info": "Topic added"}
예제 #2
0
def users(username):
    user = storage.get_user_by_username(username)
    is_me = current_identity['username'] == username
    if not user:
        return ClientError(404, 'User not found', 'Not Found')
    topics = user.topics
    if request.method == "GET":
        d = {'user_id': user.id, 'username': user.username, "Topics": []}
        for t in topics:
            td = {"id": t.id, "title": t.title, "description": t.description}
            d["Topics"].append(td)
        return jsonify(d)
    else:
        if not is_me:
            return ClientError(401, 'Access denied', 'Unauthorized')
        if 'id' in request.get_json():
            topic = storage.get(Topic, request.get_json()['id'])
            if not topic:
                return ClientError(404, 'Topic not found', 'Not Found')
            if topic in user.topics:
                return ClientError(409, "Topic already followed")
            user.topics.append(topic)
            storage.save()
            return {"status_code": 1, "info": "Topic added"}
        if 'title' in request.get_json():
            all_topics = storage.all(Topic).values()
            for topic in all_topics:
                if topic.title == request.get_json()['title']:
                    if topic in user.topics:
                        return ClientError(409, "Topic already followed")
                    user.topics.append(topic)
                    storage.save()
                    return {"status_code": 1, "info": "Topic added"}
            return ClientError(404, 'Topic not found', 'Not Found')
예제 #3
0
def user(username):
    user = storage.get_user_by_username(username)
    is_me = current_identity['username'] == username
    if not user:
        return ClientError(404, 'User not found', 'Not Found')
    if request.method == 'GET':
        user = user.to_dict()
        to_hide = [
            'birth_date', 'created_at', 'email', 'gender', 'last_login',
            'updated_at'
        ]
        del user['password']
        if current_identity['username'] != username:
            for i in to_hide:
                del user[i]
        return jsonify(user)
    elif request.method == "DELETE":
        if not is_me:
            return ClientError(401, 'Access denied', 'Unauthorized')
        storage.delete(user)
        storage.save()
        return {'status_code': 1, 'info': 'Deleted'}
    else:
        if not is_me:
            return ClientError(401, 'Access denied', 'Unauthorized')
        can_update = ['gender', 'fullname', 'birth_date']
        for k, v in request.get_json().items():
            if k in can_update:
                setattr(user, k, v)
        user.save()
        return {"status_code": 1, "info": "Updated"}
예제 #4
0
def user_topic(username, topic_id):
    user = storage.get_user_by_username(username)
    is_me = current_identity['username'] == username
    if not user:
        return ClientError(404, 'User not found', 'Not Found')
    topic = storage.get(Topic, topic_id)
    if not topic:
        return ClientError(404, 'Topic not found', 'Not Found')

    if topic not in user.topics:
        return ClientError(404, 'Topic not found for this user', 'Not Found')

    if request.method == "GET":
        user_posts = [post for post in user.posts if topic in post.topics]
        return jsonify({
            'record': 'User posts with a specific topic',
            'topic_id': topic.id,
            'topic_title': topic.title,
            'username': user.username,
            'user_id': user.id,
            'posts': user_posts
        })
    elif request.method == "DELETE":
        if not is_me:
            return ClientError(401, 'Access denied', 'Unauthorized')
        user.topics.remove(topic)
        storage.save()
        return {'status_code': 1, 'info': 'Deleted'}
예제 #5
0
파일: security.py 프로젝트: Edin93/old_Mynd
def authenticate(username, password):
    user = storage.get_user_by_username(username)
    if user and safe_str_cmp(user.password.encode('utf-8'),
                             hashlib.md5(password.encode()).hexdigest()):
        log = Log()
        log.user_id = user.id
        log.session_start = datetime.utcnow()
        log.save()
        return user
예제 #6
0
파일: posts.py 프로젝트: Edin93/old_Mynd
def user_post(username, post_id):
    """Get, modify or delete a post."""

    user = storage.get_user_by_username(username)
    is_me = current_identity['username'] == username
    if not user:
        return ClientError(404, 'User not found', 'Not Found')
    post = storage.get(Post, post_id)
    if not post:
        return ClientError(404, 'Post not found', 'Not Found')
    if post not in user.posts:
        return ClientError(404, 'Post not found for this user', 'Not Found')
    if request.method == 'GET':
        post_topics = [topic.title for topic in post.topics]
        return jsonify({
            "id": post.id, "path": post.path,
            "description": post.description,
            "topics": post_topics,
            "likes": len(post.likes),
            "comments": len(post.comments),
            "owner": {
                "username": username,
                "user_id": user.id
            }
        })
    if not is_me:
        return ClientError(401, 'Access denied', 'Unauthorized')
    if request.method == 'DELETE':
        storage.delete(post)
        storage.save()
        return {'status_code': 1, 'info': 'Deleted'}, 200
    elif not request.get_json():
        return ClientError(400, 'Not a JSON', 'Invalid')
    if 'path' in request.get_json():
        setattr(post, 'path', request.get_json()['path'])
    if 'description' in request.get_json():
        setattr(post, 'description', request.get_json()['description'])
    if 'topic_ids' in request.get_json():
        post.topics = []
        for topic_id in request.get_json()['topic_ids']:
            topic = storage.get(Topic, topic_id)
            if topic:
                post.topics.append(topic)
    if 'topic_titles' in request.get_json():
        post.topics = []
        for title in request.get_json()['topic_titles']:
            topic = storage.get_topic_by_title(title)
            if topic:
                post.topics.append(topic)
    storage.save()
    return {"status_code": 1, "info": "Updated"}
예제 #7
0
파일: posts.py 프로젝트: Edin93/old_Mynd
def user_posts(username):
    """Gets user posts, Current user creates a new post."""

    user = storage.get_user_by_username(username)
    is_me = current_identity['username'] == username
    if not user:
        return ClientError(404, 'User not found', 'Not Found')
    if request.method == 'GET':
        posts = user.posts
        d = {"Posts": []}
        for p in posts:
            post_topics = [topic.title for topic in p.topics]
            dp = {"id": p.id, "path": p.path, "description": p.description,
                  "topics": post_topics, "likes": len(p.likes),
                  "comments": len(p.comments)}
            d["Posts"].append(dp)
            d["owner"] = {
                "username": username,
                "user_id": user.id
            }
        return jsonify(d)
    elif request.method == 'POST':
        if not is_me:
            return ClientError(401, 'Access denied', 'Unauthorized')
        if not request.get_json():
            return ClientError(400, 'Not a JSON', 'Invalid')
        if 'path' not in request.get_json():
            return ClientError(400, 'No valid Entry', 'Invalid')
        post = Post()
        post.path = request.get_json()['path']
        if 'description' in request.get_json():
            post.description = request.get_json()['description']
        if 'topic_ids' in request.get_json():
            for topic_id in request.get_json()['topic_ids']:
                topic = storage.get(Topic, topic_id)
                if topic:
                    post.topics.append(topic)
        if 'topic_titles' in request.get_json():
            for title in request.get_json()['topic_titles']:
                topic = storage.get_topic_by_title(title)
                if topic:
                    post.topics.append(topic)
        post.user_id = user.id
        post.save()
        return {"status_code": 1, "info": "Created"}
예제 #8
0
파일: posts.py 프로젝트: Edin93/old_Mynd
def post_topic(username, post_id, topic_id):
    """ check if a topic is related to a post, delete the topic from post topics """
    user = storage.get_user_by_username(username)
    is_me = current_identity['username'] == username
    if not user:
        return ClientError(404, 'User not found', 'Not Found')
    post = storage.get(Post, post_id)
    if not post:
        return ClientError(404, 'Post not found', 'Not Found')
    if post not in user.posts:
        return ClientError(404, 'Post not found for this user', 'Not Found')
    topic = storage.get(Topic, topic_id)
    if not topic:
        return ClientError(404, 'Topic not found', 'Not Found')
    if topic not in post.topics:
        return ClientError(404, 'Topic not found for this post', 'Not Found')
    if request.method == 'GET':
        return {"status_code": 1, "info": "Post contains this topic"}
    if not is_me:
        return ClientError(401, 'Access denied', 'Unauthorized')
    post.topics.remove(topic)
    storage.save()
    return {"status_code": 1, "info": "Topic deleted from this post"}