-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- unauthenticated users can comment anonymously by giving their email. - user can verify the comment by click on the link sent to their email. - the email can be sent in both html and text format. - comment only hits the database when it is authenticated. Add email field to the database -> change is backward compatible for previous comments. Closes #33
- Loading branch information
1 parent
5fa0818
commit 646e93b
Showing
47 changed files
with
1,339 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,29 @@ | ||
from django import forms | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from comment.models import Comment | ||
from comment.conf import settings | ||
|
||
|
||
class CommentForm(forms.ModelForm): | ||
class Meta: | ||
model = Comment | ||
fields = ('content', ) | ||
fields = ('content',) | ||
widgets = {'content': forms.Textarea(attrs={'rows': 1})} | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.request = kwargs.pop('request') | ||
super().__init__(*args, **kwargs) | ||
if self.request.user.is_anonymous and settings.COMMENT_ALLOW_ANONYMOUS: | ||
self.fields['email'] = forms.EmailField( | ||
label=_("email"), | ||
widget=forms.EmailInput(attrs={ | ||
'placeholder': _('email address, this will be used for verification.'), | ||
'title': _("email address, it will be used for verification.") | ||
}) | ||
) | ||
|
||
def clean_email(self): | ||
"""this will only be executed when email field is present for unauthenticated users""" | ||
email = self.cleaned_data['email'] | ||
return email.strip().lower() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
def set_default_email(apps, schema_editor): | ||
comment_model = apps.get_model('comment', 'Comment') | ||
for comment in comment_model.objects.all(): | ||
comment.email = comment.user.email | ||
comment.save(update_fields=['email']) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('comment', '0008_comment_urlhash'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='comment', | ||
name='email', | ||
field=models.EmailField(default=None, max_length=254, null=True), | ||
), | ||
migrations.RunPython(set_default_email, migrations.RunPython.noop), | ||
migrations.AlterField( | ||
model_name='comment', | ||
name='email', | ||
field=models.EmailField(max_length=254, blank=True), | ||
), | ||
migrations.AlterField( | ||
model_name='comment', | ||
name='posted', | ||
field=models.DateTimeField(default=django.utils.timezone.now, editable=False), | ||
), | ||
migrations.AlterField( | ||
model_name='comment', | ||
name='user', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.contrib.auth.mixins import LoginRequiredMixin | ||
from django.http import HttpResponseBadRequest | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
# This willl be expanded to provide checks for validating the form fields | ||
class BaseCommentMixin(LoginRequiredMixin): | ||
pass | ||
|
||
|
||
class AJAXRequiredMixin: | ||
def dispatch(self, request, *args, **kwargs): | ||
if not request.is_ajax(): | ||
return HttpResponseBadRequest(_('Only AJAX request are allowed')) | ||
return super().dispatch(request, *args, **kwargs) |
Oops, something went wrong.