22import os
33import shutil
44import tempfile
5+ from datetime import datetime
6+ from datetime import timedelta
57
68from django .conf import settings
79from django .core .files .storage import default_storage as storage
810from django .core .management import call_command
911from 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
1015from kolibri_content .apps import KolibriContentConfig
1116from kolibri_content .models import ChannelMetadata as ExportedChannelMetadata
1217from kolibri_content .router import get_active_content_database
1520from kolibri_public .utils .mapper import ChannelMapper
1621
1722from contentcuration .models import Channel
23+ from contentcuration .models import User
24+ from contentcuration .utils .publish import create_content_database
1825
1926logger = logging .getLogger (__file__ )
2027
2128
2229class 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