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

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Madison Scott-Clary committed Nov 13, 2016
1 parent 5593689 commit cc819f2
Show file tree
Hide file tree
Showing 23 changed files with 943 additions and 182 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +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
# This will eventually move to makemigrations, probably, after things aren't in as much flux
venv/bin/python manage.py graph_models -g -a -o models.png
venv/bin/python manage.py show_urls > urls.tsv

Expand Down
8 changes: 3 additions & 5 deletions administration/ban_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ def create_ban(request):
'title': 'Permission denied',
}, status=403)

try:
flag = Flag.objects.get(pk=request.GET.get('flag'))
except Flag.DoesNotExist:
flag = None
form = BanForm(initial={
'user': user,
'flag': flag,
'end_date': timezone.now(),
'flags': Flag.objects.filter(pk=request.GET.get('flag')),
})
form.fields['flags'].queryset = Flag.objects.filter(
flagged_object_owner=user)
if request.method == 'POST':
form = BanForm(request.POST)
if form.is_valid():
Expand Down
78 changes: 53 additions & 25 deletions administration/flag_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def list_social_flags(request):
})


@permission_required('administration.can_list_content_applications',
@permission_required('administration.can_list_content_flags',
raise_exception=True)
@staff_member_required
def list_content_flags(request):
Expand All @@ -69,15 +69,18 @@ def list_content_flags(request):
@login_required
def create_flag(request):
# Ensure that we have both content type and object id
if 'content_type' not in request.GET or 'object_id' not in request.GET:
content_type = request.GET.get('content_type',
request.POST.get('content_type'))
object_id = request.GET.get('object_id',
request.POST.get('object_id'))
if content_type is None or object_id is None:
return render(request, 'permission_denied.html', {
'title': 'Cannot create flag without a subject',
'additional_error': 'Flags must be related to an object',
}, status=403)

# Ensure that we can flag the given content type
if request.GET.get('content_type') not in \
settings.FLAGGABLE_CONTENT_TYPES:
if content_type not in settings.FLAGGABLE_CONTENT_TYPES:
return render(request, 'permission_denied.html', {
'title': 'That content type is not flaggable',
'additional_error':
Expand All @@ -90,35 +93,37 @@ def create_flag(request):
}, status=403)

# Retrieve the content type, object, and, if possible, the object's owner
parts = request.GET.get('content_type').split(':')
no_owner = False
parts = 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'))
obj = ctype.get_object_for_this_type(pk=object_id)
if hasattr(obj, 'owner'):
owner = obj.owner
elif hasattr(obj, 'user'):
owner = obj.user
else:
owner = None
no_owner = True
owner = request.user

# Ensure that we can flag the given object
if owner == request.user:
if not no_owner and owner == request.user:
return render(request, 'permission_denied.html', {
'title': 'Permission denied',
'additional_error': 'You cannot flag your own objects',
}, status=403)

# Try to save any POSTed data
form = FlagForm(instance=Flag(
content_type=ctype,
object_id=obj.id,
))

# Try to save any POSTed data
if request.method == 'POST':
request.POST['content_type'] = ctype.id
form = FlagForm(request.POST)
if form.is_valid():
flag = form.save(commit=False)
flag.flagged_by = request.user
flag.flagged_object_owner = (request.user if not
owner else owner)
flag.flagged_object_owner = owner
flag.save()
form.save_m2m()
flag.participants.add(request.user)
Expand All @@ -142,13 +147,15 @@ def create_flag(request):
@login_required
def view_flag(request, flag_id=None):
flag = get_object_or_404(Flag, pk=flag_id)
social = 'administration.can_view_social_applications'
content = 'administration.can_view_content_applications'
if request.user not in flag.participants.all() and not \
(not (request.user.has_perm(social) and
flag.flag_type == Flag.SOCIAL) or
not (request.user.has_perm(content) and
flag.flag_type == Flag.CONTENT)):

# Ensure the user can view the flag
social = 'administration.can_view_social_flags'
content = 'administration.can_view_content_flags'
is_participant = request.user in flag.participants.all()
has_perm = (
flag.flag_type == Flag.SOCIAL and request.user.has_perm(social)) or (
flag.flag_type == Flag.CONTENT and request.user.has_perm(content))
if not (is_participant or has_perm):
return render(request, 'permission_denied.html', {
'title': 'Permission denied',
}, status=403)
Expand Down Expand Up @@ -178,15 +185,27 @@ def list_participating_flags(request):
@require_POST
def join_flag(request, flag_id=None):
flag = get_object_or_404(Flag, pk=flag_id)

# Ensure that user can join the flag
if flag.resolved is not None:
return render(request, 'permission_denied.html', {
'title': 'Permission denied',
'additional_error': 'This flag is already resolved',
}, status=403)
social = 'administration.can_view_social_applications'
content = 'administration.can_view_content_applications'
if not ((request.user.has_perm(social) and
flag.flag_type == Flag.SOCIAL) or
(request.user.has_perm(content) and
flag.flag_type == Flag.CONTENT)):
has_perm = (
flag.flag_type == Flag.SOCIAL and request.user.has_perm(social)) or (
flag.flag_type == Flag.CONTENT and request.user.has_perm(content))
if not has_perm:
return render(request, 'permission_denied.html', {
'title': 'Permission denied',
'additional_error':
'Only {} moderators may join this flag'.format(
flag.get_flag_type_display().lower()),
}, status=403)

# Join the flag if the user hasn't already
if request.user in flag.participants.all():
messages.warning(request,
'You are already a participant in this flag')
Expand All @@ -199,14 +218,21 @@ def join_flag(request, flag_id=None):
subject=flag).save()
flag.participants.add(request.user)
messages.success(request, 'You are now a participant in this flag')
return redirect
return redirect(flag.get_absolute_url())


@permission_required('administration.can_resolve_flags', raise_exception=True)
@staff_member_required
@permission_required('administration.can_resolve_flags', raise_exception=True)
@require_POST
def resolve_flag(request, flag_id=None):
flag = get_object_or_404(Flag, pk=flag_id)

# Ensure the user can resolve the flag
if flag.resolved is not None:
return render(request, 'permission_denied.html', {
'title': 'Permission denied',
'additional_error': 'This flag is already resolved',
}, status=403)
social = 'administration.can_view_social_applications'
content = 'administration.can_view_content_applications'
if not ((request.user.has_perm(social) and
Expand All @@ -220,6 +246,8 @@ def resolve_flag(request, flag_id=None):
messages.error(request, 'You must be participating in this flag to '
'resolve it')
return redirect(flag.get_absolute_url())

# Resolve the flag and notify participants
resolution = request.POST.get('resolution')
if resolution is None:
messages.error(request, 'You must provide a resolution')
Expand Down
3 changes: 1 addition & 2 deletions administration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ class Meta:
class BanForm(forms.ModelForm):
class Meta:
model = Ban
fields = ('user', 'end_date', 'reason_raw', 'flag')
fields = ('user', 'end_date', 'reason_raw', 'flags')
widgets = {
'end_date': DateWidget(options=dateTimeOptions,
attrs={
'id': 'id_dateTimeField',
},
bootstrap_version=3),
'user': forms.HiddenInput(),
'flag': forms.HiddenInput(),
}


Expand Down
43 changes: 43 additions & 0 deletions administration/migrations/0016_auto_20161113_2232.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-11-13 22:32
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('administration', '0015_auto_20161111_0313'),
]

operations = [
migrations.AlterModelOptions(
name='flag',
options={'ordering': ['-ctime'], 'permissions': (('can_list_social_flags', 'Can list social flags'), ('can_view_social_flags', 'Can view social flags'), ('can_resolve_social_flags', 'Can resolve social flags'), ('can_list_content_flags', 'Can list content flags'), ('can_view_content_flags', 'Can view content flags'), ('can_resolve_content_flags', 'Can resolve content flags'), ('can_resolve_flags', 'Can resolve flags'))},
),
migrations.RemoveField(
model_name='ban',
name='flag',
),
migrations.AddField(
model_name='ban',
name='flags',
field=models.ManyToManyField(blank=True, to='administration.Flag', verbose_name='Pertinent flags'),
),
migrations.AlterField(
model_name='flag',
name='body_raw',
field=models.TextField(help_text='Describe\n the issue with the content or the user. Be as specific as possible,\n including as many details and as much evidence as you can. Markdown is\n allowed.', verbose_name='body'),
),
migrations.AlterField(
model_name='flag',
name='flag_type',
field=models.CharField(choices=[('s', 'Social'), ('c', 'Content')], help_text="Pick 'content' if the\n issue is with the content of the object, such as a violation of the\n acceptable upload policy. Pick 'social' if the issue is with the way\n the creator of the content is behaving, such as a violation of the\n terms of service.", max_length=1),
),
migrations.AlterField(
model_name='flag',
name='subject',
field=models.CharField(help_text='Briefly describe\n the issue with the content or user. (100 characters)', max_length=100),
),
]
9 changes: 8 additions & 1 deletion administration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ def save(self, *args, **kwargs):
])
super(Flag, self).save(*args, **kwargs)

def __str__(self):
return '{} (against {})'.format(self.subject, str(self.object_model))

def __unicode__(self):
return '{} (against {})'.format(self.subject, str(self.object_model))

class Meta:
ordering = ['-ctime']
permissions = (
Expand Down Expand Up @@ -194,7 +200,8 @@ class Ban(models.Model):
reason_rendered = models.TextField()

# Any administrative flags if applicable
flag = models.ManyToManyField(Flag, blank=True)
flags = models.ManyToManyField(Flag, blank=True,
verbose_name='Pertinent flags')

def get_absolute_url(self):
return reverse('administration:view_ban', kwargs={
Expand Down
9 changes: 8 additions & 1 deletion administration/templates/create_ban.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<form method="post">
{% csrf_token %}
{{ form.user }}
{{ form.flag }}
<div class="form-group">
{{ form.end_date.errors }}
{{ form.end_date.label }}
Expand All @@ -18,6 +17,14 @@
<p class="help-block">{{ form.end_date.help_text|safe }}</p>
{% endif %}
</div>
<div class="form-group">
{{ form.flags.errors }}
{{ form.flags.label }}
{{ form.flags|append_form_control }}
{% if form.flag.help_text %}
<p class="help-block">{{ form.flags.help_text|safe }}</p>
{% endif %}
</div>
<div class="form-group">
{{ form.reason_raw.errors }}
{{ form.reason_raw.label }}
Expand Down
6 changes: 3 additions & 3 deletions administration/templates/create_flag.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
{{ form.object_id }}
<div class="form-group">
{{ form.subject.errors }}
{{ form.subject.label }}
{{ form.subject.label_tag }}
{{ form.subject|append_form_control }}
{% if form.subject.help_text %}
<p class="help-block">{{ form.subject.help_text|safe }}</p>
{% endif %}
</div>
<div class="form-group">
{{ form.flag_type.errors }}
{{ form.flag_type.label }}
{{ form.flag_type.label_tag }}
{{ form.flag_type|append_form_control }}
{% if form.flag_type.help_text %}
<p class="help-block">{{ form.flag_type.help_text|safe }}</p>
{% endif %}
</div>
<div class="form-group">
{{ form.body_raw.errors }}
{{ form.body_raw.label }}
{{ form.body_raw.label_tag }}
{{ form.body_raw|append_form_control }}
{% if form.body_raw.help_text %}
<p class="help-block">{{ form.body_raw.help_text|safe }}</p>
Expand Down
12 changes: 12 additions & 0 deletions administration/templates/view_ban.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ <h2>Information</h2>
{% endif %}
{% endif %}
</dd>
<dt>Pertinent flags</dt>
<dd>
<ul class="list-group">
{% for flag in ban.flags.all %}
<li class="list-group-item striped-item">
<a href="{{ flag.get_absolute_url }}">
{{ flag }}
</a>
</li>
{% endfor %}
</ul>
</dt>
<dt>Admin contact</dt>
<dd>
<a href="{% url 'usermgmt:view_profile' ban.admin_contact.username %}">{{ ban.admin_contact.profile.get_display_name }}</a>
Expand Down
Loading

0 comments on commit cc819f2

Please sign in to comment.