Django-taggit: how to search for all tags based on another column or foreign key? - django-taggit

The django-taggit example shows how to get all tags for one specific model, and I know there is a way to get all tags in the system, but how do I get all tags based on a foreign-key?
I have tags for the Event model, and there is a primary-key/foreign-key relationship between the EventOrganizer and Event. Each EventOrganizer will have different set of tags, and when he/she logs in, I only want to show the tags that this organizer is concerned about.
Thanks!

Assuming your Event model looks something like this:
class Event(models.Model):
organizer = models.ForeignKey(EventOrganizer)
tags = TaggableManager(blank=True)
# ...
You can filter tags by event.organizer:
from taggit.models import Tag
tags = Tag.objects.filter(event__organizer=organizer)

Related

How to put a button on List page for each row in django admin page

I want to add a button for each row as shown in the picture on the button place which shall give a popup of a field content of the same modal or whatever i insert for it to show.
This is actually, quite easy to do :) Although depending upon what you want the button to do, it might get a bit more complicated. You can have anything you want in your list view, just create a method that returns whatever you want to insert, and list it in list_display. For example:
class MyModelAdmin(admin.ModelAdmin):
list_display = (
"post_title",
...
"my_button_field",
)
def my_button_field(self, obj):
# obj is the particular instance of the object representing a particular row
return obj.property
In the case of a button, you'll want to use format_html. This will mark the relevant parts of the string safe, and so that it's displayed as an HTML button, and escape the other parts to protect against various securit issues.
It will look something like this:
from django.utils.html import format_html
class MyModelAdmin(admin.ModelAdmin):
list_display = (
"post_title",
...
"my_button_field",
)
def my_button_field(self, obj):
return format_html(
"<button onclick=`doSomething({})`>{}</button>",
obj.id,
obj.some_property
)
The values of object.id and object.some_property will get inserted into the placeholders.
Of course you'll need to add the javascript for doSomething, there are afew ways to do that, but you can find out more here. If what you want can be achieved by an anchor, it might be easier to use an <a> tag, and that way you won't have to bother with any additional javascrpt.

Fragment id linking in wagtail's rich text content

I have a bunch of content in a Wagtail 2.0 rich text field that looks like
Page heading
(intro blurb)
heading 1
(heading-1-relevant text)
heading 2
(heading-2-relevant text)
...
and I would like to give each heading an id so that any text can be made a link to jump to the relevant content. I can't seem to find an option to give headings an explicit id, and the "link" button in the rich text editor does not seem to let me pick active fragment identifiers in the content.
Is there a way to add fragment identifier based navigation on the same page work with Wagtail's rich text editor?
Revisiting my own question a year later because this is still something we need, the solution we came up with is to simply wrap the RichText html serialization, and putting fragment id injection on top:
import re
from django import template
from django.utils.text import slugify
from wagtail.core.rich_text import RichText
# We'll be wrapping the original RichText.__html__(), so make
# sure we have a reference to it that we can call.
__original__html__ = RichText.__html__
# This matches an h1/.../h6, using a regexp that is only
# guaranteed to work because we know that the source of
# the HTML code we'll be working with generates nice
# and predictable HTML code (and note the non-greedy
# "one or more" for the heading content).
heading_re = r"<h([1-6])([^>]*)>(.+?)</h\1>"
def add_id_attribute(match):
"""
This is a regexp replacement function that takes
in the above regex match results, and then turns:
<h1>some text</h1>
Into:
<h1><a id="some-text"></a>some text</h1>
where the id attribute value is generated by running
the heading text through Django's slugify() function.
"""
n = match.group(1)
attributes= match.group(2)
text_content = match.group(3)
id = slugify(text_content)
return f'<h{n}{attributes}><a id="{id}"></a>{text_content}</h{n}>'
def with_heading_ids(self):
"""
We don't actually change how RichText.__html__ works, we just replace
it with a function that does "whatever it already did", plus a
substitution pass that adds fragment ids and their associated link
elements to any headings that might be in the rich text content.
"""
html = __original__html__(self)
return re.sub(heading_re, add_id_attribute, html)
# Rebind the RichText's html serialization function such that
# the output is still entirely functional as far as wagtail
# can tell, except with headings enriched with fragment ids.
RichText.__html__ = with_heading_ids
This works rather well, does not require any hacking in draftail or wagtail, and is very easy to enable/disable simply by loading this code as part of the server startup process (we have it living in our wagtailcustom_tags.py file, so when Django loads up all template tag sets, the RichText "enrichment" kicks in automatically).
We had initially tried to extend the ... | richtext template filter, but while that's entirely possible, that only works for custom blocks we ourselves wrote, with our own custom templates, and so turned out to not be a solution given the idea that it should "just work".
To have control over the structure of your page body, it's preferable to encourage users to use heading blocks, rather than headings within the rich text block. Then you can have a heading block type which has two fields, a 'text' and an 'id', and you can specify a template that outputs the h element with the id attribute.
class Heading2Block(blocks.StructBlock):
heading = blocks.CharBlock(classname='full title')
link_id = blocks.CharBlock(help_text='For making hyperlinks to this heading')
class Meta:
template = 'blocks/h2.html'
Put the following in blocks/h2.html:
<h1{% if value.link_id %} id="{{ value.link_id|slugify }}"{% endif %}>{{ value.heading }}</h1>
In earlier versions of Wagtail it was possible to remove the h widget from the Hallo.js rich text editor, and this was a good way of encouraging user adoption of the heading block. Similar restriction is not currently present in Draftail, but there is a pull request which reimplements it.

How to structure recusrive categories in Angular, perhaps using ui-router?

I'm a beginner Angular guy and I need some help structuring an application. The part I'm having difficulty with is a categorization system.
The Category model has the fields: title, slug, parent category, image, type. Categories can be of simple type (title only, no image) or full (title and image). Other category types should be possible to create in the future - for example, category with lengthy description and no children. Categories can be automatically expanded (a simple category with subcategories has its children expanded by default) or not (a full category by default is not expanded).
Visually, it should look like the following: a category has a title, and inside it the images and titles of subcategories, like apps look on your phone's home screen:
https://s3.amazonaws.com/ish-archive/2016/bjjcollective/20160215.bjjc-uiux.png
When you click one of those "app"-looking things, that subcategory's title gets added to the hierarchy of titles (so the categorization acts as a tree), and the subsubcategories are shown, as app-looking things.
When you click on the title of a parent directory, the children tree gets replaced with the expanded view of that category's subcategories.
Additionally, each category should have a url associated with it, for example: /categories/houses/kitchen/silverware ( /categories/:slug_0/:slug_1 or /categories/*slugs ? ) should be the page where silverware category is expanded, and its subcategories are shown. The slugs are not unique! So you can have a path like /categories/camping/kitchen/silverware as well, which would be different from "houses" silverware.
The backend API can have any shape that's usable by the frontend. /api/categories.json, /api/categories/houses.json?depth=2, /api/category/houses.json - it's completely flexible.
So I am looking into ui-router and directives to make this happen but I don't see how I should structure my code. To tie views to the url I have to use ui-router, not directives, right? Is recursive views a thing in Angular? And, if each category has a sub-view, how do I restrict rendering of subcategories to only the one relevant subview? I have a pretty solid idea how to do it in jQuery, but how do you do it in Angular?

Angular Schema Form HTML Attributes?

I've been looking over the Angular Schema Form documentation to be able to apply attributes to the elements generated at: https://github.com/Textalk/angular-schema-form/blob/development/docs/index.md
Have been looking up and down and have found nothing. I see through defining the schema that you can define custom HTML classes:
htmlClass: "street foobar", // CSS Class(es) to be added to the container div
fieldHtmlClass: "street" // CSS Class(es) to be added to field input (or similar)
labelHtmlClass: "street" // CSS Class(es) to be added to the label of the field (or similar)
But, haven't been able to find where I can apply attributes like the data attribute or an attribute specific to the element itself. Any resources in regards to this type of basic functionality you'd expect from form generation?
Any ideas?
Thanks!
I do not think this is possible to do purely through json schema definitions, you would have to extend Schema Form and provide your own HTML template with your custom attributes. I've had this problem myself and have had to do the very same.
I'm sure you've seen this but here's the docs on extending:
https://github.com/json-schema-form/angular-schema-form/blob/master/docs/extending.md

Get Object from ID in VisualForce rerendered page

I am working with visualforce. Is there a way to get the fieldname from the id in the rerendered section of the visualforce page.
{!$CurrentPage.parameters.cid} gives me the id. Suppose that cid has the id of the contact object. How can i display certain fields of the contact object in the outputpanel. I tried the but it gave whole detail. I only want certain fields.
Hope you are referencing this documentation in your work. $CurrentPage.parameters.parameter_name is a way of getting query string parameters in Visuaforce and there is nothing to do with that for altering the fields you need in the Contact section.
If you are using <apex:detail /> for the Contact section, what you can do is update your Contact object detail page layout as you need.

Resources