Skip to content

Commit

Permalink
API & Client - minor refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
SamR1 committed Oct 2, 2024
1 parent 53f5cbb commit b04f246
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 251 deletions.
8 changes: 4 additions & 4 deletions fittrackee/reports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,12 @@ class ReportAction(BaseModel):
comment = db.relationship(
'Comment',
lazy=True,
backref=db.backref('comment_admin_action', lazy='select'),
backref=db.backref('comment_report_action', lazy='select'),
)
workout = db.relationship(
'Workout',
lazy=True,
backref=db.backref('workout_admin_action', lazy='select'),
backref=db.backref('workout_report_action', lazy='select'),
)

def __init__(
Expand Down Expand Up @@ -594,7 +594,7 @@ def serialize(self, current_user: User) -> Dict:


@listens_for(ReportAction, 'after_insert')
def on_admin_action_insert(
def on_report_action_insert(
mapper: Mapper, connection: Connection, new_action: ReportAction
) -> None:
@listens_for(db.Session, 'after_flush', once=True)
Expand All @@ -616,7 +616,7 @@ def receive_after_flush(session: Session, context: Connection) -> None:


@listens_for(ReportActionAppeal, 'after_insert')
def on_admin_action_appeal_insert(
def on_report_action_appeal_insert(
mapper: Mapper, connection: Connection, new_appeal: ReportActionAppeal
) -> None:
@listens_for(db.Session, 'after_flush', once=True)
Expand Down
16 changes: 7 additions & 9 deletions fittrackee/reports/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,9 @@ def update_report(
}, 200


@reports_blueprint.route(
"/reports/<int:report_id>/admin-actions", methods=["POST"]
)
@reports_blueprint.route("/reports/<int:report_id>/actions", methods=["POST"])
@require_auth(scopes=["reports:write"], as_admin=True)
def create_admin_action(
def create_action(
auth_user: User, report_id: int
) -> Union[Tuple[Dict, int], HttpResponse]:
data = request.get_json()
Expand All @@ -225,7 +223,7 @@ def create_admin_action(
return NotFoundErrorResponse(f"report not found (id: {report_id})")

try:
action = report_service.create_admin_action(
action = report_service.create_report_action(
report=report,
admin_user=auth_user,
action_type=action_type,
Expand All @@ -235,8 +233,8 @@ def create_admin_action(
db.session.flush()

if current_app.config['CAN_SEND_EMAILS']:
admin_action_email_service = ReportEmailService()
admin_action_email_service.send_admin_action_email(
report_action_email_service = ReportEmailService()
report_action_email_service.send_report_action_email(
report, action_type, reason, action
)

Expand Down Expand Up @@ -286,8 +284,8 @@ def process_appeal(
db.session.flush()

if new_report_action and current_app.config['CAN_SEND_EMAILS']:
admin_action_email_service = ReportEmailService()
admin_action_email_service.send_admin_action_email(
report_action_email_service = ReportEmailService()
report_action_email_service.send_report_action_email(
new_report_action.report,
new_report_action.action_type,
reason,
Expand Down
2 changes: 1 addition & 1 deletion fittrackee/reports/reports_email_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def _send_workout_unsuspension_email(
email_data["without_user_action"] = True
workout_unsuspension_email.send(user_data, email_data)

def send_admin_action_email(
def send_report_action_email(
self,
report: Report,
action_type: str,
Expand Down
6 changes: 3 additions & 3 deletions fittrackee/reports/reports_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def update_report(
return report

@staticmethod
def create_admin_action(
def create_report_action(
*,
report: Report,
admin_user: User,
Expand All @@ -149,12 +149,12 @@ def create_admin_action(
if action_type.startswith("user_warning"):
user = User.query.filter_by(username=username).first()

existing_admin_action = ReportAction.query.filter_by(
existing_report_action = ReportAction.query.filter_by(
action_type=action_type,
report_id=report.id,
user_id=user.id,
).first()
if existing_admin_action:
if existing_report_action:
raise UserWarningExistsException("user already warned")

report_action = ReportAction(
Expand Down
8 changes: 4 additions & 4 deletions fittrackee/tests/comments/test_comments_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3008,7 +3008,7 @@ def test_it_returns_400_if_comment_is_not_suspended(
response, error_message="workout comment is not suspended"
)

def test_it_returns_400_if_suspended_comment_has_no_admin_action(
def test_it_returns_400_if_suspended_comment_has_no_report_action(
self,
app: Flask,
user_1: User,
Expand Down Expand Up @@ -3058,7 +3058,7 @@ def test_it_returns_400_when_appeal_text_is_missing(
text_visibility=PrivacyLevel.PUBLIC,
)
comment.suspended_at = datetime.utcnow()
self.create_admin_comment_action(user_2_admin, user_1, comment)
self.create_report_comment_action(user_2_admin, user_1, comment)
db.session.commit()
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
Expand Down Expand Up @@ -3088,7 +3088,7 @@ def test_user_can_appeal_comment_suspension(
text_visibility=PrivacyLevel.PUBLIC,
)
comment.suspended_at = datetime.utcnow()
action = self.create_admin_comment_action(
action = self.create_report_comment_action(
user_2_admin, user_1, comment
)
db.session.commit()
Expand Down Expand Up @@ -3132,7 +3132,7 @@ def test_user_can_appeal_comment_suspension_only_once(
text_visibility=PrivacyLevel.PUBLIC,
)
comment.suspended_at = datetime.utcnow()
action = self.create_admin_comment_action(
action = self.create_report_comment_action(
user_2_admin, user_1, comment
)
db.session.flush()
Expand Down
12 changes: 6 additions & 6 deletions fittrackee/tests/comments/test_comments_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ def test_suspension_action_is_last_suspension_action_when_comment_is_suspended(
workout_cycling_user_1: Workout,
) -> None:
comment = self.create_comment(user_2, workout_cycling_user_1)
expected_admin_action = self.create_admin_comment_actions(
expected_report_action = self.create_report_comment_actions(
user_1_admin, user_2, comment
)
comment.suspended_at = datetime.utcnow()

assert comment.suspension_action == expected_admin_action
assert comment.suspension_action == expected_report_action

def test_suspension_action_is_none_when_comment_is_unsuspended(
self,
Expand All @@ -152,10 +152,10 @@ def test_suspension_action_is_none_when_comment_is_unsuspended(
workout_cycling_user_1: Workout,
) -> None:
comment = self.create_comment(user_2, workout_cycling_user_1)
self.create_admin_comment_action(
self.create_report_comment_action(
user_1_admin, user_2, comment, "comment_suspension"
)
self.create_admin_comment_action(
self.create_report_comment_action(
user_1_admin, user_2, comment, "comment_unsuspension"
)

Expand Down Expand Up @@ -284,7 +284,7 @@ def test_it_serializes_owner_comment_when_comment_is_suspended(
) -> None:
workout_cycling_user_2.workout_visibility = PrivacyLevel.PRIVATE
comment = self.create_comment(user_1, workout_cycling_user_2)
expected_admin_action = self.create_admin_comment_actions(
expected_report_action = self.create_report_comment_actions(
user_2_admin, user_1, comment
)
comment.suspended_at = datetime.utcnow()
Expand All @@ -302,7 +302,7 @@ def test_it_serializes_owner_comment_when_comment_is_suspended(
'mentions': [],
'suspended': True,
'suspended_at': comment.suspended_at,
'suspension': expected_admin_action.serialize(user_1, full=False),
'suspension': expected_report_action.serialize(user_1, full=False),
'modification_date': comment.modification_date,
'reply_to': comment.reply_to,
'replies': [],
Expand Down
22 changes: 11 additions & 11 deletions fittrackee/tests/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def create_user_report(self, reporter: User, user: User) -> Report:
return self.create_report(reporter=reporter, reported_object=user)

@staticmethod
def create_admin_action(
def create_report_action(
admin_user: User,
user: User,
report_id: int,
Expand Down Expand Up @@ -432,7 +432,7 @@ def create_admin_action(
db.session.commit()
return report_action

def create_admin_user_action(
def create_report_user_action(
self,
admin: User,
user: User,
Expand All @@ -442,7 +442,7 @@ def create_admin_user_action(
report_id = (
report_id if report_id else self.create_user_report(admin, user).id
)
report_action = self.create_admin_action(
report_action = self.create_report_action(
admin, user, action_type=action_type, report_id=report_id
)
user.suspended_at = (
Expand All @@ -451,7 +451,7 @@ def create_admin_user_action(
db.session.commit()
return report_action

def create_admin_workout_action(
def create_report_workout_action(
self,
admin: User,
user: User,
Expand All @@ -470,7 +470,7 @@ def create_admin_workout_action(
db.session.add(report_action)
return report_action

def create_admin_comment_action(
def create_report_comment_action(
self,
admin: User,
user: User,
Expand All @@ -492,18 +492,18 @@ def create_admin_comment_action(
)
return report_action

def create_admin_comment_actions(
def create_report_comment_actions(
self, admin: User, user: User, comment: Comment
) -> ReportAction:
for n in range(2):
action_type = (
"comment_suspension" if n % 2 == 0 else "comment_unsuspension"
)
report_action = self.create_admin_comment_action(
report_action = self.create_report_comment_action(
admin, user, comment, action_type
)
db.session.add(report_action)
report_action = self.create_admin_comment_action(
report_action = self.create_report_comment_action(
admin, user, comment, "comment_suspension"
)
db.session.add(report_action)
Expand All @@ -512,12 +512,12 @@ def create_admin_comment_actions(
def create_action_appeal(
self, action_id: int, user: User, with_commit: bool = True
) -> ReportActionAppeal:
admin_action_appeal = ReportActionAppeal(
report_action_appeal = ReportActionAppeal(
action_id=action_id,
user_id=user.id,
text=self.random_string(),
)
db.session.add(admin_action_appeal)
db.session.add(report_action_appeal)
if with_commit:
db.session.commit()
return admin_action_appeal
return report_action_appeal
Loading

0 comments on commit b04f246

Please sign in to comment.