def handle(self, **options): models = [Course, Library, CommunityItem] filters = dict(http_status__in=(None, 200)) if not options["update_existing"]: filters["screenshot"] = None total_items = sum([model.objects.filter(**filters).count() for model in models]) print "Updating screenshots for %i items..." % total_items cnt = 0 for model in models: for object_id in model.objects.filter(**filters).values_list("id", flat=True): try: item = None try: item = model.objects.get(pk=object_id) except model.DoesNotExist: pass if item: item._post_save_processed = True if not item.http_status: check_url_status(item) update_screenshot(item) except: if settings.DEBUG: print "Exception while processing %s %i" % (model._meta.object_name, object_id) print traceback.format_exc() cnt += 1 print "%i of %i..." % (cnt, total_items) print "Done!"
def run(): from materials.models import Course from materials.tasks import update_screenshot item = Course.objects.filter(http_status=200)[0] update_screenshot(item)
def handle(self, **options): from materials.models import CommunityItem from materials.models.course import Course from materials.models.library import Library from materials.tasks import update_screenshot print "Updating screenshots..." for model in (Course, Library, CommunityItem): for item in model.objects.filter(http_status=200).filter( models.Q(screenshot=None) | models.Q(screenshot=u"") ): update_screenshot(item) print "Done!"
def handle(self, **options): from materials.models.material import PUBLISHED_STATE from materials.models import CommunityItem from materials.models.course import Course from materials.models.library import Library from authoring.models import AuthoredMaterial from materials.tasks import update_screenshot for model in (Course, Library, CommunityItem): for item in model.objects.filter(http_status=200).filter( models.Q(screenshot=None) | models.Q(screenshot=u"") ): update_screenshot(item) for item in AuthoredMaterial.objects.filter(workflow_state=PUBLISHED_STATE).filter( models.Q(screenshot=None) | models.Q(screenshot=u"") ): update_screenshot(item)
def handle(self, **options): from materials.models import CommunityItem from materials.models.course import Course from materials.models.library import Library from materials.tasks import check_url_status, update_screenshot from utils import update_item models = [Course, Library, CommunityItem] now = datetime.datetime.now() items = [] # Get items that were never fetched for model in models: qs = model.objects.filter(url_fetched_on=None) if qs.exists(): items += list(qs) qs.update(url_fetched_on=now) if not items: # Get the item with the oldest fetch date item = None for model in models: qs = model.objects.exclude(url_fetched_on=None).order_by("url_fetched_on") if not qs.exists(): continue candidate = qs[0] if item is None or candidate.url_fetched_on < item.url_fetched_on: item = candidate if item is not None: update_item(item, url_fetched_on=now) items.append(item) if not items: print "No items to fetch." return for item in items: print "Fetching URL for ", unicode(item) check_url_status(item) update_screenshot(item) print "Done!"