Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. @csrf_exempt
  2. def addCinemaFunction(request):
  3.     if request.session["function"] == "admin" or request.session["function"] == "manager":
  4.         with open('data_json.json', 'r', encoding='utf-8') as file:
  5.             data_json = json.load(file)
  6.         req = request.POST
  7.         name = req.get('name')
  8.         location = req.get('location')
  9.         phone = req.get('phone')
  10.         if name != None and location != None and phone != None and \
  11.                 name != "" and location != "" and phone != "":
  12.             flag = False
  13.             last_id = int(data_json['cinemas'][-1]['id']) + 1
  14.             for cinema in data_json['cinemas']:
  15.                 if name == cinema['name']:
  16.                     flag = True
  17.                     break
  18.             if flag == False:
  19.                 new_obj = {
  20.                     'id': str(last_id),
  21.                     'name': name,
  22.                     'location': location,
  23.                     'contacts': {
  24.                         'phone': phone,
  25.                     },
  26.                     'films': [],
  27.  
  28.                 }
  29.                 data_json['cinemas'].append(new_obj)
  30.                 with open('data_json.json', 'w', encoding='utf-8') as file:
  31.                     file.write(json.dumps(data_json, ensure_ascii=False, separators=(',', ': '), indent=2))
  32.                 return redirect('guest')
  33.             else:
  34.                 return render(request, 'json/addCinema.html', {})
  35.         else:
  36.             return render(request, 'json/addCinema.html', {})
  37.     else:
  38.         return redirect('/')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement