moodleNotifer
views.py
Go to the documentation of this file.
1 
5 from django.core.exceptions import ValidationError
6 from django.shortcuts import render, redirect
7 from django.http.response import HttpResponseRedirect
8 from django.http import HttpResponse, response
9 from django.contrib.auth import default_app_config, login, authenticate
10 
11 
14 from .forms import CreateNewUser
15 from .models import Student
16 from .updateDB import new_user_firstContact
17 from .timely_update import update_database_daily
18 from .timely_update import send_update_to_students
19 from twilio.twiml.messaging_response import MessagingResponse
20 from .utils import fetch_reply
21 from django.views.decorators.csrf import csrf_exempt
22 from .clockinterval import sendupdates
23 
24 
27 
28 
31 def index(response):
32  return render(response, "main/base.html",{})
33 
34 
37 def updater(response):
38  sendupdates()
39  return render(response, "main/base.html",{})
40 
41 
44 def home(response):
45  return render(response, "main/home.html",{})
46 
47 
48 
51 def success(response):
52  return render(response, "main/success.html", {})
53 
54 
56 @csrf_exempt
57 def sms_reply(request):
58  """Respond to incoming calls with a simple text message."""
59  # Fetch the message
60  if request.method == "POST":
61  # Create reply
62  message = request.POST['Body']
63  phone_no=request.POST['From']
64  print(phone_no)
65  if phone_no :
66  phone_no = phone_no[-10:]
67  if Student.objects.filter(phone_number = phone_no).exists():
68  reply=fetch_reply(message,phone_no)
69  resp = MessagingResponse()
70  resp.message(reply)
71 
72  return HttpResponse(resp)
73  else:
74  resp = MessagingResponse()
75  resp.message("You are not registered!!")
76  return HttpResponse(resp)
77  else:
78  resp = MessagingResponse()
79  resp.message("Phone Number Not found!!")
80  return HttpResponse(resp)
81 
82 
85 def register(response):
86  if response.method == "POST":
87  form = CreateNewUser(response.POST)
88  if form.is_valid():
89  fn = form.cleaned_data["fname"]
90  pnumb = form.cleaned_data["phone_number"]
91  rnumb = form.cleaned_data["roll_number"]
92  token = form.cleaned_data["moodle_token"]
93 
94  '''Other Functions are called from here
95  to update the database table Student entries'''
96 
97  course_taken,updater, update_subs = new_user_firstContact(token)
98  course_taken = ','.join(course_taken)
99  update_subs = ','.join(update_subs)
100 
101  if(updater == True):
102  user = "updater"
103  else :
104  user = "General"
105 
106  t = Student(fname=fn, phone_number=pnumb, roll_number=rnumb, moodle_token=token,user_type = user,courses=course_taken,update_courses= update_subs)
107  t.save()
108  #update_database_daily()
109  #sendupdates(
110  return redirect("/success")
111  else:
112  form = CreateNewUser()
113 
114  return render(response, "main/register.html", {"form":form})