Skip to content

Commit

Permalink
Changes for 3W
Browse files Browse the repository at this point in the history
* FIX #516
    - Add `operation_type` field to `Project`

* FIX #514
    * Change sector field to primary_sector
    * Add secondary_sectors field
    * Update Tests
    * Update Project Endpoints

* Add flake8 config
* Add PYTHONUNBUFFERED=1 to force server stdin, stdout and stderr to be totally unbuffered.
  • Loading branch information
thenav56 committed Oct 25, 2019
1 parent 5b1e034 commit cb2a42e
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 15 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.tmp/
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
ignore = C901, W504
max-line-length = 120
exclude = .git,__pycache__,old,build,dist
max-complexity = 10
4 changes: 4 additions & 0 deletions deployments/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import deployments.models as models
from api.admin_classes import RegionRestrictedAdmin

from .forms import ProjectForm


class ERUInline(admin.TabularInline):
model = models.ERU
Expand Down Expand Up @@ -47,8 +49,10 @@ class PartnerSocietyDeploymentAdmin(RegionRestrictedAdmin):


class ProjectAdmin(admin.ModelAdmin):
form = ProjectForm
reporting_ns_in = 'country_from__in'
search_fields = ('name',)
autocomplete_fields = ('user', 'reporting_ns', 'project_district',)


class ERUReadinessAdmin(admin.ModelAdmin):
Expand Down
17 changes: 12 additions & 5 deletions deployments/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
PartnerSocietyDeployment,
ProgrammeTypes,
Sectors,
OperationTypes,
Statuses,
Project,
)
Expand Down Expand Up @@ -158,12 +159,16 @@ def handle_post(self, request, *args, **kwargs):
currentDT = datetime.datetime.now(pytz.timezone('UTC'))
if 'programme_type' not in body:
body['programme_type'] = ProgrammeTypes.MULTILATERAL
if 'sector' not in body:
body['sector'] = Sectors.WASH
if 'primary_sector' not in body:
body['primary_sector'] = Sectors.WASH
if 'secondary_sectors' not in body:
body['secondary_sectors'] = []
if 'operation_type' not in body:
body['operation_type'] = OperationTypes.LONG_TERM_OPERATION
if 'start_date' not in body:
body['start_date'] = str(currentDT)
body['start_date'] = str(currentDT)
if 'end_date' not in body:
body['end_date'] = str(currentDT)
body['end_date'] = str(currentDT)
if 'budget_amount' not in body:
body['budget_amount'] = 0
if 'status' not in body:
Expand All @@ -173,7 +178,9 @@ def handle_post(self, request, *args, **kwargs):
project_district_id = body['project_district'],
name = body['name'],
programme_type = body['programme_type'],
sector = body['sector'],
primary_sector = body['primary_sector'],
secondary_sectors = body['secondary_sectors'],
operation_type = body['operation_type'],
start_date = body['start_date'],
end_date = body['end_date'],
budget_amount = body['budget_amount'],
Expand Down
34 changes: 34 additions & 0 deletions deployments/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django import forms

from .models import Project, Sectors


class ProjectForm(forms.ModelForm):
"""
Custom Form For Project
- secondary_sectors: Array EnumField
"""
secondary_sectors = forms.MultipleChoiceField(choices=Sectors.choices, required=False)

class Meta:
model = Project
fields = '__all__'

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.secondary_sectors = {str(s.value): s for s in Sectors}
self.initial['secondary_sectors'] = [s.value for s in self.initial.get('secondary_sectors', [])]

def clean_secondary_sectors(self):
primary_sector = str(self.cleaned_data['primary_sector'].value)
return [
self.secondary_sectors[v]
for v in self.cleaned_data['secondary_sectors']
if (
v in self.secondary_sectors and
v != primary_sector
)
]

def save(self, commit=True):
return super().save(commit=commit)
32 changes: 32 additions & 0 deletions deployments/migrations/0014_auto_20191004_0646.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 2.0.12 on 2019-10-04 06:46

import deployments.models
import django.contrib.postgres.fields
from django.db import migrations
import enumfields.fields


class Migration(migrations.Migration):

dependencies = [
('deployments', '0013_auto_20190722_1410'),
]

operations = [
migrations.RenameField(
model_name='project',
old_name='sector',
new_name='primary_sector',
),
migrations.AddField(
model_name='project',
name='operation_type',
field=enumfields.fields.EnumIntegerField(default=0, enum=deployments.models.OperationTypes),
preserve_default=False,
),
migrations.AddField(
model_name='project',
name='secondary_sectors',
field=django.contrib.postgres.fields.ArrayField(base_field=enumfields.fields.EnumIntegerField(enum=deployments.models.Sectors), blank=True, default=list, size=None),
),
]
25 changes: 21 additions & 4 deletions deployments/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.db import models
from django.conf import settings
from datetime import datetime
from enumfields import EnumIntegerField
from enumfields import IntEnum

from django.db import models
from django.conf import settings
from django.contrib.postgres.fields import ArrayField

from api.models import District, Country, Region, Event, DisasterType, Appeal
from datetime import datetime, timezone, timedelta

DATE_FORMAT = '%Y/%m/%d %H:%M'

Expand Down Expand Up @@ -128,10 +131,12 @@ class PartnerSocietyDeployment(DeployedPerson):
def __str__(self):
return '%s deployment in %s' % (self.parent_society, self.country_deployed_to)


class ProgrammeTypes(IntEnum):
BILATERAL = 0
MULTILATERAL = 1


class Sectors(IntEnum):
WASH = 0
PGI = 1
Expand All @@ -142,18 +147,30 @@ class Sectors(IntEnum):
SHELTER = 6
PREPAREDNESS = 7


class Statuses(IntEnum):
PLANNED = 0
ONGOING = 1
COMPLETED = 2


class OperationTypes(IntEnum):
LONG_TERM_OPERATION = 0
EMERGENCY_OPERATION = 1


class Project(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL) # user who created this project
reporting_ns = models.ForeignKey(Country, on_delete=models.CASCADE) # this is the national society that is reporting the project
project_district = models.ForeignKey(District, on_delete=models.CASCADE) # this is the district where the project is actually taking place
name = models.TextField()
programme_type = EnumIntegerField(ProgrammeTypes)
sector = EnumIntegerField(Sectors)
primary_sector = EnumIntegerField(Sectors)
secondary_sectors = ArrayField(
EnumIntegerField(Sectors),
default=list, blank=True,
)
operation_type = EnumIntegerField(OperationTypes)
start_date = models.DateField()
end_date = models.DateField()
budget_amount = models.IntegerField()
Expand Down
4 changes: 2 additions & 2 deletions deployments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RdrtPerson,
PartnerSocietyActivities,
PartnerSocietyDeployment,
Project
Project,
)
from api.serializers import (
ListEventSerializer,
Expand Down Expand Up @@ -71,4 +71,4 @@ class Meta:
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = '__all__'
fields = '__all__'
11 changes: 8 additions & 3 deletions deployments/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def setUp(self):
,project_district = district1
,name = 'aaa'
,programme_type = 0
,sector = 0
,primary_sector = 0
,operation_type = 0
,start_date = '2011-11-11'
,end_date = '2011-11-11'
,budget_amount = 6000
Expand All @@ -44,7 +45,9 @@ def setUp(self):
,project_district = district2
,name = 'bbb'
,programme_type = 1
,sector = 1
,primary_sector = 1
,secondary_sectors= [1, 2]
,operation_type = 0
,start_date = '2012-12-12'
,end_date = '2013-01-01'
,budget_amount = 3000
Expand Down Expand Up @@ -86,7 +89,9 @@ def test_2(self):
'project_district' : district2.id,
'name' : 'CreateMePls',
'programme_type' : 0,
'sector' : 0,
'primary_sector' : 0,
'secondary_sectors': [0, 1],
'operation_type' : 0,
'start_date' : '2012-11-12',
'end_date' : '2013-11-13',
'budget_amount' : 7000,
Expand Down
3 changes: 2 additions & 1 deletion main/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
export PYTHONUNBUFFERED=1
set -e
cmd="$@"

Expand All @@ -22,4 +23,4 @@ until postgres_ready; do
done

>&2 echo "Postgres is up - continuing..."
exec $cmd
exec $cmd

0 comments on commit cb2a42e

Please sign in to comment.