Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-taggit - display all tags based on date vlog published

Using django-taggit-templatetags2, I can display all the tags associated for a test vlog in a template page.

I have vlogs stored in the db that are not yet released to the public (only displayed after a certain date), so that I can store numerous vlog details in the db and then automatically release each individual vlog on a certain day (say Tuesday of each week).

This means that the django-taggit-templatetags2 display all code of {% for tag in vlog_tags %} will include tags for vlog entries that are not yet displayed to the user in the vlog but are stored in the db.

How can I only display the tags and the count for vlog entries where the vlog_date_published is not greater than now? There may be the same tags that are used for published and not-yet published vlog entries. So this should be factored in to the display of the tag.

Here is my models code:

from taggit.managers import TaggableManager

class VlogDetails(models.Model):
    ....
    vlog_date_published = models.DateField(null=False, blank=False, default=datetime.now, help_text='The date the vlog video will be made public.')
    vlog_tags = TaggableManager(blank=True, help_text='To make a new tag, add a comma after the new tag name.')
    ....

Here is the display all tags template code:

    {% load taggit_templatetags2_tags %}

    {% get_taglist as vlog_tags %}

    {% for tag in vlog_tags %}
        {% if tag.num_times > 0 %}
            <a class='u-tags-v1 g-color-grey g-bg-grey-opacity-0_1 g-bg-grey--hover g-color-white--hover g-rounded-50 g-py-4 g-px-15' href="{% url 'vlog_tag' tag %}" hreflang="en" rel="tooltip" title="{% blocktrans %}Display all vlog entries containing this tag.{% endblocktrans %}"><i class="fa fa-tag icon_padding"></i> {{tag}} x {{tag.num_times}}</a>
        {% endif %}
    {% endfor %}

EDIT

Here are the screen shots from the database referring to the tag that is displayed to the user even though the vlog is not yet displayed to the user, but is stored in the db waiting to be automatically published based on the vlog_date_published > now.

In this scenario, the tag of Employment NDA should not be displayed to the user because the vlog entry that is using that tag is not yet displayed to the user as the vlog_date_published is greater than today (at the time of this post).

vlogdetails table (id = 24): enter image description here

taggit_taggeditem table (id=191 - FK's 24 & 123): enter image description here

taggit_tag table (id=123): enter image description here

ANOTHER EDIT

So in this scenario, the following tag of Employment NDA should not be displayed as it belongs to the vlog details that has not yet been published / displayed to the public, while all the other tags should be displayed (as the vlog details that all the other tags belong to have been published):

enter image description here

like image 767
user1261774 Avatar asked Nov 08 '22 12:11

user1261774


1 Answers

This can be achieved by creating two custom template tags to replace the {{tag}} and the {{tag.num_times}} values from django-taggit-templatetags2.

The conditional custom tag of {% if tag.id|vlog_tag_display %} will only display the tag if the related vlog published date is GT now and {{tag.num_times}} will be replaced by the custom tag of {{tag|vlog_tag_count}} as shown below.

First add the following code to your template page. This will loop through all the tags, but only display the tag if the vlog has been published:

{% load customised_template_tags i18n taggit_templatetags2_tags %}
{% get_taglist as tags %}

{% for tag in tags %}
    {% if tag.num_times > 0 %}
        {# only display the tag if the vlog is published #}
        {% if tag.id|vlog_tag_display %}
                <a class='u-tags-v1 g-color-grey g-bg-grey-opacity-0_1 g-bg-grey--hover g-color-white--hover g-rounded-50 g-py-4 g-px-15' href="{% url 'vlog_tag' tag %}" hreflang="en" rel="tooltip" title="{% blocktrans %}Display all vlog entries containing this tag.{% endblocktrans %}"><i class="fa fa-tag icon_padding"></i> {{tag}} x {{tag|vlog_tag_count}}</a>
        {% endif %}
    {% endif %}
{% empty %}
    {# No Tags recorded. #}
    <br /><b>{% trans "No Tags Recorded." %}</b><br />
{% endfor %}

Next add the following code to your custom template tags page. Here is a link to set up the custom template tags page if this is a new experience for you:

from zoodal.core.models import VlogDetails

@register.filter(name='vlog_tag_count')
def vlog_tag_count(value):
    date_now = timezone.now()
    count_vlog_tag = VlogDetails.objects.filter(vlog_date_published__lte=date_now, tags__name=value).count()
    """ Only count the tag if the vlog entry is published. """
    return count_vlog_tag


@register.filter(name='vlog_tag_display')
def vlog_tag_display(value):
    date_now = timezone.now()
    display_vlog_tag = VlogDetails.objects.filter(vlog_date_published__lte=date_now, tags__id=value)
    """ Only display the tag if the vlog entry is published. """
    if display_vlog_tag:
        return True
    else:
        return False
like image 57
user3354539 Avatar answered Nov 15 '22 05:11

user3354539