-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodels.py
1032 lines (889 loc) · 41.6 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import datetime
import reversion
from django.conf import settings
from django.db import models
from django.db.models import JSONField, Q
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from tinymce.models import HTMLField
from api.models import (
Admin2,
Appeal,
Country,
DisasterType,
District,
Event,
GeneralDocument,
Profile,
Region,
UserCountry,
VisibilityCharChoices,
)
DATE_FORMAT = "%Y/%m/%d %H:%M"
class ERUType(models.IntegerChoices):
BASECAMP = 0, _("Basecamp")
TELECOM = 1, _("IT & Telecom")
LOGISTICS = 2, _("Logistics")
EMERGENCY_HOSPITAL = 3, _("RCRC Emergency Hospital")
EMERGENCY_CLINIC = 4, _("RCRC Emergency Clinic")
RELIEF = 5, _("Relief")
WASH_15 = 6, _("Wash M15")
WASH_20 = 7, _("Wash MSM20")
WASH_40 = 8, _("Wash M40")
WATER_SUPPLY = 9, _("Water Supply and rehabilitation")
WATER_TREATMENT = 10, _("Household Water Treatment and safe storage")
COLERA_MANAGEMENT = 11, _("Cholera Case management at Community level")
BURIALS = 12, _("Safe and Dignified Burials")
CBS = 13, _("Community Based Surveillance")
BASECAMP_S = 14, _("Base Camp – S")
BASECAMP_M = 15, _("Base Camp – M")
BASECAMP_L = 16, _("Base Camp – L")
OPD = 17, _("Outpatient Department (OPD) Module")
MHPSS = 18, _("MHPSS")
@reversion.register()
class ERUOwner(models.Model):
"""A resource that may or may not be deployed"""
national_society_country = models.ForeignKey(
Country, verbose_name=_("national society country"), null=True, on_delete=models.SET_NULL
)
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(verbose_name=_("updated at"), auto_now=True)
class Meta:
verbose_name = _("ERUs from a National Society")
verbose_name_plural = _("ERUs")
def __str__(self):
if self.national_society_country.society_name is not None:
return "%s (%s)" % (self.national_society_country.society_name, self.national_society_country.name)
return self.national_society_country.name
@reversion.register()
class ERU(models.Model):
"""A resource that can be deployed"""
type = models.IntegerField(
choices=ERUType.choices,
verbose_name=_("type"),
default=0,
help_text='<a target="_blank" href="/api/v2/erutype">Key/value pairs</a>',
)
units = models.IntegerField(verbose_name=_("units"), default=0)
equipment_units = models.IntegerField(verbose_name=_("equipment units"), default=0)
num_people_deployed = models.IntegerField(
verbose_name=_("number of people deployed"), default=0, help_text=_("still not used in frontend")
)
# where deployed (none if available)
deployed_to = models.ForeignKey(
Country, verbose_name=_("country deployed to"), null=True, blank=True, on_delete=models.SET_NULL
)
event = models.ForeignKey(Event, verbose_name=_("event"), null=True, blank=True, on_delete=models.SET_NULL)
appeal = models.ForeignKey(
Appeal,
verbose_name=_("appeal"),
null=True,
blank=True,
on_delete=models.SET_NULL,
help_text=_("still not used in frontend"),
)
# links to services
eru_owner = models.ForeignKey(ERUOwner, verbose_name=_("owner"), on_delete=models.CASCADE)
supporting_societies = models.CharField(
verbose_name=_("supporting societies"),
null=True,
blank=True,
max_length=500,
help_text=_("still not used in frontend"),
)
start_date = models.DateTimeField(verbose_name=_("start date"), null=True, help_text=_("still not used in frontend"))
end_date = models.DateTimeField(verbose_name=_("end date"), null=True, help_text=_("still not used in frontend"))
available = models.BooleanField(verbose_name=_("available"), default=False)
alert_date = models.DateTimeField(verbose_name=_("alert date"), null=True, help_text=_("still not used in frontend"))
class Meta:
verbose_name = _("Emergency Response Unit")
verbose_name_plural = _("Emergency Response Units")
def __str__(self):
return self.get_type_display()
@reversion.register()
class PersonnelDeployment(models.Model):
country_deployed_to = models.ForeignKey(Country, verbose_name=_("country deployed to"), on_delete=models.CASCADE)
region_deployed_to = models.ForeignKey(Region, verbose_name=_("region deployed to"), null=True, on_delete=models.SET_NULL)
event_deployed_to = models.ForeignKey(
Event, verbose_name=_("event deployed to"), null=True, blank=True, on_delete=models.SET_NULL
)
appeal_deployed_to = models.ForeignKey(
Appeal,
verbose_name=_("appeal deployed to"),
null=True,
blank=True,
on_delete=models.SET_NULL,
help_text=_("still not used in frontend"),
)
alert_date = models.DateTimeField(verbose_name=_("alert date"), null=True, help_text=_("still not used in frontend"))
exp_start_date = models.DateTimeField(
verbose_name=_("expire start date"), null=True, help_text=_("still not used in frontend")
)
end_duration = models.CharField(
verbose_name=_("end duration"), null=True, blank=True, max_length=100, help_text=_("still not used in frontend")
)
start_date = models.DateTimeField(verbose_name=_("start date"), null=True, help_text=_("still not used in frontend"))
end_date = models.DateTimeField(verbose_name=_("end date"), null=True, help_text=_("still not used in frontend"))
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(verbose_name=_("updated at"), auto_now=True)
previous_update = models.DateTimeField(verbose_name=_("previous update"), null=True, blank=True)
comments = models.TextField(verbose_name=_("comments"), null=True, blank=True)
is_molnix = models.BooleanField(default=False) # Source is Molnix API
class Meta:
verbose_name = _("Personnel Deployment")
verbose_name_plural = _("Personnel Deployments")
def __str__(self):
return "%s, %s" % (self.country_deployed_to, self.region_deployed_to)
@reversion.register()
class MolnixTagGroup(models.Model):
molnix_id = models.IntegerField()
name = models.CharField(max_length=255, verbose_name=_("name"))
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(verbose_name=_("updated at"), auto_now=True)
is_deprecated = models.BooleanField(default=False, help_text=_("Is this a deprecated group?"))
class Meta:
verbose_name = _("Molnix Tag Group")
verbose_name_plural = _("Molnix Tag Groups")
def __str__(self):
return self.name
@reversion.register()
class MolnixTag(models.Model):
"""
We store tags from molnix in its own model, to make m2m relations
from notifications.models.SurgeAlert and DeployedPerson
"""
molnix_id = models.IntegerField()
name = models.CharField(max_length=255)
description = models.CharField(blank=True, max_length=512)
color = models.CharField(max_length=6)
tag_type = models.CharField(max_length=127)
tag_category = models.CharField(null=True, max_length=127)
groups = models.ManyToManyField(
MolnixTagGroup,
related_name="groups",
blank=True,
)
def __str__(self):
return self.name
@reversion.register()
class DeployedPerson(models.Model):
start_date = models.DateTimeField(verbose_name=_("start date"), null=True)
end_date = models.DateTimeField(verbose_name=_("end date"), null=True, db_index=True)
name = models.CharField(verbose_name=_("name"), null=True, blank=True, max_length=255)
role = models.CharField(verbose_name=_("role"), null=True, blank=True, max_length=512)
class Meta:
verbose_name = _("Deployed Person")
verbose_name_plural = _("Deployed Persons")
def __str__(self):
return "%s - %s" % (self.name, self.role)
class Personnel(DeployedPerson):
"""
Deployed personnel (connected to personneldeployment,
which contains the details, date intervals etc.)
"""
class TypeChoices(models.TextChoices):
FACT = "fact", _("FACT")
HEOP = "heop", _("HEOP")
RDRT = "rdrt", _("RDRT")
IFRC = "ifrc", _("IFRC")
ERU = "eru", _("ERU HR")
RR = "rr", _("Rapid Response")
class StatusChoices(models.TextChoices):
ACTIVE = "active", _("ACTIVE")
HIDDEN = "hidden", _("HIDDEN")
DRAFT = "draft", _("DRAFT")
DELETED = "deleted", _("DELETED")
type = models.CharField(verbose_name=_("type"), choices=TypeChoices.choices, max_length=4)
gender = models.CharField(verbose_name=_("gender"), null=True, blank=True, max_length=15)
appraisal_received = models.BooleanField(default=False, verbose_name=_("appraisal received"))
location = models.CharField(verbose_name=_("location"), blank=True, null=True, max_length=300)
country_from = models.ForeignKey(
Country, verbose_name=_("country from"), related_name="personnel_deployments", null=True, on_delete=models.SET_NULL
)
country_to = models.ForeignKey(
Country, verbose_name=("country to"), related_name="personnel_deployments_to", null=True, on_delete=models.SET_NULL
)
deployment = models.ForeignKey(PersonnelDeployment, verbose_name=_("deployment"), on_delete=models.CASCADE)
molnix_id = models.IntegerField(blank=True, null=True)
molnix_tags = models.ManyToManyField(MolnixTag, blank=True)
molnix_status = models.CharField(
verbose_name=_("molnix status"), max_length=8, choices=StatusChoices.choices, default=StatusChoices.ACTIVE
)
is_active = models.BooleanField(default=True) # Active in Molnix API
surge_alert = models.ForeignKey( # position_id in Molnix API
"notifications.SurgeAlert", # import as string to avoid circular import (MolnixTag)
verbose_name=_("surge alert"),
null=True,
blank=True,
on_delete=models.PROTECT,
)
def __str__(self):
return "%s: %s - %s" % (self.type.upper(), self.name, self.role)
class Meta:
ordering = (
"deployment",
"country_to",
"country_from",
"molnix_id",
"deployedperson_ptr_id",
)
verbose_name = _("Personnel")
verbose_name_plural = _("Personnels")
def get_tags_for_category(self, molnix_category):
tags = self.molnix_tags.filter(tag_category=molnix_category).values("name")
names = [tag["name"] for tag in tags]
return ", ".join(names)
@reversion.register()
class PartnerSocietyActivities(models.Model):
activity = models.CharField(verbose_name=_("activity"), max_length=50)
def __str__(self):
return self.activity
class Meta:
verbose_name = _("Partner society activity")
verbose_name_plural = _("Partner society activities")
class PartnerSocietyDeployment(DeployedPerson):
activity = models.ForeignKey(
PartnerSocietyActivities,
verbose_name=_("activity"),
related_name="partner_societies",
null=True,
on_delete=models.CASCADE,
)
parent_society = models.ForeignKey(
Country,
verbose_name=_("parent society"),
related_name="partner_society_members",
null=True,
on_delete=models.SET_NULL,
)
country_deployed_to = models.ForeignKey(
Country,
verbose_name=_("country deployed to"),
related_name="country_partner_deployments",
null=True,
on_delete=models.SET_NULL,
)
district_deployed_to = models.ManyToManyField(District, verbose_name=_("district deployed to"))
class Meta:
verbose_name = _("Partner Society Deployment")
verbose_name_plural = _("Partner Society Deployments")
def __str__(self):
return "%s deployment in %s" % (self.parent_society, self.country_deployed_to)
class ProgrammeTypes(models.IntegerChoices):
BILATERAL = 0, _("Bilateral")
MULTILATERAL = 1, _("Multilateral")
DOMESTIC = 2, _("Domestic")
@reversion.register()
class Sector(models.Model):
title = models.CharField(max_length=255, verbose_name=_("title"))
color = models.CharField(max_length=7, verbose_name=_("color"), null=True, blank=True)
is_deprecated = models.BooleanField(default=False, help_text=_("Is this a deprecated sector?"))
order = models.SmallIntegerField(default=0)
class Meta:
verbose_name = _("Project Sector")
verbose_name_plural = _("Project Sectors")
def __str__(self):
return self.title
@reversion.register()
class SectorTag(models.Model):
title = models.CharField(max_length=255, verbose_name=_("title"))
color = models.CharField(max_length=7, verbose_name=_("color"), null=True, blank=True)
is_deprecated = models.BooleanField(default=False, help_text=_("Is this a deprecated sector tag?"))
order = models.SmallIntegerField(default=0)
class Meta:
verbose_name = _("Project Sector Tag")
verbose_name_plural = _("Project Sector Tags")
def __str__(self):
return self.title
class Statuses(models.IntegerChoices):
# NOTE: Make sure there are no spaces in label
# - They are used to generate column `country_annotate` for movement_activities
PLANNED = 0, _("Planned")
ONGOING = 1, _("Ongoing")
COMPLETED = 2, _("Completed")
class OperationTypes(models.IntegerChoices):
PROGRAMME = 0, _("Programme")
EMERGENCY_OPERATION = 1, _("Emergency Operation")
@reversion.register()
class RegionalProject(models.Model):
name = models.CharField(verbose_name=_("name"), max_length=100)
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True)
class Meta:
verbose_name = _("Regional Project")
verbose_name_plural = _("Regional Projects")
def __str__(self):
return self.name
# 3W
@reversion.register()
class Project(models.Model):
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True)
modified_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("modified by"),
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="project_modified_by",
)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("user"),
null=True,
blank=True,
on_delete=models.SET_NULL,
) # user who created this project
# -- Reporting NS
reporting_ns = models.ForeignKey(
Country,
verbose_name=_("reporting national society"),
on_delete=models.CASCADE,
related_name="ns_projects",
) # this is the national society that is reporting the project
reporting_ns_contact_name = models.CharField(
verbose_name=_("NS Contanct Information: Name"),
max_length=255,
blank=True,
null=True,
)
reporting_ns_contact_role = models.CharField(
verbose_name=_("NS Contanct Information: Role"),
max_length=255,
blank=True,
null=True,
)
reporting_ns_contact_email = models.CharField(
verbose_name=_("NS Contanct Information: Email"),
max_length=255,
blank=True,
null=True,
)
project_country = models.ForeignKey(
Country,
verbose_name=_("country"),
on_delete=models.CASCADE,
null=True, # NOTE: Added due to migrations issue
related_name="projects",
) # this is the country where the project is actually taking place
project_districts = models.ManyToManyField(
District,
verbose_name=_("districts"),
blank=True,
) # this is the district where the project is actually taking place
project_admin2 = models.ManyToManyField(
Admin2,
verbose_name=_("admin2"),
blank=True,
)
event = models.ForeignKey(
Event,
verbose_name=_("event"),
null=True,
blank=True,
on_delete=models.SET_NULL,
) # this is the current operation
dtype = models.ForeignKey(DisasterType, verbose_name=_("disaster type"), null=True, blank=True, on_delete=models.SET_NULL)
name = models.TextField(verbose_name=_("name"))
description = HTMLField(verbose_name=_("description"), blank=True, default="")
document = models.ForeignKey(
GeneralDocument, verbose_name=_("linked document"), null=True, blank=True, on_delete=models.SET_NULL
)
programme_type = models.IntegerField(
choices=ProgrammeTypes.choices,
default=0,
verbose_name=_("programme type"),
help_text='<a target="_blank" href="/api/v2/programmetype">Key/value pairs</a>',
)
primary_sector = models.ForeignKey(
Sector,
verbose_name=_("sector"),
on_delete=models.PROTECT,
)
secondary_sectors = models.ManyToManyField(
SectorTag,
related_name="tags",
blank=True,
)
operation_type = models.IntegerField(
choices=OperationTypes.choices,
default=0,
verbose_name=_("operation type"),
help_text='<a target="_blank" href="/api/v2/operationtype">Key/value pairs</a>',
)
start_date = models.DateField(verbose_name=_("start date"))
end_date = models.DateField(verbose_name=_("end date"))
budget_amount = models.IntegerField(verbose_name=_("budget amount"), null=True, blank=True)
actual_expenditure = models.IntegerField(verbose_name=_("actual expenditure"), null=True, blank=True)
status = models.IntegerField(
choices=Statuses.choices,
default=0,
verbose_name=_("status"),
help_text='<a target="_blank" href="/api/v2/projectstatus">Key/value pairs</a>',
)
# Target Metric
target_male = models.IntegerField(verbose_name=_("target male"), null=True, blank=True)
target_female = models.IntegerField(verbose_name=_("target female"), null=True, blank=True)
target_other = models.IntegerField(verbose_name=_("target other"), null=True, blank=True)
target_total = models.IntegerField(verbose_name=_("target total"), null=True, blank=True)
# Reached Metric
reached_male = models.IntegerField(verbose_name=_("reached male"), null=True, blank=True)
reached_female = models.IntegerField(verbose_name=_("reached female"), null=True, blank=True)
reached_other = models.IntegerField(verbose_name=_("reached other"), null=True, blank=True)
reached_total = models.IntegerField(verbose_name=_("reached total"), null=True, blank=True)
regional_project = models.ForeignKey(
RegionalProject, verbose_name=_("regional project"), null=True, blank=True, on_delete=models.SET_NULL
)
visibility = models.CharField(
max_length=64,
verbose_name=_("visibility"),
choices=VisibilityCharChoices.choices,
default=VisibilityCharChoices.PUBLIC,
)
class Meta:
verbose_name = _("Project")
verbose_name_plural = _("Projects")
def __str__(self):
if self.reporting_ns is None:
postfix = None
else:
postfix = self.reporting_ns.society_name
return "%s (%s)" % (self.name, postfix)
def save(self, *args, **kwargs):
if hasattr(self, "annual_split_detail"):
# NO NEED: and self.annual_split_detail – because we want to get here in case of [] also:
if hasattr(self, "is_annual_report") and self.is_annual_report:
arrivingIds = [asd["id"] for asd in self.annual_splits]
# Remove the records frontend has not sent, due to frontend-wise row removal:
AnnualSplit.objects.filter(project_id=self.id).exclude(id__in=arrivingIds).delete()
for split in self.annual_split_detail:
annual_split, created = AnnualSplit.objects.get_or_create(pk=split["id"], project_id=self.id)
annual_split.year = split["year"] if "year" in split else None
annual_split.budget_amount = split["budget_amount"] if "budget_amount" in split else None
annual_split.target_male = split["target_male"] if "target_male" in split else None
annual_split.target_female = split["target_female"] if "target_female" in split else None
annual_split.target_other = split["target_other"] if "target_other" in split else None
annual_split.target_total = split["target_total"] if "target_total" in split else None
annual_split.reached_male = split["reached_male"] if "reached_male" in split else None
annual_split.reached_female = split["reached_female"] if "reached_female" in split else None
annual_split.reached_other = split["reached_other"] if "reached_other" in split else None
annual_split.reached_total = split["reached_total"] if "reached_total" in split else None
annual_split.save()
else:
AnnualSplit.objects.filter(project_id=self.id).delete()
# Automatically assign status according to the start_date and end_date
# Cronjob will change the status automatically in future
now = timezone.now().date()
if self.start_date > now:
self.status = Statuses.PLANNED
elif self.start_date <= now <= self.end_date:
self.status = Statuses.ONGOING
else:
self.status = Statuses.COMPLETED
return super().save(*args, **kwargs)
# FIXME: Is this used?
@staticmethod
def get_for(user, queryset=None):
countries_qs = (
UserCountry.objects.filter(user=user).values("country").union(Profile.objects.filter(user=user).values("country"))
)
return queryset.exclude(
Q(visibility=VisibilityCharChoices.IFRC_NS) & ~Q(project_country__in=countries_qs) & ~Q(reporting_ns__in=countries_qs)
)
# FIXME | Something like this would be helpful, but not this way. Hint:
# def get_primary_sector_display(self):
# return 'primary_sector__title'
#
# def get_secondary_sectors_display(self):
# return 'secondary_sectors__title'
@reversion.register(follow=("project",))
class AnnualSplit(models.Model):
"""Annual split for Project"""
project = models.ForeignKey(Project, verbose_name=_("project"), related_name="annual_splits", on_delete=models.CASCADE)
year = models.IntegerField(verbose_name=_("year"), null=True, blank=True)
budget_amount = models.IntegerField(verbose_name=_("amount"), null=True, blank=True)
target_male = models.IntegerField(verbose_name=_("target male"), null=True, blank=True)
target_female = models.IntegerField(verbose_name=_("target female"), null=True, blank=True)
target_other = models.IntegerField(verbose_name=_("target other"), null=True, blank=True)
target_total = models.IntegerField(verbose_name=_("target total"), null=True, blank=True)
reached_male = models.IntegerField(verbose_name=_("reached male"), null=True, blank=True)
reached_female = models.IntegerField(verbose_name=_("reached female"), null=True, blank=True)
reached_other = models.IntegerField(verbose_name=_("reached other"), null=True, blank=True)
reached_total = models.IntegerField(verbose_name=_("reached total"), null=True, blank=True)
class Meta:
verbose_name = _("Annual Split")
verbose_name_plural = _("Annual Splits")
def __str__(self):
return "%s: %s %s | %s %s" % (
str(self.project_id),
str(self.year),
str(self.budget_amount),
str(self.target_male),
str(self.reached_male),
)
@reversion.register()
class ProjectImport(models.Model):
"""
Track Project Imports (For Django Admin Panel)
"""
class ProjImpStatus(models.TextChoices):
PENDING = "pending", _("Pending")
SUCCESS = "success", _("Success")
FAILURE = "failure", _("Failure")
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("created by"),
on_delete=models.SET_NULL,
null=True,
) # user who created this project import
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
projects_created = models.ManyToManyField(Project, verbose_name=_("projects created"))
message = models.TextField(verbose_name=_("message"))
status = models.CharField(
verbose_name=_("status"), max_length=10, choices=ProjImpStatus.choices, default=ProjImpStatus.PENDING
)
file = models.FileField(verbose_name=_("file"), upload_to="project-imports/")
class Meta:
verbose_name = _("Project Import")
verbose_name_plural = _("Projects Import")
def __str__(self):
return f"Project Import {self.get_status_display()}:{self.created_at}"
# -------------- Emergency 3W [Start]
@reversion.register()
class EmergencyProject(models.Model):
class ActivityLead(models.TextChoices):
NATIONAL_SOCIETY = "national_society", _("National Society")
DEPLOYED_ERU = "deployed_eru", _("Deployed ERU")
class ActivityStatus(models.TextChoices):
ON_GOING = "on_going", _("Ongoing")
COMPLETE = "complete", _("Complete")
PLANNED = "planned", _("Planned")
title = models.CharField(max_length=255, verbose_name=_("title"))
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
modified_at = models.DateTimeField(verbose_name=_("modified at"), auto_now=True)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("created by"),
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
modified_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("modified by"),
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
event = models.ForeignKey(
Event, verbose_name=_("Event"), on_delete=models.CASCADE, related_name="+"
) # this is the current operation
districts = models.ManyToManyField(
District,
verbose_name=_("Districts"),
related_name="+",
blank=True,
) # this is the district where the project is actually taking place
admin2 = models.ManyToManyField(
Admin2,
verbose_name=_("admin2"),
blank=True,
) # admin2 of the district where the project is actually taking place
# Who is conducting the Activity
activity_lead = models.CharField(
max_length=30,
verbose_name=_("Activity lead"),
choices=ActivityLead.choices,
)
# -- Reporting NS
reporting_ns = models.ForeignKey(
Country,
verbose_name=_("Reporting national society"),
on_delete=models.CASCADE,
related_name="+",
blank=True,
null=True,
)
reporting_ns_contact_name = models.CharField(
verbose_name=_("NS Contact Information: Name"),
max_length=255,
blank=True,
null=True,
)
reporting_ns_contact_role = models.CharField(
verbose_name=_("NS Contact Information: Role"),
max_length=255,
blank=True,
null=True,
)
reporting_ns_contact_email = models.CharField(
verbose_name=_("NS Contact Information: Email"),
max_length=255,
blank=True,
null=True,
)
# -- Deployed ERU
deployed_eru = models.ForeignKey(
ERU,
verbose_name=_("Deployed ERU"),
on_delete=models.CASCADE,
related_name="+",
blank=True,
null=True,
)
start_date = models.DateField(
verbose_name=_("Start Date"),
blank=False,
null=False,
)
end_date = models.DateField(
verbose_name=_("End Date"),
null=True,
blank=True,
)
status = models.CharField(
max_length=40,
choices=ActivityStatus.choices,
default=ActivityStatus.ON_GOING,
)
country = models.ForeignKey(
Country,
verbose_name=_("Country"),
on_delete=models.CASCADE,
related_name="+",
) # Country to be among the country in event
visibility = models.CharField(
max_length=32,
verbose_name=_("visibility"),
choices=VisibilityCharChoices.choices,
default=VisibilityCharChoices.PUBLIC,
)
def __str__(self):
return self.title
@reversion.register()
class EmergencyProjectActivitySector(models.Model):
title = models.CharField(max_length=255, verbose_name=_("title"))
order = models.SmallIntegerField(default=0)
def __str__(self):
return self.title
@reversion.register()
class EmergencyProjectActivityAction(models.Model):
sector = models.ForeignKey(
EmergencyProjectActivitySector,
verbose_name=_("sector"),
on_delete=models.PROTECT,
)
title = models.CharField(max_length=255, verbose_name=_("title"))
order = models.SmallIntegerField(default=0)
description = models.TextField(verbose_name=_("Description"), blank=True)
is_cash_type = models.BooleanField(default=False, verbose_name=_("is_cash_type"))
has_location = models.BooleanField(default=False, verbose_name=_("has location"))
def __str__(self):
return self.title
@reversion.register()
class EmergencyProjectActivityActionSupply(models.Model):
action = models.ForeignKey(
EmergencyProjectActivityAction,
verbose_name=_("action"),
related_name="supplies",
on_delete=models.PROTECT,
)
title = models.CharField(max_length=255, verbose_name=_("title"))
order = models.SmallIntegerField(default=0)
def __str__(self):
return self.title
@reversion.register()
class EmergencyProjectActivityLocation(models.Model):
# Location Data
latitude = models.FloatField(verbose_name=_("latitude"))
longitude = models.FloatField(verbose_name=_("longitude"))
description = models.TextField(verbose_name=_("location description"))
def __str__(self):
return f"{self.latitude} - {self.longitude}"
@reversion.register(follow=("project",))
class EmergencyProjectActivity(models.Model):
class PeopleHouseholds(models.TextChoices):
PEOPLE = "people", _("People")
HOUSEHOLDS = "households", _("Households")
sector = models.ForeignKey(
EmergencyProjectActivitySector,
verbose_name=_("sector"),
on_delete=models.CASCADE,
)
action = models.ForeignKey(
EmergencyProjectActivityAction,
verbose_name=_("action"),
on_delete=models.CASCADE,
blank=True,
null=True,
)
project = models.ForeignKey(
EmergencyProject,
verbose_name=_("emergency project/3W"),
on_delete=models.CASCADE,
related_name="activities",
)
is_simplified_report = models.BooleanField(verbose_name=_("is_simplified_report"), default=True)
is_disaggregated_for_disabled = models.BooleanField(verbose_name=_("Is disaggregated for disabled"), null=True, blank=True)
has_no_data_on_people_reached = models.BooleanField(verbose_name=_("Has no data on people reached"), null=True, blank=True)
# Metrics
people_households = models.CharField(
max_length=50,
null=True,
blank=True,
choices=PeopleHouseholds.choices,
verbose_name=_("People Households"),
)
household_count = models.IntegerField(verbose_name=_("Household"), null=True, blank=True)
# if sector.is_cash_type is True
amount = models.IntegerField(verbose_name=_("Amount"), null=True, blank=True)
beneficiaries_count = models.IntegerField(verbose_name=_("Beneficiaries Count"), null=True, blank=True)
item_count = models.IntegerField(verbose_name=_("Item"), null=True, blank=True)
people_count = models.IntegerField(verbose_name=_("People"), null=True, blank=True)
male_count = models.IntegerField(verbose_name=_("Men"), null=True, blank=True)
female_count = models.IntegerField(verbose_name=_("Female"), null=True, blank=True)
# -- Age group
male_unknown_age_count = models.IntegerField(verbose_name=_("Male Unknown Age"), null=True, blank=True)
female_unknown_age_count = models.IntegerField(verbose_name=_("Female Unknown Age"), null=True, blank=True)
other_unknown_age_count = models.IntegerField(verbose_name=_("Other Unknown Age"), null=True, blank=True)
# -- When is_simplified_report is False
male_0_1_count = models.IntegerField(verbose_name=_("Boys 0-1"), null=True, blank=True)
male_2_5_count = models.IntegerField(verbose_name=_("Boys 2-5"), null=True, blank=True)
male_6_12_count = models.IntegerField(verbose_name=_("Boys 6-12"), null=True, blank=True)
male_13_17_count = models.IntegerField(verbose_name=_("Boys 13-17"), null=True, blank=True)
male_18_59_count = models.IntegerField(verbose_name=_("Men 18-29"), null=True, blank=True)
male_60_plus_count = models.IntegerField(verbose_name=_("Older Men 60+"), null=True, blank=True)
female_0_1_count = models.IntegerField(verbose_name=_("Girls 0-1"), null=True, blank=True)
female_2_5_count = models.IntegerField(verbose_name=_("Girls 2-5"), null=True, blank=True)
female_6_12_count = models.IntegerField(verbose_name=_("Girls 6-12"), null=True, blank=True)
female_13_17_count = models.IntegerField(verbose_name=_("Girls 13-17"), null=True, blank=True)
female_18_59_count = models.IntegerField(verbose_name=_("Women 18-29"), null=True, blank=True)
female_60_plus_count = models.IntegerField(verbose_name=_("Older Women 60+"), null=True, blank=True)
other_0_1_count = models.IntegerField(verbose_name=_("Others/Unknown 0-1"), null=True, blank=True)
other_2_5_count = models.IntegerField(verbose_name=_("Others/Unknown 2-5"), null=True, blank=True)
other_6_12_count = models.IntegerField(verbose_name=_("Others/Unknown 6-12"), null=True, blank=True)
other_13_17_count = models.IntegerField(verbose_name=_("Others/Unknown 13-17"), null=True, blank=True)
other_18_59_count = models.IntegerField(verbose_name=_("Others/Unknown 18-29"), null=True, blank=True)
other_60_plus_count = models.IntegerField(verbose_name=_("Others/Unknown 60+"), null=True, blank=True)
# -- When is_disaggregated_for_disabled is True
disabled_male_0_1_count = models.IntegerField(verbose_name=_("Disabled Boys 0-1"), null=True, blank=True)
disabled_male_2_5_count = models.IntegerField(verbose_name=_("Disabled Boys 2-5"), null=True, blank=True)
disabled_male_6_12_count = models.IntegerField(verbose_name=_("Disabled Boys 6-12"), null=True, blank=True)
disabled_male_13_17_count = models.IntegerField(verbose_name=_("Disabled Boys 13-17"), null=True, blank=True)
disabled_male_18_59_count = models.IntegerField(verbose_name=_("Disabled Men 18-29"), null=True, blank=True)
disabled_male_60_plus_count = models.IntegerField(verbose_name=_("Disabled Older Men 60+"), null=True, blank=True)
disabled_female_0_1_count = models.IntegerField(verbose_name=_("Disabled Girls 0-1"), null=True, blank=True)
disabled_female_2_5_count = models.IntegerField(verbose_name=_("Disabled Girls 2-5"), null=True, blank=True)
disabled_female_6_12_count = models.IntegerField(verbose_name=_("Disabled Girls 6-12"), null=True, blank=True)
disabled_female_13_17_count = models.IntegerField(verbose_name=_("Disabled Girls 13-17"), null=True, blank=True)
disabled_female_18_59_count = models.IntegerField(verbose_name=_("Disabled Women 18-29"), null=True, blank=True)
disabled_female_60_plus_count = models.IntegerField(verbose_name=_("Disabled Older Women 60+"), null=True, blank=True)
disabled_other_0_1_count = models.IntegerField(verbose_name=_("Disabled Others/Unknown 0-1"), null=True, blank=True)
disabled_other_2_5_count = models.IntegerField(verbose_name=_("Disabled Others/Unknown 2-5"), null=True, blank=True)
disabled_other_6_12_count = models.IntegerField(verbose_name=_("Disabled Others/Unknown 6-12"), null=True, blank=True)
disabled_other_13_17_count = models.IntegerField(verbose_name=_("Disabled Others/Unknown 13-17"), null=True, blank=True)
disabled_other_18_59_count = models.IntegerField(verbose_name=_("Disabled Others/Unknown 18-29"), null=True, blank=True)
disabled_other_60_plus_count = models.IntegerField(verbose_name=_("Disabled Others/Unknown 60+"), null=True, blank=True)
disabled_male_unknown_age_count = models.IntegerField(verbose_name=_("Disabled Male Unknown Age"), null=True, blank=True)
disabled_female_unknown_age_count = models.IntegerField(verbose_name=_("Disabled Female Unknown Age"), null=True, blank=True)
disabled_other_unknown_age_count = models.IntegerField(verbose_name=_("Disabled Other Unknown Age"), null=True, blank=True)
# More Details
details = models.TextField(verbose_name=_("details"), blank=True, null=True)
supplies = JSONField(verbose_name=_("supplies"), default=dict, blank=True) # key: count (key: System defined id)
# Custom action/supplies
custom_action = models.CharField(verbose_name=_("custom_action"), max_length=255, blank=True, null=True)
custom_supplies = JSONField(verbose_name=_("custom supplies"), default=dict, blank=True) # key: count (key: User defined)
# point details
# point_count to be used if is_simplified_report is True
point_count = models.IntegerField(verbose_name=_("Point Count"), null=True, blank=True)
# points to be used if is_simplified_report is False
points = models.ManyToManyField(EmergencyProjectActivityLocation, verbose_name=_("Points"), blank=True)
# -------------- Emergency 3W [END]
@reversion.register()
class ERUReadiness(models.Model):
"""ERU Readiness concerning personnel and equipment"""
national_society = models.ForeignKey(
Country, verbose_name=_("national society"), null=True, blank=True, on_delete=models.SET_NULL
)
ERU_type = models.IntegerField(choices=ERUType.choices, verbose_name=_("ERU type"), default=0)
is_personnel = models.BooleanField(verbose_name=_("is personnel?"), default=False)
is_equipment = models.BooleanField(verbose_name=_("is equipment?"), default=False)
updated_at = models.DateTimeField(verbose_name=_("updated at"), auto_now=True)
class Meta:
ordering = (
"updated_at",
"national_society",
)
verbose_name = _("ERU Readiness")
verbose_name_plural = _("NS-es ERU Readiness")
def __str__(self):
if self.national_society is None:
name = None
else:
name = self.national_society
return f"{self.get_ERU_type_display()} ({name})"
###############################################################################
####################### Deprecated tables ##################################### noqa: E266
# https://github.com/IFRCGo/go-frontend/issues/335
# NOTE: Translation is skipped for Deprecated tables
###############################################################################
class Heop(models.Model):
"""A deployment of a head officer"""
start_date = models.DateTimeField(null=True)
end_date = models.DateTimeField(null=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
event = models.ForeignKey(Event, null=True, blank=True, on_delete=models.SET_NULL)
dtype = models.ForeignKey(DisasterType, null=True, blank=True, on_delete=models.SET_NULL)
person = models.CharField(null=True, blank=True, max_length=100)
role = models.CharField(default="HeOps", null=True, blank=True, max_length=32)
comments = models.TextField(null=True, blank=True)
class Meta:
verbose_name = "HeOp"
verbose_name_plural = "HeOps"
def __str__(self):
return "%s (%s) %s - %s" % (
self.person,
self.country,
datetime.strftime(self.start_date, DATE_FORMAT),
datetime.strftime(self.end_date, DATE_FORMAT),
)
class Fact(models.Model):
"""A FACT resource"""
start_date = models.DateTimeField(null=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
event = models.ForeignKey(Event, null=True, blank=True, on_delete=models.SET_NULL)
dtype = models.ForeignKey(DisasterType, null=True, blank=True, on_delete=models.SET_NULL)
comments = models.TextField(null=True, blank=True)
def __str__(self):
return "%s %s" % (self.country, datetime.strftime(self.start_date, DATE_FORMAT))
class Meta:
verbose_name = "FACT"
verbose_name_plural = "FACTs"
class Rdrt(models.Model):
"""An RDRT/RIT resource"""