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 %}
Related
Hello I am new to django and web programming. I am building a website for my school that allows students to schedule online advising appointments.
I need to be able to email students temporary passcodes to their student emails and then validate them on the next page. I have an email form :
class EmailForm(forms.Form):
student_email = forms.EmailField(max_length = 200, label = 'Mail')
def clean_student_email(self):
student_email = self.cleaned_data['student_email']
if not student_email.endswith('.edu'):
raise ValidationError("Please enter your school email that ends with #edu")
return student_email
and a login view
def login(request):
if request.method == 'POST':
form = EmailForm(request.POST)
if form.is_valid():
student_email = form.cleaned_data['student_email']
random_code = get_random_string()
subject = "Temporary Advising Code"
message = f'Your temporary code is: \n code: {random_code}'
send_mail(subject, message, 'advising email', [student_email])
return HttpResponseRedirect('/enter_code/', {'form' : form})
else:
form = EmailForm()
return render(request, 'login/login.html', {'form' : form})
Now I am able to generate a random string and send it to the students email but I am wondering if someone can tell me how I can validate that string on the next page.
I would advice against your approach.
But I would suggest to check how password reset token generator works in django .
Source: PasswordResetTokenGenerator
You can make something very similar to generate a token which you will send to students.
You don't need to store such token as you can easily generate it again ( when it comes to verification ).
So the idea here is to generate the token using some data which after password is changed or set by students - the token will no longer be valid.
You can even incorporate a expiration time if needed.
And you don't need to generate temporary password either ( which is something i don't like very much ).
So instead of sending students the temp password and asking them to log in with that - you would just send them a link with the token and by accessing the page with that token - they will be able to set a password.
I'm managing to send emails to users that are present in my table and then showing success message on the same page. I'm successfully sending email to users(taking their id and then their email to send). While creating urls, I need to mention id as well which redirects me to another page. But what I want is to be redirected on the same page.
Here's the table which contains the users:
After clicking Send Mail, I'm taking the userid then email with this and then sendig mail.
Taking userid:
</button><span class="badge badge-success">Send Mail</span>
Here's my views.py code on this button:
def sendMails(request, id=None):
query_user = get_object_or_404(NewUser, id=id)
user_email = query_user.user
admin_email = request.user.email
result = send_emails(request, admin_email, user_email)
context = {
'code': result
}
return render(request,'requested_users.html', context)
And urls.py:
path('dashboard/requested_users/<id>', views.sendMails, name='sendMails'),
What I want to be on same page even after sending mail(such that the urls will be):
path('dashboard/requested_users/', views.sendMails, name='sendMails'),
But if I'm not providing the id in the urls, it's giving me the error:
Reverse for 'sendMails' with keyword arguments '{'id': 1}' not found.
I know I've asked a long question, but I'm really stuck into this.
Thank you in advance.
I'm currently working on a social network site with Django.
I am working on the "tag" feature, which sends a Notification to a user whenever they are tagged in a Post.
I created a post_save signal like this:
def post_tagged(*args, **kwargs):
instance = kwargs['instance']
message = f'{instance.user.slug} mentioned you in a post.'
#Find in the content of the post if there are any "#username", if yes create a Notification object
for user in MyUser.objects.all():
if instance.content.find(f'#{user.slug}') != -1 and instance.user != user:
Notification.objects.create(user = user, message = message)
models.signals.post_save.connect(post_tagged, sender = Post)
This is working fine when I create a new Post, but it also triggered when I delete a Post. This is the code in the view to delete a post:
if 'deletePost' in request.POST:
postId = request.POST.get('deletePost', None)
post = Post.objects.get(pk = postId)
if request.user == post.user:
post.delete()
message = 'Success'
else:
message = 'Fail'
jsonData = {"message": message,}
return HttpResponse(json.dumps(jsonData))
Also if it helps you can see I'm using ajax to send the request to the view. And the delete is also working fine. The only problem is that it also create a Notification to the tagged users when I delete a post.
Anyone having the same issue? I appreciate any help. Thanks
Maybe I am not asking the right question in the search area, but I can't find an answer for this. I am pretty sure that many people have this use case, but as a beginner in Django + Python, I need to ask it.
I have user that fills up a form and the data is stored in the database. Basically this form asks for an access to a Database and after the form is submitted I want my program to send an email to the user's manager and to the DBA to APPROVE or DENY it. Very simple, right?
My idea is that in this e-mail I send two URL's, one for approving and one for denying the request. When the URL the is clicked I send a response to the server with an update in the manager_approval field.
Has anyone implemented this solution, or could point me to something that could help me?
I am doing everything using Django + Python.
Regards,
Marcos Freccia
Basically this technique used in email verification. This is where you should look into.
Let's say you have model, named request, which has field like username to identify the person who requested access, database name, well, everything. But it will also have two "password-like" fields which will be used to determine if request was declined or not.
class Request(models.Model):
user = models.ForeignKey ...
databasename =
date =
...
access_granted = models.BooleanField(default=False)
deny_token = models.CharField()
allow_token = models.CharField()
The point is to generate those tokens on saving request in the View:
if request.method == POST:
form = RequestForm(request.POST)
if form.is_valid():
data['user'] = form.cleaned_data['user'])
data['databasename'] = form.cleaned_data['databasename'])
...
data['access_token'] = GENERATE_USING_HASH_FUNCTION()
data['deny_token'] = GENERATE_USING_HASH_FUNCTION()
form.save(data)
Then you can use module EmailMultiAlternatives to send html email like so:
subject, from_email, to = 'Request', 'admin#example.com', form.cleaned_data['manager_email']
html_content = render_to_string(HTML_TEMPLATE, CONTEXT) # Just as any regular templates
text_content = strip_tags(html_content)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to], reply_to=["admin#example.com"])
msg.attach_alternative(html_content, "text/html")
msg.send()
And inside that template you construct reverse url:
{% url 'app:grant_access' allow_token=token %} # "token" you get from context
{% url 'app:deny_access' deny_token=token %} # will become example.com/deny_access/7ea3c95, where 7ea3c95 is token
Then add lines to urls.py of your app like that:
url(r'^allow_access/(?P<allow_token>[0-9]+)$', CheckAcessView.as_view(), name="app:grant_access"),
url(r'^deny_access/(?P<deny_token>[0-9]+)$', CheckAcessView.as_view(), name="app:deny_access"),]
Then create CheckAcessView view. Where you access request stored in your database and check if, for example, parameter of url "allow_token" is equal stored allow_token. If so, change request status to allowed.
Im trying to understand the django-registration app, and now Im able to send emails to users with activation key. What Im not able to figure out is how to send the activation key back to server when the user clicks a link in his/her email.
class AbstractEmailUser(AbstractBaseUser, PermissionsMixin):
.....
.....
def send_activation_email(self, email):
email = email
ctx_dict = { 'activation_key' : self.activation_key,
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
}
subject = render_to_string('activation_email_subject.txt', ctx_dict)
subject = ''.join(subject.splitlines())
message = render_to_string('activation_email.txt', ctx_dict)
send_mail(subject, message, 'gccFishing.com', [email], fail_silently = False)
def activate(self, activation_key):
if SHA1_RE.search(activation_key):
try:
self.get(activation_key = activation_key)
except:
self.model.DoesNotExist
return False
if not self.activation_key_expired():
self.is_active = True
self.activation_key = self.model.ACTIVATED
self.save()
return self
return False
What code should go inside activation_email.txt for creating a link that calls the activate method with activation_key ?
Or is there a better way of doing it?
Any help will be awesome. Thanks
Try something like this in your email if you are using the sites django app:
http://{{ site.domain }}{% url registration_activate activation_key %}
Or else change site.domain with the DNS of your site. your
If I remember correct, django-registration already contains an example activation_email.txt in its templates, check it out.
Update
I don't think that you should put the activation login in your User class. You definitely cannot do {% url user.registration_activate activation_key %} since you must pass a View to the url template tag! You cannot create a link without a view function (or a CBV).
What you have to do is to create a view that searches the Profiles for the activation_key and activates it. Check the ActivationView method of django-registration / registration / backends / default / views.py.
If you want to implement custom logic to your app just sublclass ActivationView and use your class in the urls.py instead of the default (django-registration / registration / backends / default / urls.py):
url(r'^activate/(?P<activation_key>\w+)/$', ActivationView.as_view(), name='registration_activate'),