Sending emails after updating the model from the admin page (Django) - django

Hello I want to know if there is a way to send an email to a user upon someone updating certain fields in the Django admin page being updated. Is there a way to do this? I already have an email being sent upon the forms completion, but I need to send more emails once one of the users updates through the admin page.
I have taken a look at the post_save, model_save and save_formset methods, but I did not feel that they were what I was looking for.

Try overriding the ModelAdmin.save_model method. I think it has hooks for all the information you require.
The change variable lets you distinguish between a user adding or changing the model instance.
form.changed_data gives you a list of the names of the fields which have changed, which lets you determine whether or not to send the email.
Finally request.user identifies the user which made the changes.

You need django.db.models.signals.post_save signal. It is sanding after the model has been saved.
def my_callback(sender, **kwargs):
# Your specific logic here
pass
post_syncdb.connect(my_callback, sender=yourapp.models.TheModel)
Arguments sent with this signal:
sender:
The model class.
instance:
The actual instance being saved.
created
A boolean; True if a new record was created.
raw:
A boolean; True if the model is saved exactly as presented (i.e. when loading a fixture). One should not query/modify other records in the database as the database might not be in a consistent state yet.
So you need only callback and sender.

Related

How can i confirm a database entry in django admin?

I have been working around this problem. I have a user submitting a HTML form lets say to list their hotel on my website. I need to review this form before i add it into my hotels model to be published.
One approach i have worked on is to use a requests model where this form can be stored and later using django admin action write a custom action to add/remove the request. In case of acceptance i copy the details to my hotels model else it sends an email or a notification back to user.
Second approach is simply using django action on hotels model where the request is sent to approve it or reject it. In this case i want to know if thats possible where a data point doesn't get written to database until it's been accepted by admin. If yes how can i do that?
Finally, these details are displayed on my main page and search page for users to book these places.
If there is a better and effective way to do it. Please share that.
Thanks in advance. If something isn't clear i can answer your specific questions in the comments below 😊.
Have a nice day.
You can have is_published Boolean field in your hotel model and you can default it to false initially. After you inspect the hotel details you can set the is_published field to True from django admin.
So now whenever you are querying for hotels to show on your website. You can query
Hotel.objects.filter(is_published=True)

How to update a model field using templates?

I am working on a project that allows users to register, but the model contains a verified field and only admin can change it. User can log in only when verified = True. Also, I need to implement that once a user is verified, he gets notified through a mail. Is there a way to connect the mail url to the verified field? If it can be done through templates, How? I want to redirect to mail url as soon as admin updates the verified field.

Django send email to the user when a specific field on a model has bean changed

I want to send an email to a user on model change.
Here is what I have : I have a model called package containing a field status and a field owner , I want to send an email to the owner of the package when the status is changed.
Is there a way to do so ?
Thank you
Signals might help you out.
Think of Signal methods as code that is always executed before or after an operation on a model; e.g, a pre_save() signal is called just before a record is saved to database, post_delete() is called just after a record is deleted from the database.
Implement a pre_save() signal on the model and get the value of status field from the database before saving. Check if this previous value is different from the one for the corresponding status field in update_fields. If so, email the user.
Here are the docs for pre_save()
and Signals.
Here's a tutorial I followed while learning Signals:
How to Create Django Signals
- SimpleIsBetterThanComplex.
Hope this helps :)
When the event of changing status is triggered, send the email to the owner.
Documentation: https://docs.djangoproject.com/en/1.11/topics/email/
Or if your prefer the built-in notification:How to use django-notification to inform a user when somebody comments on their post

django-social-auth registration extra step

I'm using django-social-auth & custom User model (django 1.5) with extra fields which are required. I need the user to fill these fields before the models is saved. Is there any way to "interrupt" the django-social-auth right after the backend authorization succeeded, but before the new User model is created, in order to show the form asking for account details?
Thanks in advance
You add a pipeline entry which returns a HttpResponseRedirect to any place you need. Check the example app with django-social-auth, it cuts the pipeline in two places (here and here) to ask the user some extra information. The first one takes place before the user instance is created.
Check the save_status_to_session call before each break, that is required to ensure the pipeline status when it's continued.

Django logged in user in models.py or signal handler

Is there a way to access the request.user in either models.py or in a signal handler?
I'm using the m2m_changed signal and defining it in my models.py - I'd like to access the logged in user there.
Is there a way to do this?
I'm assuming user making a change is not necessarily record owner or author. This means model lookups are useless and you need to pass this data via signal.
Good way to do this is to create custom signal which has current user as one of attributes and emit it in view code when the data is being saved.