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

Commit

Permalink
Flags on all available/pertinent models
Browse files Browse the repository at this point in the history
  • Loading branch information
Madison Scott-Clary committed Nov 12, 2016
1 parent 0289d0b commit 5593689
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ metadata: ## Get metadata about the project (sloc, models, urls)
| grep -v migrations \
| grep -E '(.py|.html|.md|Makefile|sh)' \
| xargs python sloc.py > sloc.tsv
venv/bin/python manage.py graph_models -a -o models.png
venv/bin/python manage.py graph_models -g -a -o models.png
venv/bin/python manage.py show_urls > urls.tsv

.PHONY: clean
Expand Down
21 changes: 17 additions & 4 deletions administration/flag_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,19 @@ def create_flag(request):
),
}, status=403)

# Retrieve the content type and object
# Retrieve the content type, object, and, if possible, the object's owner
parts = request.GET.get('content_type').split(':')
ctype = get_object_or_404(ContentType, app_label=parts[0], model=parts[1])
obj = ctype.get_object_for_this_type(pk=request.GET.get('object_id'))
if hasattr(obj, 'owner'):
owner = obj.owner
elif hasattr(obj, 'user'):
owner = obj.user
else:
owner = None

# Ensure that we can flag the given object
if hasattr(obj, 'owner') and obj.owner == request.user:
if owner == request.user:
return render(request, 'permission_denied.html', {
'title': 'Permission denied',
'additional_error': 'You cannot flag your own objects',
Expand All @@ -112,12 +118,19 @@ def create_flag(request):
flag = form.save(commit=False)
flag.flagged_by = request.user
flag.flagged_object_owner = (request.user if not
hasattr(flag.object_model, 'owner')
else flag.object_model.owner)
owner else owner)
flag.save()
form.save_m2m()
flag.participants.add(request.user)
flag.participants.add(flag.flagged_object_owner)

# Notify the object owner if one exists
if owner != request.user:
Notification(
source=flag.flagged_by,
target=flag.flagged_object_owner,
notification_type=Notification.FLAG_CREATED_AGAINST
).save()
return redirect(flag.get_absolute_url())
return render(request, 'create_flag.html', {
'title': 'Flag {}'.format(ctype.model),
Expand Down
64 changes: 43 additions & 21 deletions core/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,56 @@
</div>
</form>
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li>
<a href="{% url 'usermgmt:view_profile' user.username %}">~{{ user.username }}</a>
</li>
{% else %}
<li>
<a href="{% url 'login' %}">Log in</a>
</li>
<li>
<a href="{% url 'usermgmt:register' %}">Register</a>
</li>
{% endif %}
<li class="dropdown">
{% with notifications=user.profile.get_notifications_counts %}
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Account <span class="badge">{% if notifications.user_notifications or notifications.submission_notifications or notifications.messages %}{{ notifications.user_notifications|add:notifications.submission_notifications|add:notifications.messages|add:notifications.admin_notifications }}{% endif %}</span> <span class="caret"></span></a>
{% with notifications=user.profile.get_notifications_counts %}
{% if user.is_authenticated %}
<li>
<a href="{% url 'usermgmt:view_profile' user.username %}">~{{ user.username }}</a>
</li>
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Admin <span class="badge">{% if notifications.admin_notifications %}{{ notifications.admin_notifications }}{% endif %}</span> <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<a href="{% url 'social:view_notifications_categories' %}#admin">
<span class="glyphicon glyphicon-bishop"></span> Admin notifications
<span class="badge">{% if notifications.admin_notifications %}{{ notifications.admin_notifications }}{% endif %}</span>
</a>
</li>
<li><a href="{% url 'administration:dashboard' %}">
<span class="glyphicon glyphicon-th"></span>
Dashboard
</a></li>
<li><a href="{% url 'administration:list_participating_applications' %}">
<span class="glyphicon glyphicon-list"></span>
My applications
</a></li>
<li><a href="{% url 'administration:list_participating_flags' %}">
<span class="glyphicon glyphicon-list"></span>
My flags
</a></li>
{% if perms.administration.can_list_bans %}
<li><a href="{% url 'administration:list_participating_bans' %}">
<span class="glyphicon glyphicon-list"></span>
My bans
</a></li>
{% endif %}
</ul>
</li>
{% else %}
<li>
<a href="{% url 'login' %}">Log in</a>
</li>
<li>
<a href="{% url 'usermgmt:register' %}">Register</a>
</li>
{% endif %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Account <span class="badge">{% if notifications.user_notifications or notifications.submission_notifications or notifications.messages %}{{ notifications.user_notifications|add:notifications.submission_notifications|add:notifications.messages }}{% endif %}</span> <span class="caret"></span></a>
<ul class="dropdown-menu">
{% if user.is_authenticated %}
<li><a href="#"><span class="glyphicon glyphicon-book"></span> Read</a></li>
<li><a href="{% url 'submissions:submit' %}"><span class="glyphicon glyphicon-pencil"></span> Write</a></li>

<li role="separator" class="divider"></li>
<li class="dropdown-header">Notifications</li>
<li>
<a href="{% url 'social:view_notifications_categories' %}#admin">
<span class="glyphicon glyphicon-bishop"></span> Admin notifications
<span class="badge">{% if notifications.admin_notifications %}{{ notifications.admin_notifications }}{% endif %}</span>
</a>
</li>
<li>
<a href="{% url 'social:view_notifications_categories' %}#users">
<span class="glyphicon glyphicon-user"></span> User notifications
Expand Down
Binary file modified models.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions sloc.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Count Location
194 administration/application_views.py
4 administration/apps.py
123 administration/ban_views.py
200 administration/flag_views.py
211 administration/flag_views.py
46 administration/forms.py
189 administration/models.py
106 administration/templates/admin-tabs-snippet.html
Expand Down Expand Up @@ -59,7 +59,7 @@ Count Location
14 core/search_indexes.py
6 core/templates/404.html
34 core/templates/activity-snippet.html
205 core/templates/base.html
227 core/templates/base.html
11 core/templates/flatpages/default.html
18 core/templates/flatpages/list.html
102 core/templates/front.html
Expand Down Expand Up @@ -157,7 +157,7 @@ Count Location
37 usermgmt/forms.py
10 usermgmt/group_models.py
119 usermgmt/group_views.py
164 usermgmt/models.py
169 usermgmt/models.py
24 usermgmt/templates/confirm_delete_group.html
20 usermgmt/templates/list_groups.html
107 usermgmt/templates/profile-tabs-snippet.html
Expand All @@ -175,7 +175,7 @@ Count Location
15 usermgmt/templates/update_group.html
49 usermgmt/templates/update_profile.html
16 usermgmt/templates/view_group.html
54 usermgmt/templates/view_profile.html
81 usermgmt/templates/view_profile.html
0 usermgmt/templatetags/__init__.py
14 usermgmt/templatetags/form_extras.py
23 usermgmt/templatetags/profile_extras.py
Expand All @@ -185,4 +185,4 @@ Count Location
24 usermgmt/urls.py
9 usermgmt/utils.py
87 usermgmt/views.py
11787 TOTAL
11852 TOTAL
7 changes: 4 additions & 3 deletions social/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def test_empty_badges(self):
self.client.login(username='foo',
password='a good password')
response = self.client.get(reverse('core:front'))
self.assertContains(response, '<span class="badge"></span>', count=5)
self.assertContains(response, '<span class="badge"></span>', count=6)

def test_badges(self):
Notification(
Expand All @@ -705,8 +705,9 @@ def test_badges(self):
self.client.login(username='foo',
password='a good password')
response = self.client.get(reverse('core:front'))
self.assertContains(response, '<span class="badge">4</span>')
self.assertContains(response, '<span class="badge">1</span>', count=4)
self.assertContains(response, '<span class="badge">3</span>')
self.assertContains(response, '<span class="badge">1</span>',
count=5)


class TestViewNotificationsCategoriesView(BaseSocialSubmissionViewTestCase):
Expand Down
17 changes: 17 additions & 0 deletions submissions/form_extras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()


@register.filter(is_safe=True)
@stringfilter
def append_form_control(value):
"""A filter for adding bootstrap classes to form fields."""
return value.replace(
'<input', '<input class="form-control"'
).replace(
'<textarea', '<textarea class="form-control"'
).replace(
'<select', '<select class="form-control"'
)
6 changes: 6 additions & 0 deletions usermgmt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def get_notifications_counts(self):
counts['submission_notifications'] += 1
return counts

def get_active_flag(self):
"""Retrieve flag if there is an active flag against this submission"""
active_flags = self.flags.filter(resolved=None)
if len(active_flags) > 0:
return active_flags[0]

def get_notifications_sorted(self):
notifications = self.user.notification_set.all()
sorted_notifications = {
Expand Down
29 changes: 28 additions & 1 deletion usermgmt/templates/view_profile.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load profile_extras %}
{% load flag_extras %}

{% block content %}
{% if blocked_by %}
Expand All @@ -14,9 +15,35 @@
<div class="panel panel-default">
<div class="panel-heading">
Profile
{% if not active_flag %}
<span class="pull-right">
<a class="btn btn-warning btn-xs" role="button" href="{% url 'administration:create_flag' %}?content_type=usermgmt:profile&amp;object_id={{ author.profile.id }}">
<span class="glyphicon glyphicon-flag"></span>
Flag profile for administrative review
</a>
</span>
{% endif %}
</div>
<div class="panel-body">
{{ author.profile.profile_rendered|safe }}
{% if active_flag %}
<div class="alert alert-danger">
<h2>Profile flagged <small>{{ active_flag.subject }}</small></h2>
<p>This profile has been flagged by {{ active_flag.flagged_by.profile.get_display_name }} for a {{ active_flag.get_flag_type_display|lower }} violation.</p>
{% if user|can_view_flagged_item:active_flag %}
{% if user in active_flag.participants.all %}
<p>You are marked as a participant in this flag; only moderators participants in this flag may view this submission.</p>
{% else %}
<p>You have permissions to view this submission because of your moderator status; only moderators participants in this flag may view this submission.</p>
{% endif %}
<p>You may view the flag <a href="{{ active_flag.get_absolute_url }}">here</a></p>
</div>
{{ author.profile.profile_rendered|safe }}
{% else %}
</div>
{% endif %}
{% else %}
{{ author.profile.profile_rendered|safe }}
{% endif %}
</div>
{% comment "Attributes TODO" %}
<div class="panel-footer">
Expand Down
22 changes: 11 additions & 11 deletions usermgmt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def view_profile(request, username):
watched = user in request.user.profile.watched_users.all()
blocked = user in request.user.profile.blocked_users.all()
blocked_by = request.user in user.profile.blocked_users.all()
active_flag = user.profile.get_active_flag()
subtitle = ''
display_name = '{} {}'.format(
gravatar(user.email, size=80),
Expand All @@ -89,14 +90,13 @@ def view_profile(request, username):
if blocked:
display_name = '&#x20e0; {}'.format(display_name)
subtitle = "blocked"
return render(request,
'view_profile.html',
{
'title': display_name,
'subtitle': subtitle,
'author': user,
'watched': watched,
'blocked': blocked,
'blocked_by': blocked_by,
'tab': 'profile',
})
return render(request, 'view_profile.html', {
'title': display_name,
'subtitle': subtitle,
'author': user,
'watched': watched,
'blocked': blocked,
'blocked_by': blocked_by,
'active_flag': active_flag,
'tab': 'profile',
})

0 comments on commit 5593689

Please sign in to comment.