Display something upon a change to a BooleanField in a django database in django app

643 Views Asked by At

I have an uber like django app. A customer can signal that they are looking for someone to provide a service and people in the same area who are looking to be service providers can view a list of actively seeking customers. When the customer presses the button signaling they are looking for a service, they are redirected to a page that says "Thank you, you will be notified when someone is on the way" and it sets attribute active to True. This displays them on the previously mentioned list where customers can be 'claimed.' When a service provider claims a customer. I want the customer's page to display 'Someone will be there shortly" or something of that nature. But how can I let the customer's page (customer_active.html) know that active has been set to False(ie they have been claimed) and then display a message on the occurrence of that event? I have read about potentially using django signals or ajax/jquery but I do not know what the right route is nor how to implement the solution in said route. I have the following code:

models.py:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)

    active = models.BooleanField(default = False)

urls.py

urlpatterns = [
    #home 
    url(r'^home/$', views.home, name = 'home'),

    #/claim/user_id
    url(r'^claim/(?P<user_id>[0-9]+)/$', views.ClaimView.as_view(), name = "claim"),


    #/active
    url(r'^active/$', views.customer_active, name='customer_active'),


]    

    def __str__(self):
        return self.user.username

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

customer_active.html (the customer is looking at this after they signal they are looking for the service):

{% extends 'core/base.html' %}

{% block head %}

    {% load static %}
    <link rel="stylesheet" href="{% static 'core/customer_active.css' %}">

    <title>Active</title>
{% endblock %}
{% block body %}
        <div class="container text-center">
            <div class="row">
                <div class="col-md-12 mb-3">
                    <h1 class="lead">Thank you, {{ user.first_name}} {{user.last_name }}, you will be alerted
                    when someone claims your laundry</h1>
                </div>
            </div>
        </div>


{% endblock %}

home.html:

{% for customer in customers%}
                            <tr>
                                <td style = "text-align:center">{{ customer.user.first_name|title }} {{ customer.user.last_name|title }}</td>
                                <td style = "text-align:center">{{ customer.user.profile.address|title }}</td>
                                <td style = "text-align:center"><a href="{% url 'claim' user_id=customer.user.id %}">Claim</a></td>
                            </tr>
{% endfor %}

views.py:

#the service provider that claimed a customer gets redirected to 'claim.html', upon this 'active' gets set to False
class ClaimView(View):
    def get(self, request, user_id, *args, **kwargs):
        customer = User.objects.get(id=user_id)
        customer.profile.active = False
        customer.save()
        return render(request, 'core/claim.html', {'customer': customer})

def customer_active(request):
    request.user.profile.active = True;
    request.user.save()
    return render(request, 'core/customer_active.html', {'user': request.user})

How can I use ajax/jquery to display a message saying 'Someone will be there shortly" in customer_active.html upon a service provider claiming the customer?

1

There are 1 best solutions below

2
On

So.. if a customer is in customer_active.html page, when service provider claims the customer, the customer should be alerted in customer_active.html page. If I have perceived the problem correctly, then it is a matter of creating a timer that will send Ajax calls every few seconds to check if a service provider has claimed the customer.

    setInterval(function(){
        minAjax({
           url:"/serviceprovider/",//request URL
           type:"GET",//Request type GET/POST
           //CALLBACK FUNCTION with RESPONSE as argument
           success: function(data){
               //check if someone has claimed
               if (data == true){
                   alert("Someone will be there shortly");
               }
           }
       });
     }, 5000); //5000 represents 5 seconds

Views.py

@login_required
def serviceprovider(request):
    claimed=False
    claim=Model.objects.filter(user=User.objects.get(username=request.user), claimed=True)
    if claim:
       claimed=True
    return HttpResponse(json.dumps(claimed))

urls.py

url(r'^serviceprovider/', views.serviceprovider, name = 'serviceprovider'),

You need minAjax for this to work, it is a very small library using pure javascript. Just download the library and put it in your js folder, and import it. I haven't tested the above example but I have used this method in a project and it works. This is just to guide you to the right direction. Good luck!