Skip to content

Commit ac0eb8b

Browse files
authored
Merge pull request #4109 from rtibbles/public_api_channels
Update Kolibri public channel export to kolibri_public to be more robust
2 parents 1b4361e + dc3ff63 commit ac0eb8b

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ migrate:
3838
# 4) Remove the management command from this `deploy-migrate` recipe
3939
# 5) Repeat!
4040
deploy-migrate:
41-
echo "Nothing to do here"
41+
python contentcuration/manage.py export_channels_to_kolibri_public
4242

4343
contentnodegc:
4444
python contentcuration/manage.py garbage_collect

contentcuration/kolibri_public/management/commands/export_channels_to_kolibri_public.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
import os
33
import shutil
44
import tempfile
5+
from datetime import datetime
6+
from datetime import timedelta
57

68
from django.conf import settings
79
from django.core.files.storage import default_storage as storage
810
from django.core.management import call_command
911
from django.core.management.base import BaseCommand
12+
from django.db.models import F
13+
from django.db.models import Q
14+
from django.utils import timezone
1015
from kolibri_content.apps import KolibriContentConfig
1116
from kolibri_content.models import ChannelMetadata as ExportedChannelMetadata
1217
from kolibri_content.router import get_active_content_database
@@ -15,12 +20,37 @@
1520
from kolibri_public.utils.mapper import ChannelMapper
1621

1722
from contentcuration.models import Channel
23+
from contentcuration.models import User
24+
from contentcuration.utils.publish import create_content_database
1825

1926
logger = logging.getLogger(__file__)
2027

2128

2229
class Command(BaseCommand):
2330

31+
def _republish_problem_channels(self):
32+
twenty_19 = datetime(year=2019, month=1, day=1)
33+
five_minutes = timedelta(minutes=5)
34+
try:
35+
chef_user = User.objects.get(email="chef@learningequality.org")
36+
except User.DoesNotExist:
37+
logger.error("Could not find chef user to republish channels")
38+
return
39+
channel_qs = Channel.objects.filter(
40+
public=True,
41+
deleted=False,
42+
main_tree__published=True
43+
).filter(
44+
Q(last_published__isnull=True) |
45+
Q(last_published__lt=twenty_19, main_tree__modified__lte=F("last_published") + five_minutes)
46+
)
47+
48+
for channel in channel_qs:
49+
kolibri_temp_db = create_content_database(channel, True, chef_user.id, False)
50+
os.remove(kolibri_temp_db)
51+
channel.last_published = timezone.now()
52+
channel.save()
53+
2454
def _export_channel(self, channel_id):
2555
logger.info("Putting channel {} into kolibri_public".format(channel_id))
2656
db_location = os.path.join(settings.DB_ROOT, "{id}.sqlite3".format(id=channel_id))
@@ -37,6 +67,7 @@ def _export_channel(self, channel_id):
3767
mapper.run()
3868

3969
def handle(self, *args, **options):
70+
self._republish_problem_channels()
4071
public_channel_ids = set(Channel.objects.filter(public=True, deleted=False, main_tree__published=True).values_list("id", flat=True))
4172
kolibri_public_channel_ids = set(ChannelMetadata.objects.all().values_list("id", flat=True))
4273
ids_to_export = public_channel_ids.difference(kolibri_public_channel_ids)
@@ -47,4 +78,6 @@ def handle(self, *args, **options):
4778
count += 1
4879
except FileNotFoundError:
4980
logger.warning("Tried to export channel {} to kolibri_public but its published channel database could not be found".format(channel_id))
81+
except Exception as e:
82+
logger.exception("Failed to export channel {} to kolibri_public because of error: {}".format(channel_id, e))
5083
logger.info("Successfully put {} channels into kolibri_public".format(count))

0 commit comments

Comments
 (0)