def get_context_data(self, **kwargs): context = super(DocumentDetailView, self).get_context_data(**kwargs) doc = self.object context['work_json'] = json.dumps( WorkSerializer(instance=doc.work, context={ 'request': self.request }).data) context['work'] = doc.work context['work_json'] = json.dumps( WorkSerializer(instance=doc.work, context={ 'request': self.request }).data) context['document_json'] = json.dumps( DocumentSerializer(instance=doc, context={ 'request': self.request }).data) # expressions context['expressions_json'] = json.dumps( DocumentSerializer(context={ 'request': self.request }, many=True).to_representation(doc.work.expressions().all())) context['country'] = doc.work.country context['locality'] = doc.work.locality # TODO do this in a better place context['countries'] = Country.objects.select_related( 'country').prefetch_related('localities', 'publication_set', 'country').all() context['document_content_json'] = json.dumps(doc.document_xml) context['amendments_json'] = json.dumps( WorkAmendmentSerializer(context={ 'request': self.request }, many=True).to_representation( doc.work.amendments)) context['form'] = DocumentForm(instance=doc) context['subtypes'] = Subtype.objects.order_by('name').all() context['download_formats'] = [{ 'url': reverse('document-detail', kwargs={ 'pk': doc.id, 'format': r.format }) + getattr(r, 'suffix', ''), 'icon': r.icon, 'title': r.title } for r in DocumentViewSet.renderer_classes if hasattr(r, 'icon')] context['download_formats'].sort(key=lambda f: f['title']) return context
def get_context_data(self, **kwargs): context = super(DocumentDetailView, self).get_context_data(**kwargs) doc = self.object context['work_json'] = json.dumps(WorkSerializer(instance=doc.work, context={'request': self.request}).data) context['work'] = doc.work context['work_json'] = json.dumps(WorkSerializer(instance=doc.work, context={'request': self.request}).data) context['document_json'] = json.dumps(DocumentSerializer(instance=doc, context={'request': self.request}).data) # expressions context['expressions_json'] = json.dumps( DocumentSerializer(context={'request': self.request}, many=True) .to_representation( doc.work.expressions().all() )) context['country'] = doc.work.country context['locality'] = context['country'].work_locality(doc.work) # TODO do this in a better place context['countries'] = Country.objects.select_related('country').prefetch_related('localities', '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) context['amendments_json'] = json.dumps( WorkAmendmentSerializer(context={'request': self.request}, many=True) .to_representation(doc.work.amendments)) context['form'] = DocumentForm(instance=doc) context['subtypes'] = Subtype.objects.order_by('name').all() return context
def get_context_data(self, **kwargs): context = super(TaskEditView, self).get_context_data(**kwargs) work = None task = self.object if task.work: work = json.dumps( WorkSerializer(instance=task.work, context={ 'request': self.request }).data) context['work_json'] = work document = None if task.document: document = json.dumps( DocumentSerializer(instance=task.document, context={ 'request': self.request }).data) context['document_json'] = document context['task_labels'] = TaskLabel.objects.all() context['place_workflows'] = self.place.workflows.filter(closed=False) if has_transition_perm(task.cancel, self): context['cancel_task_permission'] = True return context
def get_context_data(self, *args, **kwargs): context = super(TaskCreateView, self).get_context_data(**kwargs) task = context['form'].instance work = None if task.work: work = json.dumps( WorkSerializer(instance=task.work, context={ 'request': self.request }).data) context['work_json'] = work document = None if task.document: document = json.dumps( DocumentSerializer(instance=task.document, context={ 'request': self.request }).data) context['document_json'] = document context['task_labels'] = TaskLabel.objects.all() context['place_workflows'] = self.place.workflows.filter(closed=False) return context
def get_context_data(self, **kwargs): context = super(WorkViewBase, self).get_context_data(work=self.work, **kwargs) context['work_json'] = json.dumps( WorkSerializer(instance=self.work, context={ 'request': self.request }).data) # add other dates to timeline work_timeline = self.work.points_in_time() other_dates = [('assent_date', self.work.assent_date), ('commencement_date', self.work.commencement_date), ('repealed_date', self.work.repealed_date)] # add to existing events (e.g. if publication and commencement dates are the same) for entry in work_timeline: for name, date in other_dates: if entry['date'] == date: entry[name] = True # add new events (e.g. if assent is before any of the other events) existing_dates = [entry['date'] for entry in work_timeline] for name, date in other_dates: if date and date not in existing_dates: work_timeline.append({ 'date': date, name: True, }) context['work_timeline'] = sorted(work_timeline, key=lambda k: k['date'], reverse=True) return context
def get_context_data(self, **kwargs): context = super(AbstractWorkDetailView, self).get_context_data(**kwargs) is_new = not self.work.frbr_uri context['work_json'] = {} if is_new else json.dumps( WorkSerializer(instance=self.work, context={ 'request': self.request }).data) context['country'] = self.work.country context['locality'] = None if is_new else context[ 'country'].work_locality(self.work) # TODO do this in a better place context['countries'] = Country.objects.select_related( 'country').prefetch_related('localities', '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() return context
def get_context_data(self, country_code, **kwargs): context = super(LibraryView, self).get_context_data(**kwargs) country = Country.for_code(country_code) context['country'] = country context['country_code'] = country_code context['countries'] = Country.objects.select_related( 'country').prefetch_related('localities', 'publication_set', 'country').all() context['countries_json'] = json.dumps( {c.code: c.as_json() for c in context['countries']}) serializer = DocumentSerializer(context={'request': self.request}, many=True) docs = DocumentViewSet.queryset.filter(work__country=country) context['documents_json'] = json.dumps( serializer.to_representation(docs)) serializer = WorkSerializer(context={'request': self.request}, many=True) works = WorkViewSet.queryset.filter(country=country) context['works_json'] = json.dumps(serializer.to_representation(works)) return context
def get_context_data(self, **kwargs): context = super(WorkViewBase, self).get_context_data(work=self.work, **kwargs) context['work_json'] = json.dumps( WorkSerializer(instance=self.work, context={ 'request': self.request }).data) return context
def get_context_data(self, **kwargs): context = super(TaskDetailView, self).get_context_data(**kwargs) task = self.object # merge actions and comments actions = task.action_object_actions.all() task_content_type = ContentType.objects.get_for_model(self.model) comments = list(Comment.objects\ .filter(content_type=task_content_type, object_pk=task.id)\ .select_related('user')) # get the annotation for the particular task try: task_annotation = task.annotation except Annotation.DoesNotExist: task_annotation = None # for the annotation that is linked to the task, get all the replies if task_annotation: # get the replies to the annotation annotation_replies = Annotation.objects.filter(in_reply_to=task_annotation)\ .select_related('created_by_user') comments.extend([ Comment(user=a.created_by_user, comment=a.text, submit_date=a.created_at) for a in annotation_replies ]) context['task_timeline'] = sorted( chain(comments, actions), key=lambda x: x.submit_date if hasattr(x, 'comment') else x.timestamp) context['possible_workflows'] = Workflow.objects.unclosed().filter( country=task.country, locality=task.locality).all() # warn when submitting task on behalf of another user Task.decorate_submission_message([task], self) Task.decorate_potential_assignees([task], self.country) Task.decorate_permissions([task], self) # add work to context if task.work: context['work'] = task.work context['work_json'] = json.dumps( WorkSerializer(instance=task.work, context={ 'request': self.request }).data) return context
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
def get_taxonomies(self, doc): from indigo_api.serializers import WorkSerializer return WorkSerializer().get_taxonomies(doc.work)
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['document_json'] = json.dumps( DocumentSerializer(instance=doc, context={ 'request': self.request }).data) # expressions context['expressions_json'] = json.dumps( DocumentSerializer(context={ 'request': self.request }, many=True).to_representation(doc.work.expressions().all())) context['comparison_expressions'] = doc.work.expressions().filter( language=doc.language).order_by('-expression_date') context['place'] = doc.work.place context['country'] = doc.work.country context['locality'] = doc.work.locality # TODO do this in a better place context['countries'] = Country.objects.select_related( 'country').prefetch_related('localities', 'publication_set', 'country').all() context['document_content_json'] = json.dumps(doc.document_xml) # add 'numbered_title_localised' to each amendment amendments = WorkAmendmentSerializer(context={'request': self.request}, many=True)\ .to_representation(doc.work.amendments) plugin = plugins.for_document('work-detail', doc) if plugin: for a in amendments: amending_work = Work.objects.get( frbr_uri=a['amending_work']['frbr_uri']) a['amending_work'][ 'numbered_title_localised'] = plugin.work_numbered_title( amending_work) context['amendments_json'] = json.dumps(amendments) context['form'] = DocumentForm(instance=doc) context['subtypes'] = Subtype.objects.order_by('name').all() context['user_can_edit'] = ( self.request.user.is_authenticated and self.request.user.has_perm('indigo_api.change_document') and self.request.user.editor.has_country_permission(doc.work.country)) context['download_formats'] = [{ 'url': reverse('document-detail', kwargs={ 'pk': doc.id, 'format': r.format }) + getattr(r, 'suffix', ''), 'icon': r.icon, 'title': r.title } for r in DocumentViewSet.renderer_classes if hasattr(r, 'icon')] context['download_formats'].sort(key=lambda f: f['title']) return context