Esempio n. 1
0
    def get_context_data(self, **kwargs):
        context = super(DocumentDetailView, self).get_context_data(**kwargs)

        doc = self.object

        context['work'] = doc.work
        context['work_json'] = json.dumps(
            WorkSerializer(instance=doc.work,
                           context={
                               'request': self.request
                           }).data)
        context['country'] = Country.for_work(doc.work)
        context['locality'] = context['country'].work_locality(doc.work)

        # TODO do this in a better place
        context['countries'] = Country.objects.select_related(
            'country').prefetch_related('locality_set', 'publication_set',
                                        'country').all()
        context['countries_json'] = json.dumps(
            {c.code: c.as_json()
             for c in context['countries']})

        context['document_content_json'] = json.dumps(doc.document_xml)

        serializer = WorkSerializer(context={'request': self.request},
                                    many=True)
        works = Work.objects.filter(country=doc.country)
        context['works_json'] = json.dumps(serializer.to_representation(works))

        context['amendments_json'] = json.dumps(
            WorkAmendmentSerializer(context={
                'request': self.request
            },
                                    many=True).to_representation(
                                        doc.work.amendments))

        context['form'] = DocumentForm(instance=doc)
        context['countries'] = Country.objects.select_related(
            'country').prefetch_related('locality_set', 'publication_set',
                                        'country').all()
        context['countries_json'] = json.dumps(
            {c.code: c.as_json()
             for c in context['countries']})
        context['subtypes'] = Subtype.objects.order_by('name').all()
        context['languages'] = Language.objects.select_related(
            'language').all()

        serializer = DocumentListSerializer(context={'request': self.request})
        context['documents_json'] = json.dumps(
            serializer.to_representation(DocumentViewSet.queryset.all()))

        return context
Esempio n. 2
0
    def get_context_data(self, **kwargs):
        context = super(PlaceDetailView, self).get_context_data(**kwargs)

        serializer = WorkSerializer(context={'request': self.request},
                                    many=True)
        works = WorkViewSet.queryset.filter(country=self.country,
                                            locality=self.locality)
        context['works_json'] = json.dumps(serializer.to_representation(works))

        serializer = DocumentSerializer(context={'request': self.request},
                                        many=True)
        docs = DocumentViewSet.queryset.filter(work__country=self.country,
                                               work__locality=self.locality)
        context['documents_json'] = json.dumps(
            serializer.to_representation(docs))

        # map from document id to count of open annotations
        annotations = Annotation.objects.values('document_id')\
            .filter(closed=False)\
            .filter(document__deleted=False)\
            .annotate(n_annotations=Count('document_id'))\
            .filter(document__work__country=self.country)
        if self.locality:
            annotations = annotations.filter(
                document__work__locality=self.locality)

        annotations = {
            x['document_id']: {
                'n_annotations': x['n_annotations']
            }
            for x in annotations
        }
        context['annotations_json'] = json.dumps(annotations)

        # tasks for place
        tasks = Task.objects.filter(work__country=self.country,
                                    work__locality=self.locality)

        # tasks counts per state and per work
        work_tasks = tasks.values('work_id',
                                  'state').annotate(n_tasks=Count('work_id'))
        task_states = defaultdict(dict)
        for row in work_tasks:
            task_states[row['work_id']][row['state']] = row['n_tasks']

        # summarise task counts per work
        work_tasks = {}
        for work_id, states in task_states.iteritems():
            work_tasks[work_id] = {
                'n_%s_tasks' % s: states.get(s, 0)
                for s in Task.STATES
            }
            work_tasks[work_id]['n_tasks'] = sum(states.itervalues())
        context['work_tasks_json'] = json.dumps(work_tasks)

        # tasks counts per state and per document
        doc_tasks = tasks.values(
            'document_id', 'state').annotate(n_tasks=Count('document_id'))
        task_states = defaultdict(dict)
        for row in doc_tasks:
            task_states[row['document_id']][row['state']] = row['n_tasks']

        # summarise task counts per document
        document_tasks = {}
        for doc_id, states in task_states.iteritems():
            document_tasks[doc_id] = {
                'n_%s_tasks' % s: states.get(s, 0)
                for s in Task.STATES
            }
            document_tasks[doc_id]['n_tasks'] = sum(states.itervalues())
        context['document_tasks_json'] = json.dumps(document_tasks)

        # summarise amendments per work
        amendments = Amendment.objects.values('amended_work_id')\
            .filter(amended_work_id__in=[w.id for w in works])\
            .annotate(n_amendments=Count('pk'))\
            .order_by()\
            .all()

        amendments = {
            a['amended_work_id']: {
                'n_amendments': a['n_amendments']
            }
            for a in amendments
        }
        context['work_n_amendments_json'] = json.dumps(amendments)

        return context