Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Fave/block tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Madison Scott-Clary committed Nov 17, 2016
1 parent 23ce18a commit ce0fb16
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 21 deletions.
6 changes: 5 additions & 1 deletion submissions/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db.models import Q


def filters_for_authenticated_user(reader):
def filters_for_authenticated_user(reader, blocked_tags=True):
"""Gets submission filters for an authenticated user.
Args:
Expand All @@ -25,6 +25,10 @@ def filters_for_authenticated_user(reader):
(Q(restricted_to_groups=True) &
Q(allowed_groups__in=reader.friendgroup_set.all())))

# Filter out blocked tags if we've been asked
if blocked_tags:
query &= ~Q(tags__in=reader.profile.blocked_tags.all())

# Shortcut to allow authors all access
query = Q(owner=reader) | query
return query
Expand Down
2 changes: 1 addition & 1 deletion submissions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def view_submission(request, username=None, submission_id=None,
author = submission.owner
try:
submission = Submission.objects.get(Q(id=submission_id) & (
filters_for_authenticated_user(reader) if
filters_for_authenticated_user(reader, blocked_tags=False) if
reader.is_authenticated else filters_for_anonymous_user()))
except Submission.DoesNotExist:
# XXX Perhaps we should distinguish between 403 and 404 at some point
Expand Down
67 changes: 67 additions & 0 deletions tags/templates/list_submissions_with_favorite_tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{% extends 'base.html' %}
{% load tag_extras %}

{% block content %}
<div class="row">
<div class="col-md-8 col-md-offset-2 tag-cloud">
<h2><small>Your favorite tags</small></h2>
{% get_weighted_tags user.profile.favorite_tags as weighted_tags %}
{% for tag in weighted_tags %}
<a href="{% url 'tags:view_tag' tag_slug=tag.slug %}" style="font-size:calc(7px * {{ tag.weight }});">{{ tag }}</a>
{% endfor %}
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<p>
<em>Showing results {{ submissions.start_index }} through {{ submissions.end_index }} of {{ submissions.paginator.count }}</em>
</p>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
{% for submission in submissions %}
<div class="row striped-item">
<div class="col-md-12">
{% include 'submission-list-snippet.html' with submission=submission author=submission.owner %}
</div>
</div>
{% endfor %}
</div>
</div>
{% if submissions.has_next or submissions.has_previous %}
<div class="row">
<div class="col-md-12 text-center">
<nav aria-label="Submission pages">
<ul class="pagination">
{% if submissions.has_previous %}
<li>
<a href="{{ url_prefix }}page/{{ submissions.previous_page_number }}/" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
{% else %}
<li class="disabled"><span aria-hidden="true">&laquo;</span></li>
{% endif %}
{% for page in submissions.paginator.page_range %}
{% if submissions.number == page %}
<li class="active"><a href="{{ url_prefix }}page/{{ page }}/">{{ page }} <span class="sr-only">(current)</span></a></li>
{% else %}
<li><a href="{{ url_prefix }}page/{{ page }}/">{{ page }}</a></li>
{% endif %}
{% endfor %}
{% if submissions.has_next %}
<li>
<a href="{{ url_prefix }}page/{{ submissions.next_page_number }}/" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
{% else %}
<li class="disabled"><span aria-hidden="true">&raquo;</span></li>
{% endif %}
</ul>
</nav>
</div>
</div>
{% endif %}
{% endblock %}
47 changes: 45 additions & 2 deletions tags/templates/view_tag.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h2>Tag flagged <small>{{ active_flag.subject }}</small></h2>
{% endif %}
<div class="row">
<div class="col-md-8 col-md-offset-2">
<p>
<div class="pull-left">
<em>Showing results {{ submissions.start_index }} through {{ submissions.end_index }} of {{ submissions.paginator.count }}</em>
{% if not active_flag %}
<span class="pull-right">
Expand All @@ -25,7 +25,50 @@ <h2>Tag flagged <small>{{ active_flag.subject }}</small></h2>
</a>
</span>
{% endif %}
</p>
</div>
{% if user.is_authenticated %}
<div class="pull-right">
{% if tag not in user.profile.favorite_tags.all %}
{% if tag not in user.profile.blocked_tags.all %}
<form method="post" class="inline" action="{% url 'tags:favorite_tag' tag_slug=tag.slug %}">
{% csrf_token %}
<button class="btn btn-success" type="submit">
<span class="glyphicon glyphicon-plus"></span>
Favorite tag
</button>
</form>
{% endif %}
{% else %}
<form method="post" class="inline" action="{% url 'tags:unfavorite_tag' tag_slug=tag.slug %}">
{% csrf_token %}
<button class="btn btn-warning" type="submit">
<span class="glyphicon glyphicon-minus"></span>
Unfavorite tag
</button>
</form>
{% endif %}
{% if tag not in user.profile.blocked_tags.all %}
{% if tag not in user.profile.favorite_tags.all %}
<form method="post" class="inline" action="{% url 'tags:block_tag' tag_slug=tag.slug %}">
{% csrf_token %}
<button class="btn btn-danger" type="submit">
<span class="glyphicon glyphicon-ban-circle"></span>
Block tag
</button>
</form>
{% endif %}
{% else %}
<form method="post" class="inline" action="{% url 'tags:unblock_tag' tag_slug=tag.slug %}">
{% csrf_token %}
<button class="btn btn-warning" type="submit">
<span class="glyphicon glyphicon-ok-circle"></span>
Unblock tag
</button>
</form>
{% endif %}
</div>
{% endif %}
<p class="clearfix">&nbsp;</p>
</div>
</div>
<div class="row">
Expand Down
5 changes: 4 additions & 1 deletion tags/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
url('^favorite/', views.favorite_tag, name='favorite_tag'),
url('^unfavorite', views.unfavorite_tag, name='unfavorite_tag'),
url('^block/', views.block_tag, name='block_tag'),
url('^unblock/', views.block_tag, name='block_tag'),
url('^unblock/', views.unblock_tag, name='unblock_tag'),
]
tags_views = [
url('^$', views.list_tags, name='list_tags'),
url('^favorites/$', views.list_submissions_with_favorite_tags,
name='list_submissions_with_favorite_tags'),
url('^favorites/(?P<page>\d+)/$',
views.list_submissions_with_favorite_tags,
name='list_submissions_with_favorite_tags'),
url('^tag/(?P<tag_slug>[-\w]+)/', include(tag_views)),
]
categories_views = [
Expand Down
83 changes: 67 additions & 16 deletions tags/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from django.core.paginator import (
EmptyPage,
Paginator,
)
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.shortcuts import (
get_object_or_404,
redirect,
render,
)
from django.views.decorators.http import require_POST
from taggit.models import Tag

from administration.models import Flag
Expand Down Expand Up @@ -75,35 +79,82 @@ def view_tag(request, tag_slug=None, page=1):


@login_required
@require_POST
def favorite_tag(request, tag_slug=None):
# TODO
# @makyo 2016-11-06 #61
pass
tag = get_object_or_404(Tag, slug=tag_slug)
if tag in request.user.profile.favorite_tags.all():
messages.warning(request, "You have already favorited that tag")
else:
request.user.profile.favorite_tags.add(tag)
messages.success(request, "Tag favorited; submissions tagged with {} "
"will show up in the list of submissions with your "
"favorited tags".format(tag.name))
return redirect(reverse('tags:view_tag', kwargs={'tag_slug': tag.slug}))


@login_required
@require_POST
def unfavorite_tag(request, tag_slug=None):
# TODO
# @makyo 2016-11-06 #61
pass
tag = get_object_or_404(Tag, slug=tag_slug)
if tag not in request.user.profile.favorite_tags.all():
messages.warning(request, "You haven't favorited that tag")
else:
request.user.profile.favorite_tags.remove(tag)
messages.success(request, "Tag unfavorited; submissions tagged with "
"{} will no longershow up in the list of "
"submissions with your favorited "
"tags".format(tag.name))
return redirect(reverse('tags:view_tag', kwargs={'tag_slug': tag.slug}))


@login_required
def list_submissions_with_favorite_tags(request):
# TODO
# @makyo 2016-11-06 #61
pass
def list_submissions_with_favorite_tags(request, page=1):
if request.user.profile.favorite_tags.count() == 0:
messages.warning(request, "You must favorite some tags before you "
"can view this page!")
return redirect(reverse('tags:list_tags'))

# Filter submissions visible to the reader
filters = filters_for_authenticated_user(request.user) if \
request.user.is_authenticated else filters_for_anonymous_user()
results = Submission.objects.filter(
Q(tags__in=request.user.profile.favorite_tags.all()) & filters)
paginator = Paginator(results, request.user.profile.results_per_page if
request.user.is_authenticated else 25)
try:
submissions = paginator.page(page)
except EmptyPage:
submissions = paginator.page(paginator.num_pages)
return render(request, 'list_submissions_with_favorite_tags.html', {
'title': 'Submissions with your favorite tags',
'submissions': submissions,
})


@login_required
@require_POST
def block_tag(request, tag_slug=None):
# TODO
# @makyo 2016-11-06 #61
pass
tag = get_object_or_404(Tag, slug=tag_slug)
if tag in request.user.profile.blocked_tags.all():
messages.warning(request, "You have already blocked that tag")
else:
request.user.profile.blocked_tags.add(tag)
messages.success(request, "Tag blocked. You will no longer see "
"submissions tagged with {} in lists of submissions "
"(but will still see them if linked "
"directly).".format(tag.name))
return redirect(reverse('tags:view_tag', kwargs={'tag_slug': tag.slug}))


@login_required
@require_POST
def unblock_tag(request, tag_slug=None):
# TODO
# @makyo 2016-11-06 #61
pass
tag = get_object_or_404(Tag, slug=tag_slug)
if tag not in request.user.profile.blocked_tags.all():
messages.warning(request, "You haven't blocked that tag")
else:
request.user.profile.blocked_tags.remove(tag)
messages.success(request, "Tag unblocked. You will now see "
"submissions tagged with {} in lists of "
"submissions".format(tag.name))
return redirect(reverse('tags:view_tag', kwargs={'tag_slug': tag.slug}))
26 changes: 26 additions & 0 deletions usermgmt/migrations/0006_auto_20161117_0338.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-11-17 03:38
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('taggit', '0002_auto_20150616_2121'),
('usermgmt', '0005_auto_20161111_0247'),
]

operations = [
migrations.AddField(
model_name='profile',
name='blocked_tags',
field=models.ManyToManyField(related_name='blocked_by', to='taggit.Tag'),
),
migrations.AddField(
model_name='profile',
name='favorite_tags',
field=models.ManyToManyField(related_name='favorited_by', to='taggit.Tag'),
),
]
5 changes: 5 additions & 0 deletions usermgmt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.html import strip_tags
from taggit.models import Tag

from .group_models import FriendGroup
from administration.models import Flag
Expand Down Expand Up @@ -37,6 +38,10 @@ class Profile(models.Model):
# (favorite genre, editor, etc)
attributes = models.TextField(blank=True)

# Content filter settings
favorite_tags = models.ManyToManyField(Tag, related_name='favorited_by')
blocked_tags = models.ManyToManyField(Tag, related_name='blocked_by')

# Additional settings
banned = models.BooleanField(default=False)
flags = GenericRelation(Flag)
Expand Down

0 comments on commit ce0fb16

Please sign in to comment.