Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. from bottle import get, post, run, request, response
  2. import sqlite3
  3. import json
  4.  
  5. conn = sqlite3.connect("movies.sqlite")
  6.  
  7. def hash(msg):
  8. import hashlib
  9. return hashlib.sha256(msg.encode('utf-8')).hexdigest()
  10.  
  11. @get('/ping')
  12. def ping():
  13. # response.content_type = 'application/json'
  14. response.status = 200
  15. return 'pong \n'
  16.  
  17.  
  18. @post('/reset')
  19. def reset_tables():
  20. c = conn.cursor()
  21. c.executescript("""
  22.  
  23. DELETE
  24. FROM theatres;
  25.  
  26. DELETE
  27. FROM movies;
  28.  
  29. DELETE
  30. FROM tickets;
  31.  
  32. DELETE
  33. FROM customers;
  34.  
  35. DELETE
  36. FROM screenings;
  37.  
  38. INSERT OR IGNORE
  39. INTO customers(username, full_name, c_password)
  40. VALUES ('alice', 'Alice', 'dobido'),
  41. ('bob', 'Bob', 'whatsinaname');
  42.  
  43. INSERT OR IGNORE
  44. INTO movies(m_title, production_year, imdb_key, running_time)
  45. VALUES ('The Shape of Water', 2017, 'tt5580390', 120),
  46. ('Moonlight', 2016, 'tt4975722', 140),
  47. ('Spotlight', 2015, 'tt1895587', 90),
  48. ('Birdman', 2014, 'tt2562232', 107);
  49.  
  50. INSERT OR IGNORE
  51. INTO theatres(th_name, capacity)
  52. VALUES ('Kino', 10),
  53. ('Södran', 16),
  54. ('Skandia', 100);
  55. """
  56. )
  57. conn.commit()
  58. u = [{"username": username, "full name": fullname, "password": c_password}
  59. for (username, fullname, password) in c]
  60.  
  61. t = [{"th_name": th_name, "capacity": capacity}
  62. for (th_name, capacity) in c]
  63. response.status = 200
  64. return 'OK\n'
  65.  
  66. @get('/theatres')
  67. def get_theatres():
  68. c = conn.cursor()
  69. c.execute(
  70. """
  71. SELECT *
  72. FROM theatres
  73. WHERE 1=1
  74. """
  75. )
  76. s = [{"th_name": th_name, "capacity": capacity}
  77. for (th_name, capacity) in c]
  78. return json.dumps({"data": s}, indent=4)
  79.  
  80. @get('/movies')
  81. def get_movies():
  82. c = conn.cursor()
  83. query = """
  84. SELECT *
  85. FROM movies
  86. WHERE 1=1
  87. """
  88. params = []
  89. if request.query.title:
  90. query += "AND m_title = ? "
  91. params.append(request.query.title)
  92. if request.query.year:
  93. query += "AND production_year = ? "
  94. params.append(request.query.year)
  95. if request.query.imdb:
  96. query += "AND imdb_key = ?"
  97. params.append(request.query.imdb)
  98. c.execute(
  99. query,
  100. params
  101. )
  102. m = [{"title": title, "year": year, "imdb_key": imdb, "running_time": running_time}
  103. for (title, year, imdb, running_time) in c]
  104.  
  105. response.status = 200
  106. return json.dumps({'data': m}, indent=4)
  107.  
  108. @get('/movies/<imdb>')
  109. def get_movie(imdb):
  110. c = conn.cursor()
  111. c.execute(
  112. """
  113. SELECT *
  114. FROM movies
  115. WHERE imdb_key = ?
  116. """,
  117. [imdb]
  118. )
  119. m = [{"title": title, "year": year, "imdb": imdb, "running_time": running_time}
  120. for (title, year, key, running_time) in c]
  121. response.status = 200
  122. return json.dumps({'data': m}, indent=4)
  123.  
  124. @get('/performances')
  125. def get_screenings():
  126.  
  127. query = """
  128. SELECT sc_id, sc_date, start_time, th_name, imdb_key
  129. FROM screenings
  130. JOIN theatres
  131. USING (th_name)
  132. JOIN movies
  133. USING (imdb_key)
  134. WHERE 1=1
  135. """
  136. params = []
  137. if request.query.sc_id:
  138. query += "AND sc_id = ? "
  139. params.append(request.query.sc_id)
  140. if request.query.date:
  141. query += "AND sc_date = ? "
  142. params.append(request.query.date)
  143. if request.query.time:
  144. query += "AND start_time = ? "
  145. params.append(request.query.time)
  146. if request.query.imdb:
  147. query += "AND imdb_key = ?"
  148. params.append(request.query.imdb)
  149. if request.query.theater:
  150. query += "AND th_name = ?"
  151. params.append(request.query.theater)
  152. c = conn.cursor()
  153. c.execute(
  154. query,
  155. params
  156. )
  157. s = [{"screening_id": sc_id, "date": date, "time": time, "theatre": theater, "imdb_key": imdb}
  158. for (sc_id, date, time, theater, imdb) in c]
  159. response.status = 200
  160. return json.dumps({"data": s}, indent=4)
  161.  
  162.  
  163. # curl -X POST http://localhost:7007/performances\?imdb=<imdb>\&theater=<theater>\&date=<date>\&time=<time>
  164. @post('/performances')
  165. def post_screening():
  166. date = request.query.date
  167. time = request.query.time
  168. theater = request.query.theater
  169. imdb = request.query.imdb
  170. if not (date and time and theater and imdb):
  171. return 'NOT OK'
  172. c = conn.cursor()
  173. c.execute(
  174. """
  175. INSERT OR IGNORE
  176. INTO screenings(sc_date, start_time, th_name, imdb_key)
  177. VALUES(?,?,?,?)
  178. """,
  179. [date, time, theater, imdb]
  180. )
  181. conn.commit()
  182. response.status = 200
  183. return 'OK'
  184.  
  185. @post('/tickets')
  186. def post_ticket():
  187. user = request.query.user
  188. sc_id = request.query.sc_id
  189. if not (user and sc_id):
  190. response.status = 400
  191. return 'NOT OK'
  192. c = conn.cursor()
  193. c.execute(
  194. """
  195. INSERT
  196. INTO tickets(username, sc_id)
  197. VALUES(?,?)
  198. """,
  199. [user, sc_id]
  200. )
  201. conn.commit()
  202. response.status = 200
  203. return 'OK'
  204.  
  205. @get('/tickets')
  206. def get_tickets():
  207. query = """
  208. SELECT ti_id, username, sc_id, th_name, sc_date, start_time, imdb_key
  209. FROM tickets
  210. JOIN screenings
  211. USING (sc_id)
  212. WHERE 1=1
  213. """
  214. params = []
  215.  
  216. if request.query.ti_id:
  217. query += "AND ti_id = ? "
  218. params.append(request.query.ti_id)
  219. if request.query.user:
  220. query += "AND username = ? "
  221. params.append(request.query.user)
  222. if request.query.sc_id:
  223. query += "AND sc_id = ?"
  224. params.append(request.query.sc_id)
  225. if request.query.theater:
  226. query += "AND th_name =?"
  227. params.append(request.query.theater)
  228. if request.query.date:
  229. query += "AND sc_date = ? "
  230. params.append(request.query.date)
  231. if request.query.time:
  232. query += "AND start_time = ? "
  233. params.append(request.query.time)
  234. if request.query.imdb:
  235. query += "AND imdb_key = ?"
  236. params.append(request.query.imdb)
  237. c = conn.cursor()
  238. c.execute(
  239. query,
  240. params
  241. )
  242. s = [{"ti_id": ti_id, "username": user, "sc_id": sc_id, "theater": theater, "date": date, "time": time, "imdb": imdb}
  243. for (ti_id, user, sc_id, theater, date, time, imdb) in c]
  244. response.status = 200
  245. return json.dumps({"data": s}, indent=4)
  246.  
  247. run(host='localhost', port=7007)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement