• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Brunowar12 / TaskManagerSystem / 15166388190

21 May 2025 03:32PM UTC coverage: 92.47% (-0.3%) from 92.77%
15166388190

push

github

web-flow
Merge pull request #27 from Brunowar12/bugfix-and-pep

Bug fixes and code style cleanup

159 of 183 new or added lines in 24 files covered. (86.89%)

10 existing lines in 6 files now uncovered.

1621 of 1753 relevant lines covered (92.47%)

5.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.16
/projects/services.py
1
from datetime import timedelta
6✔
2
from django.core.exceptions import ValidationError
6✔
3
from django.db import transaction
6✔
4
from django.shortcuts import get_object_or_404
6✔
5
from django.utils import timezone
6✔
6
from rest_framework.exceptions import PermissionDenied
6✔
7

8
from .models import Project, ProjectMembership, ProjectShareLink, Role
6✔
9

10

11
class ProjectService:
6✔
12
    """
13
    Service for working with projects:
14
    - creating a project with automatic assignment of the Admin role
15
    - receiving a project with access verification
16
    """
17
    @staticmethod
6✔
18
    @transaction.atomic
6✔
19
    def create_project(owner, **data):
6✔
20
        """Creates a new project and adds an owner with the Admin role"""
21
        project = Project.objects.create(owner=owner, **data)
6✔
22
        admin_role = Role.objects.get(name="Admin")
6✔
23
        
24
        ProjectMembership.objects.get_or_create(
6✔
25
            user=owner,
26
            project=project,
27
            defaults={"role": admin_role}
28
        )
29
        return project
6✔
30
    
31
    @staticmethod
6✔
32
    def get_project_or_404(pk, user):
6✔
33
        """
34
        Returns the project by pk if the user is the owner 
35
        or member; otherwise calls PermissionDenied
36
        """
37
        project = get_object_or_404(
6✔
38
            Project.objects.prefetch_related("memberships__role"), pk=pk
39
        )
40
        
41
        is_member = project.memberships.filter(user=user).exists()
6✔
42
        if project.owner != user and not is_member:
6✔
UNCOV
43
            raise PermissionError("You do not have acces to this project")
×
44
        
45
        return project
6✔
46
    
47
class ProjectShareLinkService:
6✔
48
    """
49
    Service for validation and creation of links to join the project
50
    """
51
    @staticmethod
6✔
52
    def validate_share_link(link: ProjectShareLink):
6✔
53
        """Checks whether the link is valid, not expired, and not expired"""
54
        if not link.is_valid():
6✔
55
            if link.is_expired():
6✔
56
                raise PermissionDenied("Link has expired")
6✔
57
            if link.is_usage_exceeded():
6✔
58
                raise PermissionDenied("Link usage limit exceeded")
6✔
59
            raise PermissionDenied("Link is inactive")
×
60
        
61
    @staticmethod
6✔
62
    def create_share_link(project: Project, role_id: int, user, max_uses: int | None, expires_in: int):
6✔
63
        """
64
        Creates a new active link with the specified parameters:
65
        - role, lifetime in minutes, maximum number of uses
66
        """
67
        role = get_object_or_404(Role, id=role_id)
6✔
68
        expires_at = timezone.now() + timedelta(minutes=expires_in)
6✔
69

70
        share_link = ProjectShareLink.objects.create(
6✔
71
            project=project,
72
            role=role,
73
            max_uses=max_uses,
74
            expires_at=expires_at,
75
            created_by=user,
76
        )
77
        return share_link
6✔
78

79

80
class ProjectMembershipService:
6✔
81
    """
82
    Service for working with project membership:
83
    - assigning and changing roles
84
    """
85
    @staticmethod
6✔
86
    @transaction.atomic
6✔
87
    def assign_role(project, user, role):
6✔
88
        """
89
        Assigns or updates a user role in the project.
90
        Throws a ValidationError if:
91
        - the user is the project owner
92
        - the role has not changed
93
        """
94
        if user == project.owner:
6✔
95
            raise ValidationError("Cannot assign role to the project owner")
×
96
        
97
        membership = (
6✔
98
            ProjectMembership.objects
99
            .select_for_update()
100
            .filter(user=user, project=project)
101
            .first()
102
        )
103

104
        if membership:
6✔
105
            if membership.role == role:
6✔
106
                raise ValidationError("User already has this role in project")
6✔
107

108
            membership.role = role
6✔
109
            membership.save(update_fields=['role'])
6✔
110
        else:
NEW
111
            membership = ProjectMembership.objects.create(
×
112
                user=user, project=project, role=role
113
            )
114
        
115
        return membership
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc