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

gm3dmo / cmp / 12136991535

03 Dec 2024 09:44AM UTC coverage: 63.732% (-0.3%) from 64.039%
12136991535

push

github

web-flow
Merge pull request #279 from gm3dmo/issue278

close #278

3 of 34 new or added lines in 2 files covered. (8.82%)

1 existing line in 1 file now uncovered.

724 of 1136 relevant lines covered (63.73%)

1.27 hits per line

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

67.26
/cmp/forms.py
1
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
2✔
2

3
from django import forms
2✔
4

5
from .models import CustomUser
2✔
6

7
from .models import Country
2✔
8
from .models import Rank
2✔
9
from .models import Cemetery
2✔
10
from .models import PowCamp
2✔
11
from .models import Soldier
2✔
12
from .models import SoldierDeath
2✔
13
from .models import SoldierImprisonment
2✔
14
from .models import Company
2✔
15
from .models import Decoration
2✔
16
from .models import Acknowledgement
2✔
17
from .models import ProvostAppointment
2✔
18
from django.forms import inlineformset_factory
2✔
19

20
from crispy_forms.helper import FormHelper
2✔
21
from crispy_forms.layout import Layout, Field, Div, HTML
2✔
22
from crispy_forms.bootstrap import Accordion, AccordionGroup
2✔
23

24
# First, create a helper class for the formset
25
class SoldierImprisonmentFormSetHelper(FormHelper):
2✔
26
    def __init__(self, *args, **kwargs):
2✔
27
        super().__init__(*args, **kwargs)
2✔
28
        self.form_tag = False
2✔
29
        
30
        # Check if there's any existing data
31
        has_data = False
2✔
32
        if hasattr(self, 'form') and hasattr(self.form, 'instance'):
2✔
33
            has_data = SoldierImprisonment.objects.filter(soldier=self.form.instance).exists()
×
34
        
35
        # Set the title based on whether there's data
36
        title = 'Prisoner of War Details' if has_data else 'Prisoner of War Details (None Recorded)'
2✔
37
        
38
        self.layout = Layout(
2✔
39
            Accordion(
40
                AccordionGroup(
41
                    title,
42
                    'pow_camp',
43
                    'pow_number',
44
                    'date_from',
45
                    'date_to',
46
                    'notes',
47
                    active=has_data,
48
                    css_class='bg-light'
49
                ),
50
                css_id="imprisonment-details-accordion"
51
            )
52
        )
53

54
# Then create the formset with the helper
55
SoldierImprisonmentInlineFormSet = inlineformset_factory(
2✔
56
    Soldier,
57
    SoldierImprisonment,
58
    fields=['pow_camp', 'pow_number', 'date_from', 'date_to', 'notes'],
59
    extra=1,
60
    can_delete=True,
61
    can_order=False,
62
    min_num=0,  # Minimum number of forms
63
    validate_min=False,  # Don't validate minimum number of forms
64
    max_num=None,  # Maximum number of forms (None = unlimited)
65
    validate_max=False,  # Don't validate maximum number of forms
66
    absolute_max=1000,  # Absolute maximum number of forms
67
)
68

69
# Add the helper to the formset
70
SoldierImprisonmentInlineFormSet.helper = SoldierImprisonmentFormSetHelper()
2✔
71

72
class CustomUserCreationForm(UserCreationForm):
2✔
73
    class Meta:
2✔
74
        model = CustomUser
2✔
75
        fields = ("email",)
2✔
76

77

78
class CustomUserChangeForm(UserChangeForm):
2✔
79
    class Meta:
2✔
80
        model = CustomUser
2✔
81
        fields = ("email",)
2✔
82

83

84
class editPowCampForm(forms.ModelForm):
2✔
85
    def __init__(self, *args, **kwargs):
2✔
86
        super().__init__(*args, **kwargs)
×
87
        self.helper = FormHelper()
×
88
        
89
        # Determine header class and active state based on whether form has data
90
        header_class = 'bg-light' if self.instance and self.instance.pk else 'bg-light-blue'
×
91
        is_active = bool(self.instance and self.instance.pk)
×
92
        
93
        self.helper.layout = Layout(
×
94
            Field('name'),
95
            Accordion(
96
                AccordionGroup(
97
                    'POW Details',
98
                    Field('nearest_city'),
99
                    Field('notes'),
100
                    Field('wartime_country'),
101
                    Field('latitude'),
102
                    Field('longitude'),
103
                    active=is_active,
104
                    button_class=header_class
105
                ),
106
                css_id="powcamp-details-accordion"
107
            )
108
        )
109

110
    name = forms.CharField(
2✔
111
        widget=forms.TextInput(attrs={
112
            'class': 'wide-input'
113
        })
114
    )
115
    nearest_city = forms.CharField(
2✔
116
        widget=forms.TextInput(attrs={
117
            'class': 'wide-input'
118
        }),
119
        required=False
120
    )
121
    notes = forms.CharField(
2✔
122
        widget=forms.Textarea(attrs={
123
            'class': 'wide-input'
124
        }),
125
        required=False
126
    )
127
    wartime_country = forms.CharField(
2✔
128
        widget=forms.TextInput(attrs={
129
            'class': 'wide-input'
130
        }),
131
        required=False
132
    )
133
    latitude = forms.CharField(
2✔
134
        widget=forms.TextInput(attrs={
135
            'class': 'wide-input'
136
        }),
137
        required=False
138
    )
139
    longitude = forms.CharField(
2✔
140
        widget=forms.TextInput(attrs={
141
            'class': 'wide-input'
142
        }),
143
        required=False
144
    )
145

146
    class Meta:
2✔
147
        model = PowCamp
2✔
148
        fields = '__all__'
2✔
149

150

151
class editCemeteryForm(forms.ModelForm):
2✔
152
    name = forms.CharField(
2✔
153
        widget=forms.TextInput(attrs={
154
            'class': 'wide-input',
155
            'style': 'width: 500px;'
156
        })
157
    )
158
    country = forms.ModelChoiceField(
2✔
159
        queryset=Country.objects.all().order_by('name'),
160
        empty_label="Select a country"
161
    )
162

163
    class Meta:
2✔
164
        model = Cemetery
2✔
165
        fields = ['name', 'country', 'latitude', 'longitude']
2✔
166

167

168
class editCountryForm(forms.ModelForm):
2✔
169
    def __init__(self, *args, **kwargs):
2✔
170
        super().__init__(*args, **kwargs)
×
171
        self.helper = FormHelper()
×
172
        self.helper.label_class = 'form-label'  
×
173
    class Meta:
2✔
174
        model = Country
2✔
175
        fields = "__all__"
2✔
176

177
class editAcknowledgementForm(forms.ModelForm):
2✔
178
    def __init__(self, *args, **kwargs):
2✔
179
        super().__init__(*args, **kwargs)
×
180
        self.helper = FormHelper()
×
181
        self.helper.label_class = 'form-label'  
×
182
    class Meta:
2✔
183
        model = Acknowledgement
2✔
184
        created_at = forms.DateTimeField(disabled=True, required=False)
2✔
185
        exclude = ['created_at']  # This will hide created_at from the form
2✔
186
        fields = '__all__'
2✔
187

188

189
class editCompanyForm(forms.ModelForm):
2✔
190
    def __init__(self, *args, **kwargs):
2✔
191
        super().__init__(*args, **kwargs)
×
192
        self.helper = FormHelper()
×
193
        self.helper.label_class = 'form-label'  
×
194
    class Meta:
2✔
195
        model = Company 
2✔
196
        fields = "__all__"
2✔
197

198
class editDecorationForm(forms.ModelForm):
2✔
199
    def __init__(self, *args, **kwargs):
2✔
200
        super().__init__(*args, **kwargs)
×
201
        self.helper = FormHelper()
×
202
        self.helper.label_class = 'form-label'  
×
203
    class Meta:
2✔
204
        model = Decoration
2✔
205
        fields = "__all__"
2✔
206

207

208
class editRankForm(forms.ModelForm):
2✔
209
    name = forms.CharField(
2✔
210
        widget=forms.TextInput(attrs={
211
            'class': 'wide-input',
212
        })
213
    )
214

215
    class Meta:
2✔
216
        model = Rank
2✔
217
        fields = '__all__'
2✔
218

219

220
class editSoldierDeathForm(forms.ModelForm):
2✔
221
    class Meta:
2✔
222
        model = SoldierDeath
2✔
223
        fields = ['date', 'company', 'cemetery', 'cwgc_id', 'image']
2✔
224

225
    def __init__(self, *args, **kwargs):
2✔
226
        super().__init__(*args, **kwargs)
×
227
        self.helper = FormHelper()
×
228
        
229
        # Determine header class and active state based on whether form has data
230
        header_class = 'bg-light' if self.instance and self.instance.pk else 'bg-light-blue'
×
231
        is_active = bool(self.instance and self.instance.pk)
×
232
        
233
        # Set title based on whether there's data
234
        title = 'Death Details' if self.instance and self.instance.pk else 'Death Details (None Recorded)'
×
235
        
236
        self.helper.layout = Layout(
×
237
            Accordion(
238
                AccordionGroup(
239
                    title,
240
                    'date',
241
                    'company',
242
                    'cemetery',
243
                    'cwgc_id',
244
                    'image',
245
                    active=is_active,
246
                    button_class=header_class
247
                ),
248
                css_id="death-details-accordion"
249
            )
250
        )
251

252

253
class editSoldierForm(forms.ModelForm):
2✔
254
    def __init__(self, *args, **kwargs):
2✔
255
        super().__init__(*args, **kwargs)
×
256
        self.helper = FormHelper()
×
257
        self.helper.label_class = 'form-label'  
×
258

259
        # Determine header class and active state based on whether form has data
260
        header_class = 'bg-light' if self.instance and self.instance.pk else 'bg-light-blue'
×
261
        is_active = bool(self.instance and self.instance.pk)
×
262
        
263
        # Check if there are any imprisonment records
NEW
264
        has_imprisonment = False
×
NEW
265
        if self.instance and self.instance.pk:
×
NEW
266
            has_imprisonment = SoldierImprisonment.objects.filter(soldier=self.instance).exists()
×
267
        
268
        # Set title based on whether there's data
NEW
269
        title = 'Prisoner of War Details' if has_imprisonment else 'Prisoner of War Details (None Recorded)'
×
270
        
UNCOV
271
        self.helper.layout = Layout(
×
272
            Field('surname'),
273
            Field('initials'),
274
            Field('army_number'),
275
            Field('rank'),
276
            Field('provost_officer'),
277
            Field('notes'),
278
            Accordion(
279
                AccordionGroup(
280
                    title,
281
                    'imprisonment_formset',
282
                    active=has_imprisonment,
283
                    button_class=header_class
284
                ),
285
                css_id="imprisonment-details-accordion"
286
            )
287
        )
288
        # Initialize the formset
289
        self.imprisonment_formset = SoldierImprisonmentInlineFormSet(
×
290
            instance=self.instance,
291
            prefix='imprisonment'
292
        )
293

294
    class Meta:
2✔
295
        model = Soldier
2✔
296
        exclude = ['created_at']  # Exclude the created_at field
2✔
297

298

299
class ProvostOfficerForm(forms.ModelForm):
2✔
300
    def __init__(self, *args, **kwargs):
2✔
301
        super().__init__(*args, **kwargs)
×
302
        self.helper = FormHelper()
×
303
        self.helper.label_class = 'form-label'  
×
304
        super().__init__(*args, **kwargs)
×
305
        self.fields['rank'].queryset = Rank.objects.filter(rank_class="OF").order_by('name')
×
306
    provost_officer = forms.BooleanField(
2✔
307
        initial=True,
308
        disabled=True,
309
        required=True,
310
        help_text="All officers created through this form are automatically marked as Provost Officers"
311
    )
312
    class Meta:
2✔
313
        model = Soldier
2✔
314
        fields = ['surname', 'initials', 'army_number', 'rank', 'provost_officer', 'notes']
2✔
315
        widgets = {
2✔
316
            'notes': forms.Textarea(attrs={'rows': 3}),
317
        }
318
    def save(self, commit=True):
2✔
319
        soldier = super().save(commit=False)
×
320
        soldier.provost_officer = True  # Set provost_officer to True
×
321
        print(soldier)
×
322
        if commit:
×
323
            soldier.save()
×
324
        return soldier
×
325

326

327
class ProvostAppointmentForm(forms.ModelForm):
2✔
328
    def __init__(self, *args, **kwargs):
2✔
329
        super().__init__(*args, **kwargs)
×
330
        self.helper = FormHelper()
×
331
        self.helper.label_class = 'form-label'  
×
332
        
333
    class Meta:
2✔
334
        model = ProvostAppointment
2✔
335
        fields = ['rank', 'date', 'notes']
2✔
336
        widgets = {
2✔
337
            'notes': forms.Textarea(attrs={'rows': 3}),
338
        }
339

340
class AcknowledgementForm(forms.ModelForm):
2✔
341
    class Meta:
2✔
342
        model = Acknowledgement
2✔
343
        fields = ['surname', 'name', 'notes']
2✔
344

345
class SoldierForm(forms.ModelForm):
2✔
346
    def __init__(self, *args, **kwargs):
2✔
347
        super().__init__(*args, **kwargs)
×
348
        self.helper = FormHelper()
×
349
        
350
        # Determine header class and active state based on whether form has data
351
        header_class = 'bg-light' if self.instance and self.instance.pk else 'bg-light-blue'
×
352
        is_active = bool(self.instance and self.instance.pk)
×
353
        
354
        self.helper.layout = Layout(
×
355
            Field('surname'),
356
            Field('initials'),
357
            Field('army_number'),
358
            Field('rank'),
359
            Field('provost_officer'),
360
            Field('notes'),
361
            Accordion(
362
                AccordionGroup(
363
                    'Prisoner of War Details',
364
                    'imprisonment_formset',
365
                    active=is_active,
366
                    button_class=header_class
367
                ),
368
                css_id="imprisonment-details-accordion"
369
            )
370
        )
371
        # Initialize the formset
372
        self.imprisonment_formset = SoldierImprisonmentInlineFormSet(
×
373
            instance=self.instance,
374
            prefix='imprisonment'
375
        )
376

377
    class Meta:
2✔
378
        model = Soldier
2✔
379
        fields = ['surname', 'initials', 'army_number', 'rank', 'provost_officer', 'notes']
2✔
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