PageRenderTime 69ms CodeModel.GetById 1ms app.highlight 63ms RepoModel.GetById 1ms app.codeStats 0ms

/account/views.py

https://bitbucket.org/JishnuTU/d-m-intelligence-s
Python | 408 lines | 388 code | 20 blank | 0 comment | 59 complexity | ca2a1d8e329425df2478204f8555cc1d MD5 | raw file
  1from django.shortcuts import render ,redirect
  2
  3from home.models import User
  4
  5import json
  6
  7from models import Volunteer,Hospital,Pronearea,Accomadation
  8
  9from controlunit.models import  Food,Water,Machine,TransportGoods,TransportHuman,Rescuetool,Firstaid,Messagebox
 10
 11from home.tests import index
 12
 13def landinpage(request):
 14
 15    if 'username' not in request.session:
 16        return redirect(index)
 17    return render(request,'homepage.html',{'username':request.session['username']})
 18
 19
 20def logout(request):
 21    try:
 22        del request.session['username']
 23    except:
 24        pass
 25    return redirect(index)
 26
 27
 28def updateresources(request):
 29    if 'username' not in request.session:
 30        return redirect(index)
 31
 32    new_user = User.objects.get(username=request.session['username'])
 33    cate =new_user.category
 34    try:
 35        f = Food.objects.get(user=new_user)
 36        food = f.packet
 37    except Food.DoesNotExist:
 38        food = 0
 39    try:
 40        w = Water.objects.get(user=new_user)
 41        water = w.quantity
 42    except Water.DoesNotExist:
 43        water = 0
 44    try:
 45        fa = Firstaid.objects.get(user=new_user)
 46        firstaid = fa.kit
 47    except Firstaid.DoesNotExist:
 48        firstaid = 0
 49    try:
 50        r = Rescuetool.objects.get(user=new_user)
 51        rescuetools = r.box
 52    except Rescuetool.DoesNotExist:
 53        rescuetools = 0
 54
 55    try:
 56        m = Machine.objects.get(user=new_user)
 57        fireengine = m.fireengine
 58        ambulance = m.ambulance
 59        jcb = m.jcb
 60    except Machine.DoesNotExist:
 61        fireengine = 0
 62        ambulance = 0
 63        jcb = 0
 64    try:
 65        t = TransportHuman.objects.get(user=new_user)
 66        tgoods = t.capacity
 67    except TransportHuman.DoesNotExist:
 68        tgoods = 0
 69
 70    try:
 71        t = TransportGoods.objects.get(user=new_user)
 72        thuman=t.capacity
 73    except TransportGoods.DoesNotExist:
 74        thuman=0
 75
 76    if "submitfood" in request.POST:
 77        try:
 78            f = Food.objects.get(user=new_user)
 79        except Food.DoesNotExist:
 80            f=Food()
 81            f.user=new_user
 82        f.packet = request.POST['packet']
 83        f.save()
 84        return render(request, 'updateres.html',    {'username': request.session['username'], 'req': "success" ,
 85                                                    'cat' : new_user.category,
 86                                                    'food' : f.packet,
 87                                                    'water' :water,
 88                                                    'firstaid':firstaid,
 89                                                    'rescuetool' : rescuetools,
 90                                                    'fireengine' : fireengine,
 91                                                    'jcb' : jcb,
 92                                                    'ambulance':ambulance,
 93                                                    'tgoods' : tgoods,
 94                                                    'thuman' : thuman,
 95                                                    'category': json.dumps(cate)
 96                                                    })
 97    if "submitwater" in request.POST:
 98        try:
 99            w = Water.objects.get(user=new_user)
100        except Water.DoesNotExist:
101            w = Water()
102            w.user=new_user
103        w.quantity = request.POST['quantity']
104        w.save()
105        return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
106                                                  'cat': new_user.category,
107                                                  'food': food,
108                                                  'water': w.quantity ,
109                                                  'firstaid': firstaid,
110                                                  'rescuetool': rescuetools,
111                                                  'fireengine': fireengine,
112                                                  'jcb': jcb,
113                                                  'ambulance': ambulance,
114                                                  'tgoods': tgoods,
115                                                  'thuman': thuman,
116                                                  'category': json.dumps(cate)
117                                                  })
118    if "submitfirstaid" in request.POST:
119        try:
120            fa = Firstaid.objects.get(user=new_user)
121        except Firstaid.DoesNotExist:
122            fa = Firstaid()
123            fa.user = new_user
124        fa.kit = request.POST['kit']
125        fa.save()
126        return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
127                                                  'cat': new_user.category,
128                                                  'food': food,
129                                                  'water': water,
130                                                  'firstaid': fa.kit,
131                                                  'rescuetool': rescuetools,
132                                                  'fireengine': fireengine,
133                                                  'jcb': jcb,
134                                                  'ambulance': ambulance,
135                                                  'tgoods': tgoods,
136                                                  'thuman': thuman,
137                                                  'category': json.dumps(cate)
138                                                  })
139    if "submitrescuetool" in request.POST:
140        try:
141            r = Rescuetool.objects.get(user=new_user)
142        except Rescuetool.DoesNotExist:
143            r = Rescuetool()
144            r.user = new_user
145        r.box = request.POST['box']
146        r.save()
147        return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
148                                                  'cat': new_user.category,
149                                                  'food': food,
150                                                  'water': water,
151                                                  'firstaid': firstaid,
152                                                  'rescuetool': r.box,
153                                                  'fireengine': fireengine,
154                                                  'jcb': jcb,
155                                                  'ambulance': ambulance,
156                                                  'tgoods': tgoods,
157                                                  'thuman': thuman,
158                                                  'category': json.dumps(cate)
159                                                  })
160    if "submitmachine" in request.POST:
161        try:
162            m = Machine.objects.get(user=new_user)
163        except Machine.DoesNotExist:
164            m = Machine()
165            m.user = new_user
166        m.fireengine = request.POST['fireengine']
167        m.jcb = request.POST['jcb']
168        m.ambulance = request.POST['ambulance']
169        m.save()
170        return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
171                                                  'cat': new_user.category,
172                                                  'food': food,
173                                                  'water': water,
174                                                  'firstaid': firstaid,
175                                                  'rescuetool': rescuetools,
176                                                  'fireengine': m.fireengine,
177                                                  'jcb': m.jcb,
178                                                  'ambulance': m.ambulance,
179                                                  'tgoods': tgoods,
180                                                  'thuman': thuman,
181                                                  'category': json.dumps(cate)
182                                                  })
183    if "submittransport" in request.POST:
184        if request.POST['typeofvehicle']=="Goods":
185            try:
186                t = TransportGoods.objects.get(user=new_user)
187            except TransportGoods.DoesNotExist:
188                t = TransportGoods()
189                t.user = new_user
190            t.capacity = request.POST['capacity']
191            t.save()
192            return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
193                                                      'cat': new_user.category,
194                                                      'food': food,
195                                                      'water': water,
196                                                      'firstaid': firstaid,
197                                                      'rescuetool': rescuetools,
198                                                      'fireengine': fireengine,
199                                                      'jcb': jcb,
200                                                      'ambulance': ambulance,
201                                                      'tgoods': t.capacity,
202                                                      'thuman': thuman,
203                                                      'category': json.dumps(cate)
204                                                      })
205        if request.POST['typeofvehicle']=="Human":
206            try:
207                t = TransportHuman.objects.get(user=new_user)
208            except TransportHuman.DoesNotExist:
209                t = TransportHuman()
210                t.user = new_user
211            t.capacity = request.POST['capacity']
212            t.save()
213            return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
214                                                      'cat': new_user.category,
215                                                      'food': food,
216                                                      'water': water,
217                                                      'firstaid': firstaid,
218                                                      'rescuetool': rescuetools,
219                                                      'fireengine': fireengine,
220                                                      'jcb': jcb,
221                                                      'ambulance': ambulance,
222                                                      'tgoods': tgoods,
223                                                      'thuman': t.capacity,
224                                                      'category': json.dumps(cate)
225                                                      })
226
227    if "submitaid" in request.POST:
228        v = Volunteer.objects.get(user=new_user)
229        v.humanaid=request.POST['humanaid']
230        v.save()
231        return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
232                                                  'cat': new_user.category,
233                                                  'food': food,
234                                                  'water': water,
235                                                  'firstaid': firstaid,
236                                                  'rescuetool': rescuetools,
237                                                  'fireengine': fireengine,
238                                                  'jcb': jcb,
239                                                  'ambulance': ambulance,
240                                                  'tgoods': tgoods,
241                                                  'thuman': thuman,
242                                                  'category': json.dumps(cate)
243                                                  })
244    if "submitcapacity" in request.POST:
245        a =Accomadation.objects.get(user=new_user)
246        a.capacity=request.POST['capacity']
247        a.save()
248        return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
249                                                  'cat': new_user.category,
250                                                  'food': food,
251                                                  'water': water,
252                                                  'firstaid': firstaid,
253                                                  'rescuetool': rescuetools,
254                                                  'fireengine': fireengine,
255                                                  'jcb': jcb,
256                                                  'ambulance': ambulance,
257                                                  'tgoods': tgoods,
258                                                  'thuman': thuman,
259                                                  'category': json.dumps(cate)
260                                                  })
261    if "submitpopulation" in request.POST:
262        p =Pronearea.objects.get(user=new_user)
263        p.population=request.POST['population']
264        p.save()
265        return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
266                                                  'cat': new_user.category,
267                                                  'food': food,
268                                                  'water': water,
269                                                  'firstaid': firstaid,
270                                                  'rescuetool': rescuetools,
271                                                  'fireengine': fireengine,
272                                                  'jcb': jcb,
273                                                  'ambulance':ambulance,
274                                                  'tgoods': tgoods,
275                                                  'thuman': thuman,
276                                                  'category': json.dumps(cate)
277                                                  })
278    if "submitcanoccupy" in request.POST:
279        h =Hospital.objects.get(user=new_user)
280        h.canoccupy=request.POST['canoccupy']
281        h.save()
282        return render(request, 'updateres.html', {'username': request.session['username'], 'req': "success",
283                                                  'cat': new_user.category,
284                                                  'food': food,
285                                                  'water': water,
286                                                  'firstaid': firstaid,
287                                                  'rescuetool': rescuetools,
288                                                  'fireengine': fireengine,
289                                                  'jcb': jcb,
290                                                  'ambulance': ambulance,
291                                                  'tgoods': tgoods,
292                                                  'thuman': thuman,
293                                                  'category': json.dumps(cate)
294                                                  })
295    return render(request, 'updateres.html', {'username': request.session['username'],
296                                                  'cat': new_user.category,
297                                                  'food': food,
298                                                  'water': water,
299                                                  'firstaid': firstaid,
300                                                  'rescuetool': rescuetools,
301                                                  'fireengine': fireengine,
302                                                  'jcb': jcb,
303                                                  'ambulance': ambulance,
304                                                  'tgoods': tgoods,
305                                                  'thuman': thuman,
306                                                   'category': json.dumps(cate)
307                                                  })
308
309def requestres(request):
310    if 'username' not in request.session:
311        return redirect(index)
312    usr=User.objects.get(username=request.session['username'])
313    if usr.category=='volunteer':
314        mem=Volunteer.objects.get(user=usr)
315    if usr.category == 'hospital':
316        mem = Hospital.objects.get(user=usr)
317    if usr.category == 'accommodation':
318        mem = Accomadation.objects.get(user=usr)
319    if usr.category == 'pronearea':
320        mem = Pronearea.objects.get(user=usr)
321    addr = mem.loyalto
322    stat = mem.status
323    if(stat==0):
324        return render(request, 'request.html', {'username': request.session['username'], 'statuserror': "success"})
325    else:
326        m = Messagebox()
327        if "submitfood" in request.POST:
328            m.fromaddr=request.session['username']
329            m.toaddr=addr
330            m.topic="Request"
331            m.message="Food"
332            m.parameter=request.POST['food']
333            m.save()
334            return render(request, 'request.html', {'username': request.session['username'], 'req': "success"})
335        if 'submitwater' in request.POST:
336            m.fromaddr=request.session['username']
337            m.toaddr=addr
338            m.topic="Request"
339            m.message = "Water"
340            m.parameter = request.POST['water']
341            m.save()
342            return render(request, 'request.html', {'username': request.session['username'], 'req': "success"})
343        if 'submitfirstaid' in request.POST:
344            m.fromaddr=request.session['username']
345            m.toaddr=addr
346            m.topic="Request"
347            m.message = "FirstAid"
348            m.parameter = request.POST['firstaid']
349            m.save()
350            return render(request, 'request.html', {'username': request.session['username'], 'req': "success"})
351        if 'submitrescue' in request.POST:
352            m.fromaddr=request.session['username']
353            m.toaddr=addr
354            m.topic="Request"
355            m.message = "Rescue"
356            m.parameter = request.POST['rescue']
357            m.save()
358            return render(request, 'request.html', {'username': request.session['username'], 'req': "success"})
359        if 'submitmachine' in request.POST:
360            m.fromaddr=request.session['username']
361            m.toaddr=addr
362            m.topic="Request"
363            if'fireengine' in request.POST:
364                m.message = "Machine"
365                m.parameter = request.POST['fireengine']
366                m.save()
367            if'ambulance' in request.POST:
368                m.message = "Machine"
369                m.parameter = request.POST['ambulance']
370                m.save()
371            if'jcb' in request.POST:
372                m.message = "Machine"
373                m.parameter=request.POST['jcb']
374                m.save()
375            return render(request, 'request.html', {'username': request.session['username'],'req':"success"})
376        else:
377            return render(request, 'request.html', {'username': request.session['username']})
378
379def messages(request):
380    if 'username' not in request.session:
381        return redirect(index)
382    try:
383        if 'ack' in request.POST:
384            inboxobj=Messagebox.objects.get(id=request.POST['ack'])
385            inboxobj.ack=1
386            inboxobj.save()
387        if 'nack' in request.POST:
388            inboxobj=Messagebox.objects.get(id=request.POST['nack'])
389            inboxobj.nack=1
390            inboxobj.save()
391        inbox=Messagebox.objects.filter(toaddr=request.session['username'],nack=0,ack=0)
392        return render(request, 'inbox.html', {'username': request.session['username'],
393                                              'inbox':inbox
394                                            })
395    except Messagebox.DoesNotExist:
396        return render(request, 'inbox.html', {'username': request.session['username']})
397
398def outbox(request):
399    if 'username' not in request.session:
400        return redirect(index)
401    try:
402
403        inbox=Messagebox.objects.filter(fromaddr=request.session['username'])
404        return render(request, 'outbox.html', {'username': request.session['username'],
405                                              'inbox':inbox
406                                            })
407    except Messagebox.DoesNotExist:
408        return render(request, 'outbox.html', {'username': request.session['username']})