Skip to content

Commit

Permalink
[WIP] case insensitive username (nitely#246)
Browse files Browse the repository at this point in the history
* case insensitive username

* nickname everywhere

* ci backend

* ci mentions

* ci mentions

* ci mentions

* tests user migrations

* migration message

* test mentions

* test backend

* auth ci

* tests

* tests

* tests

* docs

* templates
  • Loading branch information
nitely authored Dec 18, 2018
1 parent 1bd3608 commit ac5a35e
Show file tree
Hide file tree
Showing 39 changed files with 769 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h1 class="headline">{% trans "Flag" %}</h1>
{% if flag.moderator %}
<dl class="preference">
<dt class="preference-label">{% trans "Moderated by" %}:</dt>
<dd class="preference-desc"><a href="{{ flag.moderator.get_absolute_url }}">{{ flag.moderator.username }}</a></dd>
<dd class="preference-desc"><a href="{{ flag.moderator.get_absolute_url }}">{{ flag.moderator.st.nickname }}</a></dd>
</dl>
{% endif %}

Expand All @@ -41,7 +41,7 @@ <h2 class="headline">{% trans "Comment flagged" %}</h2>
<img class="comment-avatar" src="{% get_gravatar_url user=flag.comment.user size=50 %}" />

<div class="comment-username">
<a href="{{ flag.comment.user.st.get_absolute_url }}">{{ flag.comment.user.username }}</a>
<a href="{{ flag.comment.user.st.get_absolute_url }}">{{ flag.comment.user.st.nickname }}</a>
</div>
</div>

Expand Down Expand Up @@ -77,7 +77,7 @@ <h2 class="headline">{% trans "Reporters" %}</h2>
<img class="comment-avatar" src="{% get_gravatar_url user=f.user size=50 %}" />

<div class="comment-username">
<a href="{{ f.user.st.get_absolute_url }}">{{ f.user.username }}</a>
<a href="{{ f.user.st.get_absolute_url }}">{{ f.user.st.nickname }}</a>
</div>
</div>

Expand Down
8 changes: 5 additions & 3 deletions spirit/comment/flag/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django import forms
from django.utils.translation import ugettext_lazy as _
from django.db import IntegrityError
from django.db import IntegrityError, transaction
from django.utils import timezone

from .models import Flag, CommentFlag
Expand Down Expand Up @@ -39,8 +39,10 @@ def save(self, commit=True):
self.instance.comment = self.comment

try:
CommentFlag.objects.update_or_create(comment=self.comment,
defaults={'date': timezone.now(), })
with transaction.atomic():
CommentFlag.objects.update_or_create(
comment=self.comment,
defaults={'date': timezone.now(), })
except IntegrityError:
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block content %}

<h1 class="headline">
{% blocktrans trimmed with username=comment.user.username %}
{% blocktrans trimmed with username=comment.user.st.nickname %}
Reporting {{ username }}'s comment
{% endblocktrans %}
</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1 class="headline">{% trans "Comment history" %}</h1>
<div class="comment-info">

<div class="comment-username">
<a href="{{ c.comment_fk.user.st.get_absolute_url }}">{{ c.comment_fk.user.username }}</a>
<a href="{{ c.comment_fk.user.st.get_absolute_url }}">{{ c.comment_fk.user.st.nickname }}</a>
</div>

<ul class="comment-date">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block content %}

<h1 class="headline">
{% blocktrans trimmed with username=comment.user.username %}
{% blocktrans trimmed with username=comment.user.st.nickname %}
Like {{ username }}'s comment
{% endblocktrans %}
</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block content %}

<h1 class="headline">
{% blocktrans trimmed with username=like.comment.user.username %}
{% blocktrans trimmed with username=like.comment.user.st.nickname %}
Remove {{ username }}'s comment like
{% endblocktrans %}
</h1>
Expand Down
48 changes: 31 additions & 17 deletions spirit/comment/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
class CommentQuerySet(models.QuerySet):

def filter(self, *args, **kwargs):
# TODO: find a better way
return super(CommentQuerySet, self)\
.filter(*args, **kwargs)\
.select_related('user__st')
return (
super(CommentQuerySet, self)
.filter(*args, **kwargs)
.select_related('user__st'))

def unremoved(self):
# TODO: remove action
return self.filter(
Q(topic__category__parent=None) | Q(topic__category__parent__is_removed=False),
Q(topic__category__parent=None) |
Q(topic__category__parent__is_removed=False),
topic__category__is_removed=False,
topic__is_removed=False,
is_removed=False,
action=0
)
action=0)

def public(self):
return self.filter(topic__category__is_private=False)
Expand All @@ -38,32 +38,47 @@ def for_topic(self, topic):
return self.filter(topic=topic)

def _access(self, user):
return self.filter(Q(topic__category__is_private=False) | Q(topic__topics_private__user=user))
return self.filter(
Q(topic__category__is_private=False) |
Q(topic__topics_private__user=user))

def with_likes(self, user):
if not user.is_authenticated:
return self

user_likes = CommentLike.objects.filter(user=user)
prefetch = Prefetch("comment_likes", queryset=user_likes, to_attr='likes')
prefetch = Prefetch(
"comment_likes",
queryset=user_likes,
to_attr='likes')
return self.prefetch_related(prefetch)

def with_polls(self, user):
visible_polls = CommentPoll.objects.unremoved()
prefetch_polls = Prefetch("comment_polls", queryset=visible_polls, to_attr='polls')
prefetch_polls = Prefetch(
"comment_polls",
queryset=visible_polls,
to_attr='polls')

# Choices are attached to polls
visible_choices = CommentPollChoice.objects.unremoved()
prefetch_choices = Prefetch("polls__poll_choices", queryset=visible_choices, to_attr='choices')
prefetch_choices = Prefetch(
"polls__poll_choices",
queryset=visible_choices,
to_attr='choices')

if not user.is_authenticated:
return self.prefetch_related(prefetch_polls, prefetch_choices)

# Votes are attached to choices
visible_votes = CommentPollVote.objects\
.unremoved()\
.for_voter(user)
prefetch_votes = Prefetch("polls__choices__choice_votes", queryset=visible_votes, to_attr='votes')
visible_votes = (
CommentPollVote.objects
.unremoved()
.for_voter(user))
prefetch_votes = Prefetch(
"polls__choices__choice_votes",
queryset=visible_votes,
to_attr='votes')

return self.prefetch_related(prefetch_polls, prefetch_choices, prefetch_votes)

Expand All @@ -73,5 +88,4 @@ def for_access(self, user):
def for_update_or_404(self, pk, user):
if user.st.is_moderator:
return get_object_or_404(self._access(user=user), pk=pk)
else:
return get_object_or_404(self.for_access(user), user=user, pk=pk)
return get_object_or_404(self.for_access(user), user=user, pk=pk)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h1 class="headline">

{% for v in votes %}
<div class="row">
<a href="{{ v.voter.st.get_absolute_url }}">{{ v.voter.username }}</a>
<a href="{{ v.voter.st.get_absolute_url }}">{{ v.voter.st.nickname }}</a>
<span class="row-edit" title="{{ v.created_at }}"><i class="fa fa-clock-o"></i> {{ v.created_at|shortnaturaltime }}</span>
</div>
{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions spirit/comment/templates/spirit/comment/_render_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{% spaceless %}
<div class="comment-username">
<a class="username{% if c.user.st.is_administrator %} is-admin{% elif c.user.st.is_moderator %} is-mod{% endif %}"
href="{{ c.user.st.get_absolute_url }}">{{ c.user.username }}</a>
href="{{ c.user.st.get_absolute_url }}">{{ c.user.st.nickname }}</a>
<span class="comment-realname">{{ c.user.get_full_name }}</span>
</div>

Expand Down Expand Up @@ -150,7 +150,7 @@
<div class="comment-media">
<div class="comment-img">
<div class="comment-removed">
<a href="{{ c.user.st.get_absolute_url }}">{{ c.user.username }}</a>
<a href="{{ c.user.st.get_absolute_url }}">{{ c.user.st.nickname }}</a>
</div>
</div>

Expand Down
36 changes: 22 additions & 14 deletions spirit/comment/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,23 @@ def test_comment_publish_quote(self):
"""
utils.login(self)
comment = utils.create_comment(topic=self.topic)
response = self.client.get(reverse('spirit:comment:publish', kwargs={'topic_id': self.topic.pk,
'pk': comment.pk}))
self.assertEqual(response.context['form'].initial['comment'],
markdown.quotify(comment.comment, comment.user.username))
response = self.client.get(
reverse('spirit:comment:publish', kwargs={
'topic_id': self.topic.pk,
'pk': comment.pk}))
self.assertEqual(
response.context['form'].initial['comment'],
markdown.quotify(comment.comment, comment.user.username))

def test_comment_publish_next(self):
"""
next on create comment
"""
utils.login(self)
form_data = {'comment': 'foobar', 'next': '/fakepath/'}
response = self.client.post(reverse('spirit:comment:publish', kwargs={'topic_id': self.topic.pk, }),
form_data)
response = self.client.post(
reverse('spirit:comment:publish', kwargs={'topic_id': self.topic.pk, }),
form_data)
self.assertRedirects(response, '/fakepath/', status_code=302, target_status_code=404)

def test_comment_update(self):
Expand All @@ -275,8 +279,9 @@ def test_comment_update(self):

# next
form_data.update({'next': '/fakepath/', })
response = self.client.post(reverse('spirit:comment:update', kwargs={'pk': comment.pk, }),
form_data)
response = self.client.post(
reverse('spirit:comment:update', kwargs={'pk': comment.pk, }),
form_data)
self.assertRedirects(response, '/fakepath/', status_code=302, target_status_code=404)

def test_comment_update_not_moderator(self):
Expand All @@ -288,8 +293,9 @@ def test_comment_update_not_moderator(self):

utils.login(self)
form_data = {'comment': 'barfoo', }
response = self.client.post(reverse('spirit:comment:update', kwargs={'pk': comment.pk, }),
form_data)
response = self.client.post(
reverse('spirit:comment:update', kwargs={'pk': comment.pk, }),
form_data)
self.assertEqual(response.status_code, 404)

def test_comment_update_moderator(self):
Expand All @@ -302,8 +308,9 @@ def test_comment_update_moderator(self):

utils.login(self)
form_data = {'comment': 'barfoo', }
response = self.client.post(reverse('spirit:comment:update', kwargs={'pk': comment.pk, }),
form_data)
response = self.client.post(
reverse('spirit:comment:update', kwargs={'pk': comment.pk, }),
form_data)
expected_url = reverse('spirit:comment:find', kwargs={'pk': comment.pk, })
self.assertRedirects(response, expected_url, status_code=302, target_status_code=302)
self.assertEqual(Comment.objects.get(pk=comment.pk).comment, 'barfoo')
Expand All @@ -330,8 +337,9 @@ def test_comment_update_increase_modified_count(self):
utils.login(self)
comment_posted = utils.create_comment(user=self.user, topic=self.topic)
form_data = {'comment': 'my comment, oh!', }
self.client.post(reverse('spirit:comment:update', kwargs={'pk': comment_posted.pk, }),
form_data)
self.client.post(
reverse('spirit:comment:update', kwargs={'pk': comment_posted.pk, }),
form_data)
self.assertEqual(Comment.objects.get(pk=comment_posted.pk).modified_count, 1)

def test_comment_update_history(self):
Expand Down
2 changes: 1 addition & 1 deletion spirit/comment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def publish(request, topic_id, pk=None):

if pk: # todo: move to form
comment = get_object_or_404(Comment.objects.for_access(user=user), pk=pk)
quote = markdown.quotify(comment.comment, comment.user.username)
quote = markdown.quotify(comment.comment, comment.user.st.nickname)
initial = {'comment': quote}

form = CommentForm(initial=initial)
Expand Down
12 changes: 12 additions & 0 deletions spirit/core/conf/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@
#: Make emails case insensitive
ST_CASE_INSENSITIVE_EMAILS = True

#: Make user-names case insensitive
#:
#: .. Note::
#: This can be set to ``False`` at any time,
#: however setting it back to ``True`` requires
#: taking care of clashing users,
#: i.e: ``someuser``, ``SomeUser`` and ``SoMeUsEr``,
#: only one of those users will be able log-in
#: (the one in lowercase). Removing clashing users
#: is usually not possible.
ST_CASE_INSENSITIVE_USERNAMES = True

# Tests helper
ST_TESTS_RATELIMIT_NEVER_EXPIRE = False

Expand Down
2 changes: 1 addition & 1 deletion spirit/core/templates/spirit/_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<a class="header-tab-link js-tab"
href="{% url "spirit:user:menu" %}"
data-related=".js-user-content"
>{{ user.username }} <i class="fa fa-chevron-down"></i></a>
>{{ user.st.nickname }} <i class="fa fa-chevron-down"></i></a>
</li>
</ul>
{% endspaceless %}
Expand Down
Loading

0 comments on commit ac5a35e

Please sign in to comment.