def render_search_row(context, item): template_from_fedora = FedoraTemplateCache.get_template_string(item, 'search_row') context = Context(context) context['item'] = item if template_from_fedora: return Template(template_from_fedora).render(context) chosen_template = select_template(['fedoralink_ui/search_result_row.html']) return chosen_template.template.render(context)
def get(self, request, *args, **kwargs): self.object = self.get_object() # noinspection PyTypeChecker template = FedoraTemplateCache.get_template_string(self.object, view_type='view') print("Got template", template) if template: context = self.get_context_data(object=self.object) return HttpResponse( Template("{% extends '" + self.template_name + "' %}" + template).render( RequestContext(request, context))) return super(GenericDetailView, self).get(request, *args, **kwargs)
def render_search_row(context, item): if not getattr(item._meta, 'fields'): #TODO: If not fields => does not have valid rdf_types. return '' if 'item_template' in context: template_name = context['item_template'] else: template_name = 'fedoralink_ui/search_result_row.html' template_from_fedora = FedoraTemplateCache.get_template_string(item, 'search_row') context = Context(context) context['item'] = item if template_from_fedora: return Template(template_from_fedora).render(context) chosen_template = select_template([template_name]) return chosen_template.template.render(context)
def render_to_response(self, context, **response_kwargs): """ Returns a response, using the `response_class` for this view, with a template rendered with the given context. If any keyword arguments are provided, they will be passed to the constructor of the response class. """ model = get_subcollection_model(self.kwargs.get('id'), fedora_prefix=self.fedora_prefix) template = FedoraTemplateCache.get_template_string(self.object if self.object else model, view_type='create') if template: return HttpResponse( Template("{% extends '" + self.template_name + "' %}" + template).render( RequestContext(self.request, context))) return super().render_to_response(context, **response_kwargs)
def render_to_response(self, context, **response_kwargs): """ Returns a response, using the `response_class` for this view, with a template rendered with the given context. If any keyword arguments are provided, they will be passed to the constructor of the response class. """ model = get_subcollection_model(self.kwargs.get('id'), fedora_prefix=self.fedora_prefix) template = FedoraTemplateCache.get_template_string( self.object if self.object else model, view_type='create') if template: return HttpResponse( Template("{% extends '" + self.template_name + "' %}" + template).render(RequestContext( self.request, context))) return super().render_to_response(context, **response_kwargs)
def get(self, request, *args, **kwargs): self.object = self.get_object() if (FEDORA.Binary in self.object.types): bitstream = self.object.get_bitstream() resp = FileResponse(bitstream.stream, content_type=bitstream.mimetype) resp[ 'Content-Disposition'] = 'inline; filename="%s"' % bitstream.filename return resp # noinspection PyTypeChecker template = FedoraTemplateCache.get_template_string(self.object, view_type='view') if template: context = self.get_context_data(object=self.object) return HttpResponse( Template("{% extends '" + self.template_name + "' %}" + template).render(RequestContext(request, context))) return super(GenericDetailView, self).get(request, *args, **kwargs)
def render_to_response(self, context, **response_kwargs): """ Returns a response, using the `response_class` for this view, with a template rendered with the given context. If any keyword arguments are provided, they will be passed to the constructor of the response class. """ self.object = self.get_object() form = self.get_form() print("media", form.media) context = self.get_context_data(object=self.object, form=form, **response_kwargs) # noinspection PyTypeChecker template = FedoraTemplateCache.get_template_string(self.object, view_type='edit') if template: return HttpResponse( Template("{% extends '" + self.template_name + "' %}" + template).render( RequestContext(self.request, context))) return super().render_to_response(context, **response_kwargs)
def render_to_response(self, context, **response_kwargs): """ Returns a response, using the `response_class` for this view, with a template rendered with the given context. If any keyword arguments are provided, they will be passed to the constructor of the response class. """ self.object = self.get_object() form = self.get_form() # print("media", form.media) context = self.get_context_data(object=self.object, form=form, **response_kwargs) # noinspection PyTypeChecker template = FedoraTemplateCache.get_template_string(self.object, view_type='edit') if template: return HttpResponse( Template("{% extends '" + self.template_name + "' %}" + template).render(RequestContext( self.request, context))) return super().render_to_response(context, **response_kwargs)
def _get(self, request, *args, **kwargs): if not self.model_class: if self.request.user.is_authenticated(): credentials = Credentials(self.request.user.username, USERS_TOMCAT_PASSWORD) # print("user:" + credentials.username) with as_user(credentials): model = get_subcollection_model( collection_id=kwargs['collection_id'], fedora_prefix=self.fedora_prefix) else: model = get_subcollection_model( collection_id=kwargs['collection_id'], fedora_prefix=self.fedora_prefix) else: model = self.model_class if self.facets and callable(self.facets): requested_facets = self.facets(request) else: requested_facets = self.facets within_collection = None if 'path' in kwargs: within_collection = FedoraObject.objects.get(pk=kwargs['path']) requested_facet_ids = [x[0] for x in requested_facets] if self.initial_queryset: data = self.initial_queryset(request) else: data = model.objects.all() if requested_facets: data = data.request_facets(*requested_facet_ids) if 'searchstring' in request.GET: search_string = request.GET['searchstring'].strip() if search_string: q = None flds = self.search_fields if callable(flds): flds = flds(search_string) for fld in flds: q1 = Q(**{fld: search_string}) if q: q |= q1 else: q = q1 data = data.filter(q) else: search_string = None for k in request.GET: if k.startswith('facet__'): values = request.GET.getlist(k) k = k[len('facet__'):] q = None for v in values: if not q: q = Q(**{k: v}) else: q |= Q(**{k: v}) if q: data = data.filter(q) if within_collection: data = data.filter(_fedora_parent__exact=within_collection.id) current_language = get_language() if not search_string: sort = request.GET.get('sort', self.orderings[0][0]) if sort: data = data.order_by(*[ x.strip().replace('@lang', '@' + current_language) for x in sort.split(',') ]) else: sort = '' page = request.GET.get('page', ) paginator = Paginator(data, 10) try: page = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. page = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. page = paginator.page(paginator.num_pages) template = None if 'path' in kwargs: template = FedoraTemplateCache.get_template_string( within_collection, 'search') if not template: template = get_template(self.template_name) # have facets in the same order as in requested facets def find_facet_index(facet): for i, f in enumerate(requested_facets): if f[0] == facet[0]: return i return 10000 facet_values = data.facets facet_values.sort(key=find_facet_index) context = { 'page': page, 'data': data, 'facets': facet_values, 'item_template': self.list_item_template, 'facet_names': {k[0]: k[1] for k in requested_facets}, 'facet_params': {k[0]: k[2] for k in requested_facets if len(k) > 2}, 'searchstring': request.GET.get('searchstring', ''), 'orderings': self.orderings, 'ordering': sort, 'title': self.title, 'create_button_title': self.create_button_title, 'fedora_prefix': self.fedora_prefix } return TemplateResponse(request, template, context)
def get(self, request, *args, **kwargs): model = get_model(collection_id = kwargs['collection_id'], fedora_prefix=self.fedora_prefix) if self.facets and callable(self.facets): requested_facets = self.facets(request) else: requested_facets = self.facets within_collection = None if 'path' in kwargs: within_collection = FedoraObject.objects.get(pk=kwargs['path']) requested_facet_ids = [x[0] for x in requested_facets] data = model.objects.all() if requested_facets: data = data.request_facets(*requested_facet_ids) if 'searchstring' in request.GET and request.GET['searchstring'].strip(): q = None for fld in self.search_fields: q1 = Q(**{fld.replace('@', '__') + "__fulltext": request.GET['searchstring'].strip()}) if q: q |= q1 else: q = q1 data = data.filter(q) for k in request.GET: if k.startswith('facet__'): values = request.GET.getlist(k) k = k[len('facet__'):] q = None for v in values: if not q: q = Q(**{k: v}) else: q |= Q(**{k: v}) if q: data = data.filter(q) if within_collection: data = data.filter(_fedora_parent__exact=within_collection.id) sort = request.GET.get('sort', self.orderings[0][0]) if sort: data = data.order_by(*[x.strip() for x in sort.split(',')]) page = request.GET.get('page', ) paginator = Paginator(data, 10) try: page = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. page = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. page = paginator.page(paginator.num_pages) template = None if 'path' in kwargs: template = FedoraTemplateCache.get_template_string(within_collection, 'search') if not template: template = get_template(self.template_name) context = RequestContext(request, { 'page': page, 'data': data, 'item_template': self.list_item_template, 'facet_names': {k: v for k, v in requested_facets}, 'searchstring': request.GET.get('searchstring', ''), 'orderings': self.orderings, 'ordering': sort, 'title': self.title, 'create_button_title': self.create_button_title, 'fedora_prefix': self.fedora_prefix }) return TemplateResponse(request, template, context)