def serialize_work(work, viewer=None, full=False): data = { 'id': work.id, 'title': work.title, } if work.image_filename: data['image_url'] = 'https://animeta.net/media/' + work.image_filename if full: data['alt_titles'] = list(TitleMapping.objects.filter(work=work) .exclude(title=work.title) .values_list('title', flat=True)) data['episodes'] = get_episodes(work) try: data['record_count'] = work.index.record_count data['rank'] = work.index.rank except WorkIndex.DoesNotExist: data['record_count'] = work.record_set.count() if viewer and viewer.is_authenticated(): try: data['record'] = serialize_record(viewer.record_set.get(work=work)) except Record.DoesNotExist: pass metadata = work.metadata if metadata: period = Period.parse(metadata['periods'][0]) data['metadata'] = item_json(work, metadata, period) return data
def fill_image_filename(apps, schema_editor): Work = apps.get_model('work', 'Work') for work in Work.objects.exclude(raw_metadata=None): if work.image_filename or not work.raw_metadata: continue metadata = yaml.load(work.raw_metadata) if metadata: period = Period.parse(metadata['periods'][0]) work.image_filename = '%s/images/thumb/%s' % (period, metadata['image']) work.save()
def get(self, request, period): period = Period.parse(period) cache_key = "table:%s" % period only_first_period = request.GET.get("only_first_period") == "true" if only_first_period: cache_key += ":first_only" data = cache.get(cache_key) if data is None: indexes = WorkPeriodIndex.objects.select_related("work", "work__index").filter(period=str(period)) if only_first_period: indexes = indexes.exclude(is_first_period=False) data = [serialize_work(index.work) for index in indexes] cache.set(cache_key, data, 60 * 60) if request.user.is_authenticated(): records = {} work_ids = [work["id"] for work in data] for record in request.user.record_set.filter(work_id__in=work_ids): records[record.work_id] = serialize_record(record) for work in data: if work["id"] in records: work["record"] = records[work["id"]] return data