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

gcivil-nyu-org / INT2-Monday-Spring2024-Team-1 / 904

24 Apr 2024 04:23AM UTC coverage: 91.473% (-0.03%) from 91.507%
904

Pull #247

travis-pro

web-flow
Merge 4703ee4a3 into 8c448b77f
Pull Request #247: Fix issue #203

3 of 4 new or added lines in 1 file covered. (75.0%)

1770 of 1935 relevant lines covered (91.47%)

0.91 hits per line

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

98.57
/healthScore/homepage_and_auth.py
1
from django.shortcuts import render, redirect
1✔
2
from django.contrib.auth import authenticate, login
1✔
3
from django.contrib.auth.decorators import login_required
1✔
4
from .user_utils import get_health_history_details
1✔
5
from .models import (
1✔
6
    Post,
7
    HealthHistoryAccessRequest,
8
)
9

10

11
# To overcame issues with regards to permissions (POST calls will give CSRF errors if the below tag is not used)
12
from django.views.decorators.csrf import csrf_exempt
1✔
13

14
from .models import (
1✔
15
    Hospital,
16
    User,
17
    HospitalStaff,
18
)
19

20
from .file_upload import file_upload
1✔
21
from django.views.decorators.cache import never_cache
1✔
22

23

24
def homepage(request):
1✔
25
    return render(request, "homepage.html")
1✔
26

27

28
@csrf_exempt
1✔
29
def registration(request):
1✔
30
    if request.method == "POST":
1✔
31
        role = request.POST.get("role")
1✔
32
        email = request.POST.get("email")
1✔
33
        password = request.POST.get("password")
1✔
34
        fullname = request.POST.get("fullname")
1✔
35
        phone_number = request.POST.get("contactInfo")
1✔
36
        context = {"error_message:": ""}
1✔
37

38
        if User.objects.filter(email=email).exists():
1✔
39
            user = User.objects.get(email=email)
1✔
40
            if user.is_patient:
1✔
41
                context["error_message"] = (
1✔
42
                    "A patient account already exists with this email"
43
                )
44
            elif user.is_staff:
1✔
45
                context["error_message"] = (
1✔
46
                    "An admin account already exists with this email"
47
                )
48
            else:
49
                context["error_message"] = (
1✔
50
                    "A healthcare worker account already exists with this email"
51
                )
52

53
            return render(request, "registration.html", context)
1✔
54

55
        common_fields = {
1✔
56
            "email": email,
57
            "password": password,
58
            "name": fullname,
59
            "contactInfo": phone_number,
60
        }
61

62
        if role == "User":
1✔
63
            file_url = file_upload(request, "identityProof")
1✔
64
            user_specific_fields = {
1✔
65
                "dob": request.POST.get("dob"),
66
                "gender": request.POST.get("gender"),
67
                "address": f"{request.POST.get('street_address')}, {request.POST.get('city')}, {request.POST.get('state')}, {request.POST.get('zipcode')}",
68
                "proofOfIdentity": file_url,  # This needs handling for file upload
69
            }
70
            User.objects.create_patient(**common_fields, **user_specific_fields)
1✔
71

72
        elif role == "Healthcare Admin":
1✔
73
            hospital_name = request.POST.get("hospital_name")
1✔
74
            hospital_address = f"{request.POST.get('facility_street_address')}, {request.POST.get('facility_city')}, {request.POST.get('facility_state')}, {request.POST.get('facility_zipcode')}"
1✔
75

76
            user = User.objects.create_staff(**common_fields)
1✔
77

78
            hospital, created = Hospital.objects.get_or_create(
1✔
79
                name=hospital_name,
80
                defaults={"address": hospital_address, "contactInfo": phone_number},
81
            )
82

83
            HospitalStaff.objects.create(
1✔
84
                hospitalID=hospital,
85
                admin=True,
86
                name=fullname,
87
                contactInfo=phone_number,
88
                userID=user.id,
89
            )
90

91
        return redirect("homepage")
1✔
92

93
    return render(request, "registration.html")
1✔
94

95

96
@never_cache
1✔
97
def login_view(request):
1✔
98
    if request.user.is_authenticated:
1✔
NEW
99
        return redirect("user_dashboard")
×
100

101
    if request.method == "POST":
1✔
102
        email = request.POST.get("email")
1✔
103
        password = request.POST.get("password")
1✔
104

105
        user = authenticate(request, email=email, password=password, is_active=True)
1✔
106

107
        if user is not None:
1✔
108
            login(request, user)
1✔
109
            return redirect("user_dashboard")
1✔
110
        else:
111
            return render(
1✔
112
                request,
113
                "login.html",
114
                {"error_message": "Invalid email or password. Please try again."},
115
            )
116
    return render(request, "login.html")
1✔
117

118

119
@login_required(login_url="/")
1✔
120
def user_dashboard(request):
1✔
121
    if not request.user.is_patient:
1✔
122
        return redirect("homepage")
1✔
123

124
    posts = Post.objects.filter(user=request.user).order_by("-createdAt")[:5]
1✔
125

126
    updated_params = request.GET.copy()
1✔
127
    updated_params["record_status"] = "approved"
1✔
128

129
    request.GET = updated_params
1✔
130

131
    zipped_details = get_health_history_details(request=request)
1✔
132

133
    filtered_details = [
1✔
134
        details
135
        for details in zipped_details
136
        if details[0]["record_status"] == "approved"
137
    ]
138
    sorted_details = sorted(
1✔
139
        filtered_details, key=lambda x: x[0]["createdAt"], reverse=True
140
    )[:5]
141

142
    all_access_requests = HealthHistoryAccessRequest.objects.filter(
1✔
143
        userID=request.user
144
    ).order_by("-createdAt")
145

146
    total_requests = all_access_requests.count()
1✔
147

148
    recent_requests = all_access_requests[:5]
1✔
149

150
    context = {
1✔
151
        "posts": posts,
152
        "zipped_details": sorted_details,
153
        "access_requests": recent_requests,
154
        "total_requests": total_requests,
155
    }
156
    return render(request, "user_dashboard.html", context)
1✔
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