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

gcivil-nyu-org / wed-fall24-team1 / #62

20 Nov 2024 09:39PM UTC coverage: 90.197% (-0.7%) from 90.918%
#62

push

coveralls-python

web-flow
#210 remove extra coveralls stuff (#209)

3064 of 3397 relevant lines covered (90.2%)

0.9 hits per line

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

77.05
/src/services/forms.py
1
# services/forms.py
2
from decimal import Decimal
1✔
3

4
from django import forms
1✔
5
from django.forms import formset_factory
1✔
6
from geopy import Nominatim
1✔
7
from geopy.exc import GeocoderTimedOut, GeocoderServiceError
1✔
8
from django.forms import BaseFormSet
1✔
9

10

11
class ServiceForm(forms.Form):
1✔
12
    CATEGORY_CHOICES = [
1✔
13
        ("Mental Health Center", "Mental Health Center"),
14
        ("Homeless Shelter", "Homeless Shelter"),
15
        ("Food Pantry", "Food Pantry"),
16
        ("Restroom", "Restroom"),
17
    ]
18

19
    name = forms.CharField(max_length=255)
1✔
20
    address = forms.CharField(widget=forms.Textarea)
1✔
21
    category = forms.ChoiceField(choices=CATEGORY_CHOICES)
1✔
22
    is_active = forms.BooleanField(
1✔
23
        required=False, initial=True, label="Is the service currently available?"
24
    )
25

26
    def clean(self):
1✔
27
        cleaned_data = super().clean()
1✔
28
        address = cleaned_data.get("address")
1✔
29

30
        if address:
1✔
31
            geolocator = Nominatim(user_agent="public_service_finder")
1✔
32
            try:
1✔
33
                location = geolocator.geocode(address)
1✔
34
                if location:
1✔
35
                    cleaned_data["latitude"] = Decimal(str(location.latitude))
1✔
36
                    cleaned_data["longitude"] = Decimal(str(location.longitude))
1✔
37
                else:
38
                    self.add_error(
×
39
                        "address",
40
                        "Unable to geocode the given address. Please check if the address is correct.",
41
                    )
42
            except (GeocoderTimedOut, GeocoderServiceError):
×
43
                self.add_error(
×
44
                    "address",
45
                    "Error occurred while geocoding the address. Please try again later.",
46
                )
47

48
        # Translate category to backend value
49
        category_translation = {
1✔
50
            "Mental Health Center": "MENTAL",
51
            "Homeless Shelter": "SHELTER",
52
            "Food Pantry": "FOOD",
53
            "Restroom": "RESTROOM",
54
        }
55
        cleaned_data["category"] = category_translation[cleaned_data["category"]]
1✔
56

57
        return cleaned_data
1✔
58

59

60
class DescriptionItemForm(forms.Form):
1✔
61
    key = forms.CharField(max_length=100)
1✔
62
    value = forms.CharField(widget=forms.Textarea(attrs={"rows": 3}))
1✔
63

64

65
class CustomDescriptionFormSet(BaseFormSet):
1✔
66
    def clean(self):
1✔
67
        """
68
        Adds validation to check for duplicate keys in the formset.
69
        """
70
        if any(self.errors):
1✔
71
            return
×
72

73
        keys = []
1✔
74
        duplicate_keys = set()
1✔
75

76
        # First pass: collect all keys and identify duplicates
77
        for form in self.forms:
1✔
78
            if self.can_delete and self._should_delete_form(form):
1✔
79
                continue
×
80

81
            if form.cleaned_data:
1✔
82
                key = form.cleaned_data.get("key")
1✔
83
                if key:
1✔
84
                    print(f"Processing key: {key}")  # Debug print
1✔
85
                    if key in keys:
1✔
86
                        duplicate_keys.add(key)
×
87
                    keys.append(key)
1✔
88

89
        # Second pass: add errors to all forms with duplicate keys
90
        if duplicate_keys:
1✔
91
            for form in self.forms:
×
92
                if self.can_delete and self._should_delete_form(form):
×
93
                    continue
×
94

95
                if form.cleaned_data:
×
96
                    key = form.cleaned_data.get("key")
×
97
                    if key in duplicate_keys:
×
98
                        form.add_error(
×
99
                            "key",
100
                            f"Duplicate key detected: '{key}'. Each key must be unique.",
101
                        )
102

103
            # Raise the validation error with all duplicate keys listed
104
            raise forms.ValidationError(
×
105
                f"Duplicate keys found: {', '.join(duplicate_keys)}. Each key must be unique."
106
            )
107

108
        print(f"All keys found: {keys}")  # Debug print for all keys
1✔
109
        return self.cleaned_data
1✔
110

111

112
DescriptionFormSet = formset_factory(
1✔
113
    DescriptionItemForm,
114
    formset=CustomDescriptionFormSet,
115
    extra=0,
116
    can_delete=True,
117
)
118

119

120
class ReviewResponseForm(forms.Form):
1✔
121
    responseText = forms.CharField(  # Changed from 'response' to 'responseText'
1✔
122
        widget=forms.Textarea(
123
            attrs={
124
                "class": "w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",
125
                "rows": "4",
126
                "placeholder": "Enter your response to this review...",
127
            }
128
        ),
129
        required=True,
130
    )
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

© 2026 Coveralls, Inc