Skip to content

Commit

Permalink
Using cronjob to update project status
Browse files Browse the repository at this point in the history
- Using pytest as test runner.
  • Loading branch information
thenav56 committed Apr 21, 2021
1 parent f50485d commit 0fedcb8
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 125 deletions.
Empty file.
19 changes: 19 additions & 0 deletions deployments/management/commands/update_project_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.core.management.base import BaseCommand
from django.utils import timezone

from deployments.models import Project, Statuses


class Command(BaseCommand):
help = 'Update project status using start/end date'

def handle(self, *args, **options):
now = timezone.now().date()

for projects, new_status in [
(Project.objects.filter(start_date__gt=now), Statuses.PLANNED),
(Project.objects.filter(start_date__lt=now, end_date__gt=now), Statuses.ONGOING),
(Project.objects.filter(end_date__lt=now), Statuses.COMPLETED),
]:
print(f'Total {str(new_status)} projects: {projects.count()}')
projects.update(status=new_status)
11 changes: 0 additions & 11 deletions deployments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from enumfields import IntEnum

from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.utils.hashable import make_hashable
Expand Down Expand Up @@ -393,16 +392,6 @@ def __str__(self):
postfix = self.reporting_ns.society_name
return '%s (%s)' % (self.name, postfix)

@property
def current_status_display(self):
# TODO: Update self.status instead using background task after celery is used in production?
now = timezone.now().date()
if now < self.start_date:
return Statuses.PLANNED
if now > self.end_date:
return Statuses.COMPLETED
return Statuses.ONGOING

def get_secondary_sectors_display(self):
choices_dict = dict(make_hashable(SectorTags.choices()))
return [
Expand Down
1 change: 0 additions & 1 deletion deployments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ class ProjectSerializer(EnumSupportSerializerMixin, ModelSerializer):
operation_type_display = serializers.CharField(source='get_operation_type_display', read_only=True)
status_display = serializers.CharField(source='get_status_display', read_only=True)
secondary_sectors_display = serializers.ListField(source='get_secondary_sectors_display', read_only=True)
current_status_display = serializers.CharField(read_only=True)

class Meta:
model = Project
Expand Down
202 changes: 98 additions & 104 deletions deployments/snapshots/snap_tests.py

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions deployments/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import datetime
from unittest import mock
from django.core import management

from modeltranslation.utils import build_localized_fieldname
from django.conf import settings
Expand Down Expand Up @@ -374,23 +375,28 @@ def test_regional_project_get(self):
'links': sorted(resp['links'], key=lambda item: dict_to_string(item)),
})

@mock.patch('deployments.models.timezone')
def test_project_current_status(self, mock_timezone):
def test_project_current_status(self):
Project.objects.all().delete()
project = ProjectFactory.create(
start_date=datetime.date(2012, 11, 12),
end_date=datetime.date(2012, 12, 13),
status=Statuses.PLANNED.value,
)
self.authenticate()

patcher = mock.patch('django.utils.timezone.now')
mock_timezone_now = patcher.start()
for now, current_status in [
(datetime.date(2011, 11, 11), Statuses.PLANNED),
(datetime.date(2012, 11, 15), Statuses.ONGOING),
(datetime.date(2012, 12, 14), Statuses.COMPLETED),
]:
mock_timezone.now.return_value.date.return_value = now
mock_timezone_now.return_value.date.return_value = now
management.call_command('update_project_status')
response = self.client.get(f'/api/v2/project/{project.id}/')
self.assert_200(response)
self.assertEqual(response.data['current_status_display'], str(current_status))
self.assertEqual(response.data['status_display'], str(current_status))
patcher.stop()


class TranslationTest(APITestCase):
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ services:

test:
<<: *base_django_setup
command: python manage.py test -k
command: pytest --durations=10

test_snapshot_update:
<<: *base_django_setup
Expand Down
1 change: 0 additions & 1 deletion lang/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.conf import settings
from django.core import management
from django.contrib.auth import get_permission_codename
from django.contrib.auth.models import User, Permission
from main.test_case import APITestCase

Expand Down
1 change: 1 addition & 0 deletions main/runserver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' > $HOME/.env
(crontab -l 2>/dev/null; echo '10 2 * * * . /home/ifrc/.env; python /home/ifrc/go-api/manage.py scrape_pdfs >> /home/ifrc/logs/scrape_pdfs.log 2>&1') | crontab -
(crontab -l 2>/dev/null; echo '30 1 * * * . /home/ifrc/.env; python /home/ifrc/go-api/manage.py ingest_databank >> /home/ifrc/logs/ingest_databank.log 2>&1') | crontab -
(crontab -l 2>/dev/null; echo '*/5 * * * * . /home/ifrc/.env; python /home/ifrc/go-api/manage.py sync_molnix >> /home/ifrc/logs/sync_molnix.log 2>&1') | crontab -
(crontab -l 2>/dev/null; echo '* */6 * * * . /home/ifrc/.env; python /home/ifrc/go-api/manage.py update_project_status >> /home/ifrc/logs/update_project_status.log 2>&1') | crontab -
service cron start

tail -n 0 -f $HOME/logs/*.log &
Expand Down
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
DJANGO_SETTINGS_MODULE = main.settings
log_cli = true
python_files = test*.py *_tests.py
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ djangorestframework-csv==2.1.0
djangorestframework-guardian==0.1.1
djangorestframework==3.11.2
elasticsearch==6.0.0
factory_boy==2.12.0
futures==3.1.1
fuzzywuzzy==0.17.0
graphene-django==2.0.0
Expand All @@ -69,6 +70,8 @@ promise==2.1
psycopg2==2.7.3.2
pycountry==19.8.18
pycparser==2.19
pydash==4.8.0
pytest-django==4.2.0
python-Levenshtein==0.12.0
python-dateutil==2.8.0
python-mimeparse==1.6.0
Expand All @@ -79,6 +82,7 @@ requests==2.21.0
setuptools==45
singledispatch==3.4.0.3
six==1.12.0
snapshottest==0.5.1
tabula-py==1.2.0
typing==3.6.2
unicodecsv==0.14.1
Expand All @@ -87,6 +91,3 @@ urllib3==1.24.2
urllib3==1.24.2
wheel==0.30.0
xmltodict==0.11.0
snapshottest==0.5.1
factory_boy==2.12.0
pydash==4.8.0

0 comments on commit 0fedcb8

Please sign in to comment.