-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
327 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
from .appreciations import can_create_appreciations, can_view_appreciations, can_view_appreciation | ||
from .one_on_one import can_view_one_on_one, can_delete_one_on_one | ||
|
||
__all__ = ['can_view_one_on_one', 'can_delete_one_on_one'] | ||
__all__ = [ | ||
'can_view_one_on_one', | ||
'can_delete_one_on_one', | ||
'can_view_appreciations', | ||
'can_view_appreciation', | ||
'can_create_appreciations' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from ..models import Appreciation | ||
|
||
|
||
def can_view_appreciations(): | ||
return True | ||
|
||
|
||
def can_create_appreciations(): | ||
return True | ||
|
||
|
||
def can_view_appreciation(_: Appreciation): | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from . import appreciations | ||
from . import healthcheck | ||
from . import notifications | ||
from . import one_on_ones | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import re | ||
from datetime import datetime | ||
|
||
from flask import Blueprint, request | ||
from flask_login import login_required, current_user | ||
|
||
from .response import bad_request, not_found, unauthorized | ||
from ... import acl | ||
from ...models import Appreciation, Like, User, Mention | ||
from ...schemas.appreciation import AppreciationSchema, CreateAppreciationSchema | ||
from ...services import notification | ||
|
||
blueprint = Blueprint('api.appreciations', __name__) | ||
|
||
appreciation_schema = AppreciationSchema() | ||
create_appreciation_schema = CreateAppreciationSchema() | ||
|
||
|
||
def get_appreciation_viewer_like(appreciation: Appreciation): | ||
return Like.get_by_appreciation_and_user(appreciation, current_user) | ||
|
||
|
||
def appreciation_view(appreciation: Appreciation): | ||
return { | ||
'id': appreciation.id, | ||
'content': appreciation.content, | ||
'created_at': appreciation.created_at, | ||
'created_by': appreciation.created_by, | ||
'like_count': appreciation.like_count, | ||
'comment_count': appreciation.comment_count, | ||
'mentions': appreciation.mentions, | ||
|
||
'viewer_like': get_appreciation_viewer_like(appreciation), | ||
} | ||
|
||
|
||
@login_required | ||
@blueprint.route('', methods=['GET']) | ||
def list_all_appreciations(): | ||
if not acl.can_view_appreciations(): | ||
return unauthorized() | ||
|
||
formatted_appreciations = [appreciation_view(a) for a in Appreciation.get_all()] | ||
return appreciation_schema.jsonify(formatted_appreciations, many=True) | ||
|
||
|
||
@login_required | ||
@blueprint.route('', methods=['PUT']) | ||
def create_appreciation(): | ||
if not request.is_json: | ||
return bad_request() | ||
|
||
if not acl.can_create_appreciations(): | ||
return unauthorized() | ||
|
||
appreciation: Appreciation = create_appreciation_schema.load(request.json) | ||
|
||
appreciation.created_by = current_user | ||
appreciation.created_at = datetime.now() | ||
|
||
Appreciation.create(appreciation) | ||
|
||
mentions = re.findall(r'@[a-zA-Z0-9._]+', appreciation.content) | ||
|
||
for mention_text in mentions: | ||
user = User.get_by_username(mention_text[1:]) | ||
if user is None: | ||
continue | ||
mention = Mention(user=user, appreciation=appreciation) | ||
Mention.create(mention) | ||
|
||
notification.notify_appreciation(appreciation) | ||
|
||
return appreciation_schema.jsonify(appreciation) | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<_id>', methods=['GET']) | ||
def get_appreciation(_id): | ||
appreciation = Appreciation.get(_id) | ||
|
||
if not appreciation: | ||
return not_found() | ||
|
||
if not acl.can_view_appreciation(appreciation): | ||
return unauthorized() | ||
|
||
return appreciation_schema.jsonify(appreciation) | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<_id>', methods=['PATCH']) | ||
def update_appreciation(_id): | ||
return not_found() | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<_id>', methods=['DELETE']) | ||
def delete_appreciation(_id): | ||
pass | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<appreciation_id>/likes', methods=['GET']) | ||
def get_appreciation_likes(appreciation_id): # pylint: disable=unused-argument | ||
pass | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<appreciation_id>/likes', methods=['PUT']) | ||
def like(appreciation_id): # pylint: disable=unused-argument | ||
pass | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<appreciation_id>/likes/<like_id>', methods=['DELETE']) | ||
def delete_like(appreciation_id, like_id): # pylint: disable=unused-argument | ||
pass | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<appreciation_id>/comments', methods=['GET']) | ||
def get_comments(appreciation_id): # pylint: disable=unused-argument | ||
pass | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<appreciation_id>/comments', methods=['GET']) | ||
def create_comment(appreciation_id): # pylint: disable=unused-argument | ||
pass | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<appreciation_id>/comments/<comment_id>', methods=['GET']) | ||
def update_comment(appreciation_id, comment_id): # pylint: disable=unused-argument | ||
pass | ||
|
||
|
||
@login_required | ||
@blueprint.route('/<appreciation_id>/comments/<comment_id>', methods=['GET']) | ||
def delete_comment(appreciation_id, comment_id): # pylint: disable=unused-argument | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from marshmallow.fields import Integer, String, DateTime, List | ||
from marshmallow_sqlalchemy.fields import Nested | ||
|
||
from .base import BaseSQLAlchemySchema, BaseSchema | ||
from ..marshmallow import marshmallow | ||
from ..models import Like | ||
from ..schemas.user import UserSchema | ||
|
||
|
||
class LikeSchema(BaseSQLAlchemySchema): | ||
class Meta: | ||
model = Like | ||
|
||
id = marshmallow.auto_field() | ||
created_by = Nested(UserSchema) | ||
|
||
|
||
class MentionSchema(BaseSchema): | ||
user = Nested(UserSchema) | ||
|
||
|
||
class AppreciationSchema(BaseSchema): | ||
id = Integer() | ||
content = String() | ||
created_at = DateTime() | ||
|
||
created_by = Nested(UserSchema) | ||
|
||
like_count = Integer() | ||
comment_count = Integer() | ||
viewer_like = Nested(LikeSchema, exclude=("user",)) | ||
|
||
mentions = List(Nested(MentionSchema)) | ||
|
||
|
||
class CreateAppreciationSchema(BaseSchema): | ||
class Meta: | ||
model = True | ||
load_instance = True | ||
|
||
content = String() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.