Coverage for src/couchers/servicers/reporting.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-22 17:19 +0000

1import grpc 

2from google.protobuf import empty_pb2 

3 

4from couchers import errors 

5from couchers.db import session_scope 

6from couchers.models import ContentReport, User 

7from couchers.sql import couchers_select as select 

8from couchers.tasks import send_content_report_email 

9from proto import reporting_pb2_grpc 

10 

11 

12class Reporting(reporting_pb2_grpc.ReportingServicer): 

13 def Report(self, request, context): 

14 with session_scope() as session: 

15 # note no filtering on visibility 

16 author_user = session.execute(select(User).where_username_or_id(request.author_user)).scalar_one_or_none() 

17 

18 if not author_user: 

19 context.abort(grpc.StatusCode.NOT_FOUND, errors.USER_NOT_FOUND) 

20 

21 content_report = ContentReport( 

22 reporting_user_id=context.user_id, 

23 reason=request.reason, 

24 description=request.description, 

25 content_ref=request.content_ref, 

26 author_user=author_user, 

27 user_agent=request.user_agent, 

28 page=request.page, 

29 ) 

30 

31 session.add(content_report) 

32 session.flush() 

33 

34 send_content_report_email(content_report) 

35 

36 return empty_pb2.Empty()