Skip to content

Commit

Permalink
Fix template for unauthenticated users
Browse files Browse the repository at this point in the history
- they are redirected to the login page now.

Also, fix readme links.
  • Loading branch information
abhiabhi94 committed Oct 20, 2020
1 parent a608c21 commit 11c8619
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ django-flag-app
A pluggable django application that adds the ability for users to flag(or report) your models.


.. image:: https://github.com/abhiabhi94/django-flag-app/tree/master/docs/_static/images/django-flag-app.gif
.. image:: https://github.com/abhiabhi94/django-flag-app/blob/master/docs/_static/images/django-flag-app.gif
:alt: flagging-process


For complete documentation you may visit `Read the Doc`_. or see the `docs`_ directory.

.. _Read the Doc: https://django-flag-app.readthedocs.io
.. _docs: https://github.com/abhiabhi94/django-flag-app/tree/master/docs/
.. _docs: https://github.com/abhiabhi94/django-flag-app/blob/master/docs/

Installation
------------
Expand Down Expand Up @@ -124,7 +124,7 @@ Use template tag

If you want to use web API, this step is not required. See further instructions at `Web API`_.

.. _Web API: https://github.com/abhiabhi94/django-flag-app/tree/master/docs/webAPI.rst
.. _Web API: https://github.com/abhiabhi94/django-flag-app/blob/master/docs/webAPI.rst

``render_flag_form`` tag requires 2 required positional arguments:

Expand All @@ -144,4 +144,4 @@ Contributing

Please see the instructions at `Contributing`_.

.. _Contributing: https://github.com/abhiabhi94/django-flag-app/tree/master/CONTRIBUTING.rst
.. _Contributing: https://github.com/abhiabhi94/django-flag-app/blob/master/CONTRIBUTING.rst
2 changes: 1 addition & 1 deletion flag/static/flag/css/flag.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
background-color: #fff3cd;
border-color: #ffeeba;
}
.flag-report-icon {
.flag-icon {
float: right;
border: inherit;
background: inherit;
Expand Down
19 changes: 15 additions & 4 deletions flag/templates/flag/flag_form.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{% load i18n %}
{% load static %}
<link rel="stylesheet"
{% load flag_tags %}
<link
rel="stylesheet"
type="text/css"
href="{% static 'flag/css/flag.css' %}"/>
href="{% static 'flag/css/flag.css' %}">
{% if user.is_anonymous %}
{% get_login_url as login_url %}
<a
href="{{ login_url }}?next={{ request.build_absolute_uri }}"
class="{% block cls_flag_icon_unauthenticated %}{% endblock cls_flag_icon_unauthenticated %}">
<span title="{% trans 'Report content' %}">
{% include "flag/flag_icon.html" %}
</span>
</a>
{% else %}
<div class="report-modal-form-combined {% block cls_flag %}{% endblock cls_flag %}">
<div class="flag-report-icon {% block cls_flag_icon %}{% endblock cls_flag_icon %}"
data-url="{% url 'flag:flag' %}"
Expand All @@ -12,9 +24,7 @@
data-csrf="{{ csrf_token }}">
<span
title="{% if has_flagged %}{% trans "Remove flag" %}{% else %}{% trans "Report content" %}{% endif %}">
{% block flag_icon %}
{% include "flag/flag_icon.html" %}
{% endblock flag_icon %}
</span>
</div>
<div
Expand Down Expand Up @@ -63,3 +73,4 @@
<script
type="text/javascript"
src="{% static 'flag/js/flag.js' %}"></script>
{% endif %}
14 changes: 14 additions & 0 deletions flag/templatetags/flag_tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import gettext_lazy as _

from flag.models import FlagInstance

Expand All @@ -20,6 +23,16 @@ def has_flagged(user, obj):
return False


@register.simple_tag(name='get_login_url')
def get_login_url():
login_url = getattr(settings, 'LOGIN_URL', None)
if not login_url:
raise ImproperlyConfigured(_('Django Flag App: LOGIN_URL is not defined in the settings'))
if not login_url.endswith('/'):
login_url += '/'
return login_url


@register.inclusion_tag('flag/flag_form.html')
def render_flag_form(obj, user):
"""
Expand All @@ -33,6 +46,7 @@ def render_flag_form(obj, user):
'app_name': get_app_name(obj),
'model_name': get_model_name(obj),
'model_id': obj.id,
'user': user,
'has_flagged': has_flagged(user, obj),
'flag_reasons': FlagInstance.reasons
}
23 changes: 22 additions & 1 deletion tests/test_template_tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from flag.templatetags.flag_tags import get_app_name, get_model_name, has_flagged, render_flag_form
from unittest.mock import patch

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from flag.templatetags.flag_tags import get_app_name, get_model_name, has_flagged, render_flag_form, get_login_url
from tests.base import BaseTemplateTagsTest, FlagInstance


Expand All @@ -21,6 +26,20 @@ def test_has_flagged_for_authenticated_user(self):
self.set_flag(post, user)
self.assertEqual(has_flagged(user, post), True)

@patch.object(settings, 'LOGIN_URL', None)
def test_login_url_without_setting_login_url(self):
with self.assertRaises(ImproperlyConfigured) as error:
get_login_url()
self.assertIsInstance(error.exception, ImproperlyConfigured)

@patch.object(settings, 'LOGIN_URL', '/login')
def test_get_login_url_without_backward_slash(self):
self.assertEqual(settings.LOGIN_URL + '/', get_login_url())

@patch.object(settings, 'LOGIN_URL', '/login/')
def test_login_url_with_backward_slash(self):
self.assertEqual(get_login_url(), settings.LOGIN_URL)

def test_render_flag_form(self):
post = self.post_1
user = self.user_2
Expand All @@ -29,6 +48,7 @@ def test_render_flag_form(self):
self.assertEqual(data['app_name'], post._meta.app_label)
self.assertEqual(data['model_name'], type(post).__name__)
self.assertEqual(data['model_id'], post.id)
self.assertEqual(data['user'], user)
self.assertEqual(data['flag_reasons'], FlagInstance.reasons)
self.assertEqual(data['has_flagged'], False)

Expand All @@ -39,5 +59,6 @@ def test_render_flag_form(self):
self.assertEqual(data['app_name'], post._meta.app_label)
self.assertEqual(data['model_name'], type(post).__name__)
self.assertEqual(data['model_id'], post.id)
self.assertEqual(data['user'], user)
self.assertEqual(data['flag_reasons'], FlagInstance.reasons)
self.assertEqual(data['has_flagged'], True)

0 comments on commit 11c8619

Please sign in to comment.