I want to send notification emails to a list of users defined in a 'observers' ManyToMany field when a new post is created.
The post is created without errors and the list of observer users are added to it successfully (they appear in the post_detail.html template), but the notification email is never sent to the observer users.
I think I'm doing something wrong in the new_post function below, which I adapted from this code for sending email when a user comments on a post, which does work. Any help much appreciated.
models.py (relevant parts):
from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.db.models import signals
from notification import models as notification
class Post(models.Model):
author = models.ForeignKey(User, related_name="added_posts")
observers = models.ManyToManyField(User, verbose_name=_("Observers"), related_name='observers+', blank=True, null=True)
# send notification to Post observers
def create_notice_types(app, created_models, verbosity, **kwargs):
notification.create_notice_type("new_post", "New post created", "A new post has been created")
signals.post_syncdb.connect(create_notice_types, sender=notification)
def new_post(sender, instance, created, **kwargs):
context = {
'observer': instance,
'site': Site.objects.get_current(),
}
recipients = []
pk=instance._get_pk_val()
for observer in instance.observers.all().distinct():
if observer.user not in recipients:
recipients.append(observer.user)
notification.send(recipients, 'new_post', context)
signals.post_save.connect(
new_post, sender=models.get_model(
'blog', 'Post'), dispatch_uid="pkobservers")
views.py (relevant parts):
#login_required
def add(request, form_class=PostForm, template_name="blog/post_add.html"):
post_form = form_class(request)
if request.method == "POST" and post_form.is_valid():
post = post_form.save(commit=False)
post.author = request.user
post_form.save()
post_form.save_m2m()
return redirect("blog_user_post_detail",
username=request.user.username, slug=post.slug)
return render_to_response(template_name,
{"post_form": post_form}, context_instance=RequestContext(request))
EDIT:
Also tried this (after dropping the blog_post and blog_post_observers tables, and running manage.py syncdb again, but still doesn't work):
models.py
class Post(models.Model):
# ....
observers = models.ManyToManyField(User, verbose_name=_("Observers"), related_name='observers+')
def new_post(sender, instance, created, **kwargs):
context = {
'observer': instance,
'site': Site.objects.get_current(),
}
recipients = instance.observers.all()
pk=instance._get_pk_val()
notification.send(recipients, 'new_post', context)
signals.post_save.connect(new_post, sender=models.get_model('blog', 'Post'), dispatch_uid="pkobservers")
EDIT 2 Thursday, 27 June 2013: 11:48:49 Italy Time:
When I edit/update a post, using the following view, the notification email does work:
views.py
#login_required
def edit(request, id, form_class=PostForm, template_name="blog/post_edit.html"):
post = get_object_or_404(Post, id=id)
if post.author != request.user:
request.user.message_set.create(message="You can't edit items that aren't yours")
return redirect("desk")
post_form = form_class(request, instance=post)
if request.method == "POST" and post_form.is_valid():
post = post_form.save(commit=False)
post.updated_at = datetime.now()
post_form.save()
post_form.save_m2m()
messages.add_message(request, messages.SUCCESS, message=_("Successfully updated post '%s'") % post.title)
return redirect("blog_user_post_detail", username=request.user.username, slug=post.slug)
return render_to_response(template_name, {"post_form": post_form, "post": post}, context_instance=RequestContext(request))
First of all, I think your many to many relationship should not have blank or null, since each observer would ideally be a user and not a None. This avoids trying to send emails to None users, which probably gives error.
Second, I think you can just use
recipients = instance.observers.all().distinct()
instead of looping on the list (distinct() already considers only unique users)
Third, I don't see why you really need a "distinct()": can a user be observer more than once?
recipients = instance.observers.all()
Fourth, In your code, you are looping trough instance.observers.all() with an "observer", which is already a User. Why do you append observer.user in recipients? I think appending observer should be enough.
Finally, confirm that recipients is not empty. If you have tested notification.send(), your code seems correct.
Figured it out — the notification email is now sent to observers when creating a new post.
Basically I needed to call save a second time after setting the post.id:
views.py:
#login_required
def add(request, form_class=PostForm, template_name="blog/post_add.html"):
post_form = form_class(request)
if request.method == "POST" and post_form.is_valid():
post = post_form.save(commit=False)
post.author = request.user
post_form.save()
post.id = post.id # set post id
post_form.save() # save a second time for notifications to be sent to observers
return redirect("blog_user_post_detail",
username=request.user.username, slug=post.slug)
return render_to_response(template_name,
{"post_form": post_form}, context_instance=RequestContext(request))
The models.py is unchanged from the first EDIT: in the question (Thanks to J. C. Leitão for the help).
Here it is to be sure:
models.py
class Post(models.Model):
observers = models.ManyToManyField(User, verbose_name=_("Observers"), related_name='observers+')
def new_post(sender, instance, created, **kwargs):
context = {
'observer': instance,
'site': Site.objects.get_current(),
}
recipients = instance.observers.all()
pk=instance._get_pk_val()
notification.send(recipients, 'new_post', context)
signals.post_save.connect(new_post, sender=models.get_model('blog', 'Post'), dispatch_uid="pkobservers")
Related
I am trying to add messaging functionality to my web app made in Django.
So far, I have managed to successfully send and receive messages from user to user.
But, now I have been stuck at showing all the conversation lists to the inbox.html page of the logged user.
I have tried different approaches that I can think of but can not get the expected result.
models.py
class Messaging(models.Model):
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sender')
receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name='receiver')
message_text = models.TextField(max_length=360, verbose_name='Write Message')
message_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'{self.sender}\'s Message to {self.receiver}'
viwes.py
Function to send and receive messages from user to user
#login_required
def messageview(request, user_name):
sender_user = request.user
receiver_user = User.objects.get(username=user_name)
message_list = Messaging.objects.filter(sender=sender_user, receiver=receiver_user).order_by('message_date') | \
Messaging.objects.filter(receiver=sender_user, sender=receiver_user).order_by('message_date')
if request.method == 'POST':
msg_text = request.POST.get('msg_text')
messaging = Messaging()
messaging.sender = sender_user
messaging.receiver = receiver_user
messaging.message_text = msg_text
messaging.save()
return redirect(request.META['HTTP_REFERER'])
context = {
'sender_user': sender_user,
'receiver_user': receiver_user,
'message_list': message_list,
}
return render(request, 'message.html', context)
Now I want to create an inboxview in views.py that will render all the conversation of the logged user.
Suppose I have two users in the database A and B, they have exchange 4 messages between them. What I want is to show the conversation as a list, which is in this case only one. For example, the logged user is A, he exchanges messages with user B and C. The inbox will show two rows. When user A clicks on either of the rows, he will be taken to the details message page corresponding to the user. It is kinds of like WhatsApp or messenger. I hope I can explain.
Edited: Added example image for better understanding
I am able to do this:
I need help to do this:
Please guide me the way.
You could try something like this.
This view will query the Messaging model and get all entries where sender or receiver is the logged in user.
#login_required
def inbox(request, user):
# Get all the records where either sender OR receiver is the logged in user
messages = Messaging.objects.filter(sender=request.user) | Messaging.objects.filter(receiver=request.user)
context = {'messages': messages}
return render(request, 'inbox.html', context)
You can add any extra lines of code to the above code that suits your requirements.
inbox.html
{% for message in messages %}
# You can show your message details here
{% endfor %}
EDIT
Here is my test:
def test_admin_sees_unpublished_questions(self):
"""
Logged-in admin users see unpublished questions on the index page.
"""
# create admin user and log her in
password = 'password'
my_admin = User.objects.create_superuser('myuser', 'myemail#test.com', password)
user = authenticate(username="myuser", password="password")
if user is not None:
print(user.username)
else:
print('Not found!')
self.client.login(username=my_admin.username, password=password)
#create future question and check she can see it
create_question(question_text="Unpublished question.", days=5)
response = self.client.get(reverse('polls:index'))
self.assertContains('Please review these unpublished questions:')
self.assertEqual(response.context["user"], user)
self.assertQuerysetEqual(
response.context['unpublished_question_list'],
['<Question: Unpublished question.>']
)
It's a bit messy. There are a few lines checking if there is a user in context which all appear to show response.context["user"] is there.
Here is my view:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
def get_queryset(self):
"""
Return the last five published questions (not including those set to be
published in the future).
"""
queryset = Question.objects.filter(
pub_date__lte=timezone.now()
).exclude(
#question without choices
choice=None
).order_by('-pub_date')[:5]
return queryset
def get_context_data(self, **kwargs):
"""
Override get_context_data to add another variable to the context.
"""
context = super(IndexView, self).get_context_data(**kwargs)
context['unpublished_question_list'] = Question.objects.filter(pub_date__gte=timezone.now())
print(context)
return context
I am writing tests for the django polls app tutorial.
I want to write a test which which logs in a user, creates an instance of the question model with a publish date in the future, and ensures that this logged in user can see this question.
I've tried using
self.assertContains('Please review these unpublished questions: ')
in the test method because my template looks like this:
{% if user.is_authenticated %}
<p>Hello, {{ user.username }}. Please review these unpublished questions: </p>
{% if unpublished_question_list %} etc
but even though
self.assertEqual(response.context["user"], user)
passes testing after
self.client.login(username=my_admin.username, password=password)
my template doesn't seem to be rendering properly to the test client.
Some help would be much appreciated!
AssertionError: False is not true : Couldn't find 'Please review these unpublished questions: ' in response
I have my Order model which has a FK to Item model. I customized the Order create method to create the item which is passed in from the POST request. I want to customize it to allow the POST request to pass the Item pk instead of the name. so if the pk exists, I just use it and no need to create a new item.
However, when I look at the validated_data values in the create(), the id field doesn't exist.
class Item(models.Model):
name = models.CharField(max_length=255,blank=False)
class Order(models.Model):
user = models.OneToOneField(User,blank=True)
item = models.OneToOneField(Item,blank=True)
I want the POST body to be
{
"name":"iPhone"
}
or
{
"id":15
}
I tried to implement this kind of behavior myself, and didn't find a satisfying way in terms of quality. I think it's error prone to create a new object without having the user confirming it, because the user could type in "iphone" or "iPhon" instead of "iPhone" for example, and it could create a duplicate item for the iphone.
Instead, I recommend to have two form fields:
a select field for the item name,
a text input to create an item that's not in the list.
Then, it's easy to handle in the form class.
Or, with autocompletion:
user types in an Item name in an autocompletion field,
if it doesn't find anything, the autocomplete box proposes to create the "iPhon" Item,
then the user can realize they have a typo,
or click to create the "iPhone" item in which case it's easy to trigger an ajax request on a dedicated view which would respond with the new pk, to set in the form field
the form view can behave normally, no need to add confusing code.
This is how the example "creating choices on the fly" is demonstrated in django-autocomplete-light.
You may need to use Django Forms.
from django import forms
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from .models import Order, Item
from django.shortcuts import render
class OrderForm(forms.Form):
item_name = forms.CharField(required=False)
item = forms.ModelChoiceField(queryset=Item.objects.none(), required=False)
def __init__(self, *args, **kwargs):
super(OrderForm, self).__init__(*args, **kwargs)
self.fields['item'].queryset = Item.objects.all()
def clean_item(self):
item_name = self.cleaned_data['item_name']
item = self.cleaned_data['item']
if item:
return item
if not item_name:
raise ValidationError('New item name or existing one is required.')
return Item.objects.create(name=item_name)
#login_required
def save(request):
if request.method == 'GET':
return render(request, 'template.html', {'form': OrderForm()})
user = request.user
form = OrderForm(request.POST)
if form.is_valid():
order = Order.objects.create(user=user, item=form.cleaned_data['item'])
return render(request, 'template.html', {'form': OrderForm(), 'order': order, 'success': True})
else:
return render(request, 'template.html', {'form': OrderForm(), 'error': True})
I'm new to Django and looking for best practices on the code I just wrote (below). The code currently exists in my view.py and simply creates a new event. Being familiar with other languages it just 'smells bad' if you know what I mean. Could someone point out how they would do this simple task.
The only thing looking at my code again (and from reading the docs a little more), would be to move the request.user into the models.py save function.
Anything else which is a big newbie mistake below?
#login_required
def event_new(request):
# If we had a POST then get the request post values.
if request.method == 'POST':
form = EventForm(request.POST)
# Check we have valid data
if form.is_valid():
# If form has passed all validation checks then continue to save it.
city = City.objects.get(name=form.cleaned_data['autocompleteCity'])
category = form.cleaned_data['category']
event = Event(
name=form.cleaned_data['name'],
details=form.cleaned_data['details'],
date=form.cleaned_data['date'],
start=form.cleaned_data['start'],
end=form.cleaned_data['end'],
category=category,
city=city,
user=request.user,
)
event.save()
messages.add_message(request, messages.SUCCESS, 'Event has been created.')
return HttpResponseRedirect('/events/invite/')
else:
messages.add_message(request, messages.ERROR, 'Error')
context = {'form': form}
return render_to_response('events/event_edit.html', context, context_instance=RequestContext(request))
else:
form = EventForm
context = {'form': form}
return render_to_response('events/event_edit.html', context, context_instance=RequestContext(request))
You should read about create forms from models. The ModelForm class will save you from copying the fields from the form to the model.
Apart from that this view looks pretty normal to me.
You can even get rid of some of the boilerplate code (if request.method == "POST", if form.is_valid(), etc.) with the generic FormView or CreateView. Since you seam to have some special form handling it might not be of any use for you, but it might be worth a look.
This code is not 100% complete (your special logic for cities is missing) but apart from that should be pretty complete and give you an idea how generic views could be used.
forms.py
from django.forms import ModelForm
class EventForm(ModelForm):
def __init__(self, user, **kwargs):
self.user = user
super(EventForm, self).__init__(**kwargs)
class Meta:
model = Event
def save(self, commit=True):
event = super(EventForm, self).save(commit=False)
event.user = self.user
if commit:
event.save()
views.py
from django.views.generic.edit import CreateView
class EventCreate(CreateView):
model = Event
form_class = EventForm
template = "events/event_edit.html"
success_url = "/events/invite/" # XXX use reverse()
def get_form(form_class):
return form_class(self.request.user, **self.get_form_kwargs())
def form_valid(form):
form.user = self.request.user
messages.success(request, 'Event has been created.')
super(EventCreate, self).form_valid(form)
def form_invalid(form):
messages.error(request, 'Error')
super(EventCreate, self).form_invalid(form)
urls.py
url(r'event/add/$', EventCreate.as_view(), name='event_create'),
I think this looks great. You have followed the conventions from the docs nicely.
Moving request.user to a model would certainly be an anti pattern - a views function is to serve in the request/response loop, so access this property here makes sense. Models know nothing of requests/responses, so it's correct keep these decoupled from any view behavior.
Only thing that I noticed was creating a category variable out only to be used in construction of the Event, very minor.
My email change form for users works, but I feel like my code is not written correctly. If I did it the way I have done below, I'd need a thousand else statements so that the page would return a response. Can someone tell me how I can make this more efficient/better? I'm not sure of the conventional way to do this
Views.py
def email_change(request):
form = Email_Change_Form()
if request.method=='POST':
form = Email_Change_Form(request.POST)
if form.is_valid():
if request.user.is_authenticated:
if form.cleaned_data['email1'] == form.cleaned_data['email2']:
user = request.user
u = User.objects.get(username=user)
# get the proper user
u.email = form.cleaned_data['email1']
u.save()
return HttpResponseRedirect("/accounts/profile/")
else:
return render_to_response("email_change.html", {'form':form}, context_instance=RequestContext(request))
I would suggest a complete change on how you looked at this. In my opinion, you should have all the implementation on the form side.
forms.py
I've implemented a class based on the SetPasswordForm that is more complete:
class EmailChangeForm(forms.Form):
"""
A form that lets a user change set their email while checking for a change in the
e-mail.
"""
error_messages = {
'email_mismatch': _("The two email addresses fields didn't match."),
'not_changed': _("The email address is the same as the one already defined."),
}
new_email1 = forms.EmailField(
label=_("New email address"),
widget=forms.EmailInput,
)
new_email2 = forms.EmailField(
label=_("New email address confirmation"),
widget=forms.EmailInput,
)
def __init__(self, user, *args, **kwargs):
self.user = user
super(EmailChangeForm, self).__init__(*args, **kwargs)
def clean_new_email1(self):
old_email = self.user.email
new_email1 = self.cleaned_data.get('new_email1')
if new_email1 and old_email:
if new_email1 == old_email:
raise forms.ValidationError(
self.error_messages['not_changed'],
code='not_changed',
)
return new_email1
def clean_new_email2(self):
new_email1 = self.cleaned_data.get('new_email1')
new_email2 = self.cleaned_data.get('new_email2')
if new_email1 and new_email2:
if new_email1 != new_email2:
raise forms.ValidationError(
self.error_messages['email_mismatch'],
code='email_mismatch',
)
return new_email2
def save(self, commit=True):
email = self.cleaned_data["new_email1"]
self.user.email = email
if commit:
self.user.save()
return self.user
This class checks both if the e-mail have in fact changed (very useful if you need to validate the e-mail or update mail chimp for example) and produce the appropriate errors, so they are helpful for the user in the form view.
views.py
Your code adapted to my class:
#login_required()
def email_change(request):
form = EmailChangeForm()
if request.method=='POST':
form = EmailChangeForm(user, request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect("/accounts/profile/")
else:
return render_to_response("email_change.html", {'form':form},
context_instance=RequestContext(request))
As you can see the view is simplified, assuring everything on the form level.
To ensure the login I set a decorator (See the docs).
Ps: I changed email1 and email2 to new_email1 and new_email2 to be consistent with the Django approach on passwords. I also changed the form Email_Change_Form to EmailChangeForm according to Python guidelines for classes.
I would suggest moving the validation to the form clean method:
#form
class EmailChangeForm():
..
..
def clean(self):
if self.cleaned_data.get('email1', None) != self.cleaned_data.get('email1', None):
raise forms.ValidationError('Validation Failed')
#login_required('/login/') //You can check the user is logged in using the decorator
def email_change(request):
form = Email_Change_Form()
if request.method=='POST':
form = Email_Change_Form(request.POST)
if form.is_valid():
user = request.user //Don't know why you want to get the object from database when you already have it
user.email = form.cleaned_data['email1']
user.save()
return HttpResponseRedirect("/accounts/profile/")
else:
return render_to_response("email_change.html", {'form':form}, context_instance=RequestContext(request))
Update:
Doing this is redundant:
user = request.user
u = User.objects.get(username=user.username)
Because user is going to be the same as u i.e. user = u
You will create more complicated code with nested if, if you write every bit of logic in your views. You need to break them in appropriate sections. Like, for every form related validations, do it in forms like -
if `email1` is same as `email2`,
and if email1 is valid
check it in your form. You should check that in clean or clean_FieldName methods. Refer here: https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
Another check you applied for authentication - if the user is authenticated or not. In this case, can a Unauthorised user change his email - well no. So why should I let my code run for it. It would be better to check this condition as soon as possible and then send the user to login page. #login_required is used to check this condition as a decorator of your view. See here : https://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator
If you really want to check your user authentication in your view, I think the good approach would be -
def email_change(request):
if not request.user.is_authenticated:
// Do what you need to say to user or send them to login
// return HttpResponse object / HttpResponseRedirect
form = Email_Change_Form(request.POST)
if request.method=='POST':
if form.is_valid():
...
else:
... // Display form.