|
1 | 1 | import json |
2 | 2 | from app.models.project_users import ProjectUser |
| 3 | +from app.schemas.user import UserSchema |
3 | 4 | from flask_restful import Resource, request |
4 | | -from app.schemas import ProjectUserSchema, AnonymousUsersSchema, UserIndexSchema |
| 5 | +from app.schemas import ProjectUserSchema, AnonymousUsersSchema |
5 | 6 | from app.models.user import User |
6 | 7 | from app.models.role import User |
7 | 8 | from app.models.project import Project |
|
14 | 15 | from app.helpers.activity_logger import log_activity |
15 | 16 | from datetime import date |
16 | 17 | from app.models.anonymous_users import AnonymousUser |
17 | | - |
| 18 | +from app.models import db |
18 | 19 |
|
19 | 20 | class ProjectUsersView(Resource): |
20 | 21 |
|
@@ -547,20 +548,81 @@ def post(self, project_id): |
547 | 548 |
|
548 | 549 | @ jwt_required |
549 | 550 | def get(self, project_id): |
| 551 | + page = request.args.get('page', 1, type=int) |
| 552 | + per_page = request.args.get('per_page', 10, type=int) |
| 553 | + |
| 554 | + if per_page > 100: |
| 555 | + per_page = 100 |
| 556 | + |
| 557 | + if page < 1: |
| 558 | + page = 1 |
| 559 | + |
550 | 560 | project = Project.get_by_id(project_id) |
551 | | - follower_schema = UserIndexSchema(many=True) |
552 | | - |
553 | | - followers = project.followers |
554 | | - users_data, errors = follower_schema.dumps(followers) |
555 | | - |
556 | | - if errors: |
557 | | - return dict(status='fail', message=errors), 400 |
558 | | - |
559 | | - return dict( |
560 | | - status='success', |
561 | | - data=dict(followers=json.loads(users_data)) |
562 | | - ), 200 |
563 | | - |
| 561 | + |
| 562 | + if not project: |
| 563 | + return dict(status='fail', message=f'Project with id {project_id} not found'), 404 |
| 564 | + |
| 565 | + try: |
| 566 | + query = db.session.query(User).join( |
| 567 | + ProjectFollowers, User.id == ProjectFollowers.user_id |
| 568 | + ).filter( |
| 569 | + ProjectFollowers.project_id == project_id |
| 570 | + ).order_by(ProjectFollowers.id.desc()) |
| 571 | + |
| 572 | + total_followers_count = query.count() |
| 573 | + |
| 574 | + paginated_result = query.paginate( |
| 575 | + page=page, |
| 576 | + per_page=per_page, |
| 577 | + error_out=False |
| 578 | + ) |
| 579 | + |
| 580 | + follower_schema = UserSchema(many=True) |
| 581 | + schema_result = follower_schema.dump(paginated_result.items) |
| 582 | + |
| 583 | + if hasattr(schema_result, 'data'): |
| 584 | + followers_data = schema_result.data |
| 585 | + else: |
| 586 | + followers_data = schema_result |
| 587 | + |
| 588 | + if followers_data is None: |
| 589 | + followers_data = [] |
| 590 | + elif not isinstance(followers_data, list): |
| 591 | + try: |
| 592 | + followers_data = list(followers_data) |
| 593 | + except: |
| 594 | + followers_data = [] |
| 595 | + |
| 596 | + pagination = { |
| 597 | + 'total': paginated_result.total, |
| 598 | + 'pages': paginated_result.pages, |
| 599 | + 'page': paginated_result.page, |
| 600 | + 'per_page': paginated_result.per_page, |
| 601 | + 'next': paginated_result.next_num, |
| 602 | + 'prev': paginated_result.prev_num, |
| 603 | + 'has_next': paginated_result.has_next, |
| 604 | + 'has_prev': paginated_result.has_prev |
| 605 | + } |
| 606 | + |
| 607 | + project_info = dict( |
| 608 | + id=str(project.id), |
| 609 | + name=project.name, |
| 610 | + alias=project.alias, |
| 611 | + followers_count=total_followers_count |
| 612 | + ) |
| 613 | + |
| 614 | + return dict( |
| 615 | + status='success', |
| 616 | + data=dict( |
| 617 | + project=project_info, |
| 618 | + followers=followers_data, |
| 619 | + pagination=pagination |
| 620 | + ) |
| 621 | + ), 200 |
| 622 | + |
| 623 | + except Exception as e: |
| 624 | + return dict(status='fail', message='Internal Server Error'), 500 |
| 625 | + |
564 | 626 | @ jwt_required |
565 | 627 | def delete(self, project_id): |
566 | 628 | current_user_id = get_jwt_identity() |
|
0 commit comments