Skip to content
This repository was archived by the owner on Dec 5, 2023. It is now read-only.

Commit 2f1daba

Browse files
Merge branch 'develop'
2 parents 0dc6e57 + ab8ec36 commit 2f1daba

File tree

14 files changed

+122
-9
lines changed

14 files changed

+122
-9
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,17 @@ After installing requirements (via pip) within your environment or virtualenv:
6464
* `python manage.py reload_imports`
6565

6666
At this point, the basic country and language datasets will be populated but without many optional fields or extra data.
67+
68+
### Updating the `/exports/langnames.json` and `/exports/langnames_short.json` endpoints
69+
70+
When languages are added or updated, run this command to update the data locally:
71+
72+
```bash
73+
python manage.py rebuild_langnames
74+
```
75+
76+
Switch to the master branch and run this command to update the data on the server:
77+
78+
```bash
79+
ec run web python manage.py rebuild_langnames
80+
```

readme-dev.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Also, the instructions assume you are using PyCharm or IntelliJ IDEA.
77

88
### Ubuntu prerequisites
99

10-
sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk
10+
sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk libpq-dev
1111

1212

1313
### Install python versions
@@ -91,7 +91,8 @@ Now, restart the service: `sudo service postgresql restart`.
9191

9292
Run this script to initialize the database with a dump from the production server:
9393

94-
ec run db --instance=primary -- pg_dump --no-owner --no-acl | ./manage.py dbshell
94+
curl $(ec postgres dump) > db.dump
95+
pg_restore --no-owner --no-acl --verbose -d td db.dump
9596

9697
If you get timeout errors running the above command, this is an alternative that has worked:
9798

@@ -120,6 +121,14 @@ If you get timeout errors running the above command, this is an alternative that
120121
* PostgreSQL repository: `deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main`
121122

122123

124+
### Deploying
125+
126+
1. Switch to the develop branch (for td-demo) or the master branch (for td).
127+
2. Make sure changes have been pushed to Github.
128+
3. Run this command:
129+
```ec deploy```
130+
131+
123132
### Upgrade PostgreSQL 9.3 to 9.5 on Ubuntu 14.04
124133

125134
This was required because the `publishing_publishrequest` contains a `jsonb` field, a new type that was introduced in PostgreSQL 9.4.
@@ -144,4 +153,3 @@ wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key a
144153
sudo apt-get update
145154
sudo apt-get install postgresql postgresql-contrib
146155
```
147-

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Pillow==3.0.0
2727
pinax-invitations==4.0.4
2828
pinax-theme-bootstrap==7.2.0
2929
pinax-types==0.10.0
30-
psycopg2==2.6.1
30+
psycopg2==2.7.4
3131
python-dateutil==2.4.2
3232
pytz==2015.7
3333
redis==2.10.5

td/api/urls.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from django.conf.urls import url
22

3-
from .views import QuestionnaireView, templanguages_json, lang_assignment_json, lang_assignment_changed_json
3+
from .views import (
4+
celerybeat_healthz,
5+
lang_assignment_changed_json,
6+
lang_assignment_json,
7+
QuestionnaireView,
8+
templanguages_json
9+
)
410

511

612
urlpatterns = [
713
url(r"^questionnaire/$", QuestionnaireView.as_view(), name="questionnaire"),
814
url(r"^templanguages/$", templanguages_json, name="templanguages"),
915
url(r"^templanguages/assignment/$", lang_assignment_json, name="templanguages_assignment"),
1016
url(r"^templanguages/assignment/changed/$", lang_assignment_changed_json, name="templanguages_changed"),
17+
url(r"^celerybeat/healthz/$", celerybeat_healthz),
1118
]

td/api/views.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import json
22
import logging
33

4+
from django.conf import settings
45
from django.http import JsonResponse
56
from django.views.decorators.csrf import csrf_exempt
67
from django.views.generic import View
78

9+
from djcelery.models import PeriodicTask
10+
811
from td.models import TempLanguage, Country
912
from td.resources.models import Questionnaire
1013

@@ -107,3 +110,35 @@ def lang_assignment_json(request):
107110

108111
def lang_assignment_changed_json(request):
109112
return JsonResponse(TempLanguage.lang_assigned_changed_map(), safe=False)
113+
114+
115+
def celerybeat_healthz(request):
116+
if request.GET.get("key") != settings.CELERYBEAT_HEALTHZ_AUTH_KEY:
117+
return JsonResponse({"message": "Not authorized."}, status=403)
118+
119+
succesful_tasks = []
120+
failing_tasks = []
121+
122+
past_sixty_seconds = -60
123+
for task in PeriodicTask.objects.filter(enabled=True):
124+
# retrieve the estimated number of seconds until the next time the task should be ran
125+
seconds_until_next_execution = task.schedule.remaining_estimate(task.last_run_at).total_seconds()
126+
127+
if seconds_until_next_execution > past_sixty_seconds:
128+
succesful_tasks.append(task.name)
129+
else:
130+
# the task should have been scheduled already
131+
failing_tasks.append(task.name)
132+
133+
healthy = True
134+
status_code = 200
135+
if failing_tasks:
136+
healthy = False
137+
status_code = 503
138+
139+
data = {
140+
"healthy": healthy,
141+
"failing": failing_tasks,
142+
"succesful": succesful_tasks,
143+
}
144+
return JsonResponse(data, status=status_code)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.core.management.base import BaseCommand
2+
3+
from td.tasks import update_langnames_data
4+
5+
6+
class Command(BaseCommand):
7+
help = "rebuild langnames and langnames_short in the table td_jsondata"
8+
9+
def handle(self, *args, **options):
10+
update_langnames_data()

td/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def names_text(cls):
438438
def names_data(cls, short=False):
439439
languages = cls.objects.all().order_by("code")
440440
if short:
441-
data = [dict(pk=x.pk, lc=x.lc, ln=x.ln, ang=x.ang, lr=x.lr) for x in languages]
441+
data = [dict(pk=x.pk, lc=x.lc, ln=x.ln, ang=x.ang, lr=x.lr, hc=x.cc) for x in languages]
442442
else:
443443
# Filter out languages that have pending or rejected temporary language
444444
# NOTE: Can this be simplified or turned into a list comprehension?
@@ -450,7 +450,7 @@ def names_data(cls, short=False):
450450
x = lang
451451
if x:
452452
data.append(dict(pk=x.pk, lc=x.lc, ln=x.ln, ang=x.ang, alt=x.alt_name_all, cc=x.cc_all, lr=x.lr,
453-
gw=x.gateway_flag, ld=x.get_direction_display()))
453+
gw=x.gateway_flag, ld=x.get_direction_display(), hc=x.cc))
454454
return data
455455

456456
@classmethod

td/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@
256256
CELERY_TASK_SERIALIZER = "json"
257257
CELERY_ACCEPT_CONTENT = ['json', 'msgpack', 'yaml'] # for security reasons, don't allow pickle
258258
CELERY_RESULT_BACKEND = "redis://"
259+
CELERYBEAT_HEALTHZ_AUTH_KEY = os.environ.get("CELERYBEAT_HEALTHZ_AUTH_KEY", "celerybeat-healthz")
259260

260261
REST_FRAMEWORK = {
261262
# Use Django's standard `django.contrib.auth` permissions,

td/tasks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def update_langnames_data():
7777
langnames.data = Language.names_data()
7878
langnames.save()
7979

80+
shorter, created = JSONData.objects.get_or_create(name="langnames_short")
81+
shorter.data = Language.names_data(True)
82+
shorter.save()
83+
8084

8185
@task()
8286
def reset_langnames_cache(short=False):

td/tests/test_models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,11 @@ def test_names_data_short(self):
319319
self.assertIn("ln", result[0])
320320
self.assertIn("ang", result[0])
321321
self.assertIn("lr", result[0])
322+
self.assertIn("hc", result[0])
322323
self.assertNotIn("alt", result[0])
323324
self.assertNotIn("ld", result[0])
324325
self.assertNotIn("gw", result[0])
326+
self.assertNotIn("cc", result[0])
325327
self.assertEqual(result[0].get("lc"), "tl")
326328
self.assertEqual(result[0].get("ln"), "Test Language")
327329

@@ -331,6 +333,12 @@ def test_names_data(self):
331333
"""
332334
result = Language.names_data()
333335
self.assertEqual(len(result), 1)
336+
self.assertIn("pk", result[0])
337+
self.assertIn("lc", result[0])
338+
self.assertIn("ln", result[0])
339+
self.assertIn("ang", result[0])
340+
self.assertIn("lr", result[0])
341+
self.assertIn("hc", result[0])
334342
self.assertIn("alt", result[0])
335343
self.assertIn("ld", result[0])
336344
self.assertIn("gw", result[0])

0 commit comments

Comments
 (0)