def email_link(request): initial_data = { 'link_text': request.GET.get('link_text', ''), 'email_address': request.GET.get('link_url', ''), } if request.method == 'POST': form = EmailLinkChooserForm(request.POST, initial=initial_data) if form.is_valid(): result = { 'url': 'mailto:' + form.cleaned_data['email_address'], 'title': form.cleaned_data['link_text'].strip() or form.cleaned_data['email_address'], # If the user has explicitly entered / edited something in the link_text field, # always use that text. If not, we should favour keeping the existing link/selection # text, where applicable. 'prefer_this_title_as_link_text': ('link_text' in form.changed_data), } return render_modal_workflow( request, None, 'tuiuiuadmin/chooser/external_link_chosen.js', { 'result_json': json.dumps(result), }) else: form = EmailLinkChooserForm(initial=initial_data) return render_modal_workflow(request, 'tuiuiuadmin/chooser/email_link.html', 'tuiuiuadmin/chooser/email_link.js', shared_context(request, { 'form': form, }))
def chooser_upload(request): Document = get_document_model() DocumentForm = get_document_form(Document) if request.method == 'POST': document = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=document, user=request.user) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed search_index.insert_or_update_object(document) return render_modal_workflow( request, None, 'tuiuiudocs/chooser/document_chosen.js', {'document_json': get_document_json(document)}) else: form = DocumentForm(user=request.user) documents = Document.objects.order_by('title') return render_modal_workflow(request, 'tuiuiudocs/chooser/chooser.html', 'tuiuiudocs/chooser/chooser.js', { 'documents': documents, 'uploadform': form })
def set_privacy(request, page_id): page = get_object_or_404(Page, id=page_id) page_perms = page.permissions_for_user(request.user) if not page_perms.can_set_view_restrictions(): raise PermissionDenied # fetch restriction records in depth order so that ancestors appear first restrictions = page.get_view_restrictions().order_by('page__depth') if restrictions: restriction = restrictions[0] restriction_exists_on_ancestor = (restriction.page != page) else: restriction = None restriction_exists_on_ancestor = False if request.method == 'POST': form = PageViewRestrictionForm(request.POST, instance=restriction) if form.is_valid() and not restriction_exists_on_ancestor: if form.cleaned_data[ 'restriction_type'] == PageViewRestriction.NONE: # remove any existing restriction if restriction: restriction.delete() else: restriction = form.save(commit=False) restriction.page = page form.save() return render_modal_workflow( request, None, 'tuiuiuadmin/page_privacy/set_privacy_done.js', { 'is_public': (form.cleaned_data['restriction_type'] == 'none') }) else: # request is a GET if not restriction_exists_on_ancestor: if restriction: form = PageViewRestrictionForm(instance=restriction) else: # no current view restrictions on this page form = PageViewRestrictionForm( initial={'restriction_type': 'none'}) if restriction_exists_on_ancestor: # display a message indicating that there is a restriction at ancestor level - # do not provide the form for setting up new restrictions return render_modal_workflow( request, 'tuiuiuadmin/page_privacy/ancestor_privacy.html', None, { 'page_with_restriction': restriction.page, }) else: # no restriction set at ancestor level - can set restrictions here return render_modal_workflow( request, 'tuiuiuadmin/page_privacy/set_privacy.html', 'tuiuiuadmin/page_privacy/set_privacy.js', { 'page': page, 'form': form, })
def chooser_select_format(request, image_id): image = get_object_or_404(get_image_model(), id=image_id) if request.method == 'POST': form = ImageInsertionForm(request.POST, initial={'alt_text': image.default_alt_text}) if form.is_valid(): format = get_image_format(form.cleaned_data['format']) preview_image = image.get_rendition(format.filter_spec) image_json = json.dumps({ 'id': image.id, 'title': image.title, 'format': format.name, 'alt': form.cleaned_data['alt_text'], 'class': format.classnames, 'edit_link': reverse('tuiuiuimages:edit', args=(image.id, )), 'preview': { 'url': preview_image.url, 'width': preview_image.width, 'height': preview_image.height, }, 'html': format.image_to_editor_html(image, form.cleaned_data['alt_text']), }) return render_modal_workflow( request, None, 'tuiuiuimages/chooser/image_chosen.js', {'image_json': image_json}) else: initial = {'alt_text': image.default_alt_text} initial.update(request.GET.dict()) form = ImageInsertionForm(initial=initial) return render_modal_workflow(request, 'tuiuiuimages/chooser/select_format.html', 'tuiuiuimages/chooser/select_format.js', { 'image': image, 'form': form })
def chooser(request, get_results=False): # Get most popular queries queries = models.Query.get_most_popular() # If searching, filter results by query string query_string = None if 'q' in request.GET: searchform = SearchForm(request.GET) if searchform.is_valid(): query_string = searchform.cleaned_data['q'] queries = queries.filter( query_string__icontains=normalise_query_string(query_string)) else: searchform = SearchForm() paginator, queries = paginate(request, queries, per_page=10) # Render if get_results: return render(request, "tuiuiusearch/queries/chooser/results.html", { 'queries': queries, }) else: return render_modal_workflow( request, 'tuiuiusearch/queries/chooser/chooser.html', 'tuiuiusearch/queries/chooser/chooser.js', { 'queries': queries, 'searchform': searchform, })
def chooser(request): Document = get_document_model() if permission_policy.user_has_permission(request.user, 'add'): DocumentForm = get_document_form(Document) uploadform = DocumentForm(user=request.user) else: uploadform = None documents = Document.objects.all() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_document_chooser_queryset'): documents = hook(documents, request) q = None if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET: collection_id = request.GET.get('collection_id') if collection_id: documents = documents.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] documents = documents.search(q) is_searching = True else: documents = documents.order_by('-created_at') is_searching = False # Pagination paginator, documents = paginate(request, documents, per_page=10) return render( request, "tuiuiudocs/chooser/results.html", { 'documents': documents, 'query_string': q, 'is_searching': is_searching, }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None documents = documents.order_by('-created_at') paginator, documents = paginate(request, documents, per_page=10) return render_modal_workflow( request, 'tuiuiudocs/chooser/chooser.html', 'tuiuiudocs/chooser/chooser.js', { 'documents': documents, 'uploadform': uploadform, 'searchform': searchform, 'collections': collections, 'is_searching': False, })
def chooser_upload(request): Image = get_image_model() ImageForm = get_image_form(Image) searchform = SearchForm() if request.method == 'POST': image = Image(uploaded_by_user=request.user) form = ImageForm(request.POST, request.FILES, instance=image, user=request.user) if form.is_valid(): form.save() # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) if request.GET.get('select_format'): form = ImageInsertionForm( initial={'alt_text': image.default_alt_text}) return render_modal_workflow( request, 'tuiuiuimages/chooser/select_format.html', 'tuiuiuimages/chooser/select_format.js', { 'image': image, 'form': form }) else: # not specifying a format; return the image details now return render_modal_workflow( request, None, 'tuiuiuimages/chooser/image_chosen.js', {'image_json': get_image_json(image)}) else: form = ImageForm(user=request.user) images = Image.objects.order_by('-created_at') paginator, images = paginate(request, images, per_page=12) return render_modal_workflow(request, 'tuiuiuimages/chooser/chooser.html', 'tuiuiuimages/chooser/chooser.js', { 'images': images, 'uploadform': form, 'searchform': searchform })
def choose(request, app_label, model_name): model = get_snippet_model_from_url_params(app_label, model_name) items = model.objects.all() # Preserve the snippet's model-level ordering if specified, but fall back on PK if not # (to ensure pagination is consistent) if not items.ordered: items = items.order_by('pk') # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': model._meta.verbose_name}) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': model._meta.verbose_name}) # Pagination paginator, paginated_items = paginate(request, items, per_page=25) # If paginating or searching, render "results.html" if request.GET.get('results', None) == 'true': return render( request, "tuiuiusnippets/chooser/results.html", { 'model_opts': model._meta, 'items': paginated_items, 'query_string': search_query, 'is_searching': is_searching, }) return render_modal_workflow( request, 'tuiuiusnippets/chooser/choose.html', 'tuiuiusnippets/chooser/choose.js', { 'model_opts': model._meta, 'items': paginated_items, 'is_searchable': is_searchable, 'search_form': search_form, 'query_string': search_query, 'is_searching': is_searching, })
def chooser_upload(request): if request.method == 'POST': form = EmbedForm(request.POST, request.FILES) if form.is_valid(): error = None try: embed_html = embed_to_editor_html(form.cleaned_data['url']) return render_modal_workflow( request, None, 'tuiuiuembeds/chooser/embed_chosen.js', {'embed_html': embed_html} ) except AccessDeniedEmbedlyException: error = _("There seems to be a problem with your embedly API key. Please check your settings.") except EmbedNotFoundException: error = _("Cannot find an embed for this URL.") except EmbedlyException: error = _( "There seems to be an error with Embedly while trying to embed this URL." " Please try again later." ) if error: errors = form._errors.setdefault('url', ErrorList()) errors.append(error) return render_modal_workflow( request, 'tuiuiuembeds/chooser/chooser.html', 'tuiuiuembeds/chooser/chooser.js', { 'form': form, } ) else: form = EmbedForm() return render_modal_workflow(request, 'tuiuiuembeds/chooser/chooser.html', 'tuiuiuembeds/chooser/chooser.js', { 'form': form, })
def external_link(request): initial_data = { 'url': request.GET.get('link_url', ''), 'link_text': request.GET.get('link_text', ''), } if request.method == 'POST': form = ExternalLinkChooserForm(request.POST, initial=initial_data) if form.is_valid(): result = { 'url': form.cleaned_data['url'], 'title': form.cleaned_data['link_text'].strip() or form.cleaned_data['url'], # If the user has explicitly entered / edited something in the link_text field, # always use that text. If not, we should favour keeping the existing link/selection # text, where applicable. # (Normally this will match the link_text passed in the URL here anyhow, # but that won't account for non-text content such as images.) 'prefer_this_title_as_link_text': ('link_text' in form.changed_data), } return render_modal_workflow( request, None, 'tuiuiuadmin/chooser/external_link_chosen.js', { 'result_json': json.dumps(result), }) else: form = ExternalLinkChooserForm(initial=initial_data) return render_modal_workflow(request, 'tuiuiuadmin/chooser/external_link.html', 'tuiuiuadmin/chooser/external_link.js', shared_context(request, { 'form': form, }))
def chosen(request, app_label, model_name, id): model = get_snippet_model_from_url_params(app_label, model_name) item = get_object_or_404(model, id=id) snippet_json = json.dumps({ 'id': item.id, 'string': text_type(item), 'edit_link': reverse('tuiuiusnippets:edit', args=(app_label, model_name, item.id)) }) return render_modal_workflow(request, None, 'tuiuiusnippets/chooser/chosen.js', { 'snippet_json': snippet_json, })
def chooser(request): form = EmbedForm(initial=request.GET.dict()) return render_modal_workflow(request, 'tuiuiuembeds/chooser/chooser.html', 'tuiuiuembeds/chooser/chooser.js', { 'form': form, })
def browse(request, parent_page_id=None): # A missing or empty page_type parameter indicates 'all page types' # (i.e. descendants of tuiuiucore.page) page_type_string = request.GET.get('page_type') or 'tuiuiucore.page' user_perm = request.GET.get('user_perms', False) try: desired_classes = page_models_from_string(page_type_string) except (ValueError, LookupError): raise Http404 # Find parent page if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) elif desired_classes == (Page, ): # Just use the root page parent_page = Page.get_first_root_node() else: # Find the highest common ancestor for the specific classes passed in # In many cases, such as selecting an EventPage under an EventIndex, # this will help the administrator find their page quicker. all_desired_pages = filter_page_type(Page.objects.all(), desired_classes) parent_page = all_desired_pages.first_common_ancestor() # Get children of parent page pages = parent_page.get_children().specific() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_page_chooser_queryset'): pages = hook(pages, request) # Filter them by page type if desired_classes != (Page, ): # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) choosable_pages = filter_page_type(pages, desired_classes) descendable_pages = pages.filter(numchild__gt=0) pages = choosable_pages | descendable_pages can_choose_root = request.GET.get('can_choose_root', False) # Do permission lookups for this user now, instead of for every page. permission_proxy = UserPagePermissionsProxy(request.user) # Parent page can be chosen if it is a instance of desired_classes parent_page.can_choose = can_choose_page(parent_page, permission_proxy, desired_classes, can_choose_root, user_perm) # Pagination # We apply pagination first so we don't need to walk the entire list # in the block below paginator, pages = paginate(request, pages, per_page=25) # Annotate each page with can_choose/can_decend flags for page in pages: page.can_choose = can_choose_page(page, permission_proxy, desired_classes, can_choose_root, user_perm) page.can_descend = page.get_children_count() # Render return render_modal_workflow( request, 'tuiuiuadmin/chooser/browse.html', 'tuiuiuadmin/chooser/browse.js', shared_context( request, { 'parent_page': parent_page, 'parent_page_id': parent_page.pk, 'pages': pages, 'search_form': SearchForm(), 'page_type_string': page_type_string, 'page_type_names': [ desired_class.get_verbose_name() for desired_class in desired_classes ], 'page_types_restricted': (page_type_string != 'tuiuiucore.page') }))
def chooser(request): Image = get_image_model() if permission_policy.user_has_permission(request.user, 'add'): ImageForm = get_image_form(Image) uploadform = ImageForm(user=request.user) else: uploadform = None images = Image.objects.order_by('-created_at') # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_image_chooser_queryset'): images = hook(images, request) q = None if ('q' in request.GET or 'p' in request.GET or 'tag' in request.GET or 'collection_id' in request.GET): # this request is triggered from search, pagination or 'popular tags'; # we will just render the results.html fragment collection_id = request.GET.get('collection_id') if collection_id: images = images.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] images = images.search(q) is_searching = True else: is_searching = False tag_name = request.GET.get('tag') if tag_name: images = images.filter(tags__name=tag_name) # Pagination paginator, images = paginate(request, images, per_page=12) return render( request, "tuiuiuimages/chooser/results.html", { 'images': images, 'is_searching': is_searching, 'query_string': q, 'will_select_format': request.GET.get('select_format') }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None paginator, images = paginate(request, images, per_page=12) return render_modal_workflow( request, 'tuiuiuimages/chooser/chooser.html', 'tuiuiuimages/chooser/chooser.js', { 'images': images, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, 'query_string': q, 'will_select_format': request.GET.get('select_format'), 'popular_tags': popular_tags_for_model(Image), 'collections': collections, })
def image_chosen(request, image_id): image = get_object_or_404(get_image_model(), id=image_id) return render_modal_workflow(request, None, 'tuiuiuimages/chooser/image_chosen.js', {'image_json': get_image_json(image)})
def document_chosen(request, document_id): document = get_object_or_404(get_document_model(), id=document_id) return render_modal_workflow( request, None, 'tuiuiudocs/chooser/document_chosen.js', {'document_json': get_document_json(document)})