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

gcivil-nyu-org / wed-fall24-team5 / 628699075

04 Dec 2024 02:58AM UTC coverage: 90.579% (-0.4%) from 90.962%
628699075

push

github

web-flow
refactor image sizes and drive dashboard ui (#381)

* refactor image sizes and drive dashboard ui

* fix js data reload on modify and delete contribution

1 of 11 new or added lines in 1 file covered. (9.09%)

6 existing lines in 2 files now uncovered.

2798 of 3089 relevant lines covered (90.58%)

0.91 hits per line

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

55.45
/src/community_drives/views.py
1
from django.shortcuts import render, redirect, get_object_or_404
1✔
2
from django.contrib.auth.decorators import login_required
1✔
3
from django.contrib import messages
1✔
4
from django.db.models import Sum, Q
1✔
5
from django.db.models.functions import Coalesce
1✔
6
from database.models import CommunityDrive, DriveOrganization, User, Organization
1✔
7
from .forms import AddCommunityDriveForm
1✔
8
from django.http import JsonResponse
1✔
9
import json
1✔
10
import base64
1✔
11

12

13
@login_required
1✔
14
def get_drive_list(request):
1✔
15
    user = User.objects.get(email=request.user.email)
×
16
    organizations = Organization.objects.filter(organizationadmin__user=user)
×
17

18
    if request.method == "POST":
×
19
        form = AddCommunityDriveForm(request.POST, user=user)
×
20
        if form.is_valid():
×
21
            form.save()
×
22
            messages.success(request, "Community drive successfully created.")
×
23
            return redirect("/community_drives")
×
24
    else:
25
        form = AddCommunityDriveForm(user=user)
×
26

27
    drives = (
×
28
        CommunityDrive.objects.filter(active=True)
29
        .annotate(
30
            meal_progress=Coalesce(Sum("driveorganization__meal_pledge"), 0),
31
            volunteer_progress=Coalesce(Sum("driveorganization__volunteer_pledge"), 0),
32
        )
33
        .order_by("-end_date")
34
    )
35

36
    drive_orgs = DriveOrganization.objects.filter(drive__in=drives)
×
37
    my_drives = drives.filter(lead_organization_id__in=organizations)
×
38

39
    return render(
×
40
        request,
41
        "community_drives/list.html",
42
        {
43
            "form": form,
44
            "drives": drives,
45
            "drive_orgs": drive_orgs,
46
            "my_drives": my_drives,
47
        },
48
    )
49

50

51
def drive_dashboard(request, drive_id):
1✔
NEW
52
    drives = CommunityDrive.objects.annotate(
×
53
        meal_progress=Coalesce(Sum("driveorganization__meal_pledge"), 0),
54
        volunteer_progress=Coalesce(Sum("driveorganization__volunteer_pledge"), 0),
55
    )
NEW
56
    drive = get_object_or_404(drives, pk=drive_id)
×
NEW
57
    participating_organizations = DriveOrganization.objects.filter(drive=drive)
×
UNCOV
58
    participating_organizations = DriveOrganization.objects.filter(drive=drive).filter(
×
59
        Q(meal_pledge__gte=1) | Q(volunteer_pledge__gte=1)
60
    )
UNCOV
61
    active_user_orgs = request.user.organizationadmin_set.filter(
×
62
        organization__active=True
63
    )
64
    can_edit = any(
×
65
        org_admin.organization_id == drive.lead_organization.organization_id
66
        for org_admin in active_user_orgs
67
    )
NEW
68
    meals_percentage = (
×
69
        (drive.meal_progress / drive.meal_target) * 100 if drive.meal_progress else 0
70
    )
NEW
71
    volunteers_percentage = (
×
72
        (drive.volunteer_progress / drive.volunteer_target) * 100
73
        if drive.volunteer_progress
74
        else 0
75
    )
76

UNCOV
77
    context = {
×
78
        "drive": drive,
79
        "meals_percentage": meals_percentage,
80
        "volunteers_percentage": volunteers_percentage,
81
        "participating_organizations": participating_organizations,
82
        "active_user_orgs": active_user_orgs,
83
        "can_edit": can_edit,
84
    }
85
    return render(request, "community_drives/drive_dashboard.html", context)
×
86

87

88
def fetch_contributions(request, drive_id):
1✔
NEW
89
    if request.method == "GET":
×
NEW
90
        drive = get_object_or_404(CommunityDrive, pk=drive_id)
×
91

92
        # Fetch all the DriveOrganization records for the drive
NEW
93
        drive_organizations = DriveOrganization.objects.filter(drive=drive).filter(
×
94
            Q(meal_pledge__gte=1) | Q(volunteer_pledge__gte=1)
95
        )
96
        # Prepare the response data, including updated table data
NEW
97
        contributions = [
×
98
            {
99
                "organization_name": org.organization.organization_name,
100
                "meals_contributed": org.meal_pledge,
101
                "volunteers_contributed": org.volunteer_pledge,
102
                "created_at": org.created_at,
103
            }
104
            for org in drive_organizations
105
        ]
106

NEW
107
        return JsonResponse({"contributions": contributions})
×
108

109

110
def contribute_to_drive(request, drive_id):
1✔
111
    if request.method == "POST":
×
112
        try:
×
113
            data = json.loads(request.body)
×
114
            meals = data.get("meals")
×
115
            volunteers = data.get("volunteers")
×
116
            donor_organization_id = data.get("donor_organization")
×
117

118
            # Get the drive instance
119
            drive = get_object_or_404(CommunityDrive, drive_id=drive_id)
×
120

121
            # Get the donor organization
122
            donor_organization = get_object_or_404(
×
123
                Organization, organization_id=donor_organization_id
124
            )
125

126
            # Create or update the DriveOrganization record with the contribution
127
            drive_org, created = DriveOrganization.objects.get_or_create(
×
128
                drive=drive, organization=donor_organization
129
            )
130

131
            if int(meals) == 0 and int(volunteers) == 0:
×
132
                error_message = "Meals and volunteers both cannot be 0! Click on 'Delete' to take back your contribution"
×
133
                return JsonResponse({"success": False, "error": error_message})
×
134

135
            # Update the DriveOrganization with the new pledges
136
            drive_org.meal_pledge = int(meals)
×
137
            drive_org.volunteer_pledge = int(volunteers)
×
138
            drive_org.save()
×
139

140
            # Fetch all the DriveOrganization records for the drive
141
            drive_organizations = DriveOrganization.objects.filter(drive=drive).filter(
×
142
                Q(meal_pledge__gte=1) | Q(volunteer_pledge__gte=1)
143
            )
144

145
            # Prepare the response data, including updated table data
146
            contributions = [
×
147
                {
148
                    "organization_name": org.organization.organization_name,
149
                    "meals_contributed": org.meal_pledge,
150
                    "volunteers_contributed": org.volunteer_pledge,
151
                    "created_at": org.created_at,
152
                }
153
                for org in drive_organizations
154
            ]
155

156
            return JsonResponse({"success": True, "contributions": contributions})
×
157
        except Exception as e:
×
158
            return JsonResponse({"success": False, "error": str(e)})
×
159

160
    return JsonResponse({"success": False, "error": "Invalid request"})
×
161

162

163
@login_required
1✔
164
def upload_drive_image(request):
1✔
165
    if request.method == "POST":
1✔
166
        drive_id = request.POST.get("drive_id")
1✔
167
        image = request.FILES.get("image")
1✔
168

169
        if not drive_id or not image:
1✔
170
            return JsonResponse({"success": False, "error": "Invalid data."})
1✔
171

172
        try:
1✔
173
            # Convert image to base64 string
174
            image_data = base64.b64encode(image.read()).decode("utf-8")
1✔
175

176
            # Update the donation object with the image string
177
            drive = CommunityDrive.objects.get(drive_id=drive_id)
1✔
178
            drive.image_data = (
1✔
179
                image_data  # Assuming `image_data` is a TextField in the model
180
            )
181
            drive.save()
1✔
182

183
            return JsonResponse({"success": True, "image_data": image_data})
1✔
184
        except CommunityDrive.DoesNotExist:
1✔
185
            return JsonResponse(
1✔
186
                {"success": False, "error": "Community Drive not found."}
187
            )
188
    return JsonResponse({"success": False, "error": "Invalid request method."})
1✔
189

190

191
@login_required
1✔
192
def delete_drive_image(request):
1✔
193
    if request.method == "POST":
1✔
194
        drive_id = request.POST.get("drive_id")
1✔
195

196
        if not drive_id:
1✔
197
            return JsonResponse({"success": False, "error": "Invalid data."})
1✔
198

199
        try:
1✔
200
            drive = CommunityDrive.objects.get(drive_id=drive_id)
1✔
201
            drive.image_data = None
1✔
202
            drive.save()
1✔
203

204
            return JsonResponse({"success": True})
1✔
205
        except CommunityDrive.DoesNotExist:
1✔
206
            return JsonResponse(
1✔
207
                {"success": False, "error": "Community Drive not found."}
208
            )
209
    return JsonResponse({"success": False, "error": "Invalid request method."})
1✔
210

211

212
def get_participation_details(request, organization_id, drive_id):
1✔
213
    if request.method == "GET":
1✔
214
        # Fetch details based on organization and drive
215
        drive_org = get_object_or_404(
1✔
216
            DriveOrganization,
217
            organization__organization_id=organization_id,
218
            drive_id=drive_id,
219
        )
220
        data = {
1✔
221
            "meals": drive_org.meal_pledge,
222
            "volunteers": drive_org.volunteer_pledge,
223
        }
224
        return JsonResponse(data)
1✔
225

226

227
def delete_participation(request, organization_id, drive_id):
1✔
228
    if request.method == "DELETE":
1✔
229
        try:
1✔
230
            participation = DriveOrganization.objects.get(
1✔
231
                organization_id=organization_id, drive_id=drive_id
232
            )
233
            participation.delete()
1✔
234

235
            # Fetch all the DriveOrganization records for the drive
236
            drive_organizations = DriveOrganization.objects.filter(
1✔
237
                drive_id=drive_id
238
            ).filter(Q(meal_pledge__gte=1) | Q(volunteer_pledge__gte=1))
239

240
            # Prepare the response data, including updated table data
241
            contributions = [
1✔
242
                {
243
                    "organization_name": org.organization.organization_name,
244
                    "meals_contributed": org.meal_pledge,
245
                    "volunteers_contributed": org.volunteer_pledge,
246
                    "created_at": org.created_at,
247
                }
248
                for org in drive_organizations
249
            ]
250

251
            return JsonResponse({"success": True, "contributions": contributions})
1✔
252
        except DriveOrganization.DoesNotExist:
1✔
253
            return JsonResponse({"success": False, "error": "Participation not found"})
1✔
254
    return JsonResponse(
1✔
255
        {"success": False, "error": "Invalid request method"}, status=405
256
    )
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