Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3.  
  4. from django.shortcuts import render
  5. from django.http import HttpResponse
  6. from django.template import loader, RequestContext
  7. from home.models import Cart
  8. from django.template.response import TemplateResponse
  9.  
  10. from rest_framework import status
  11. from rest_framework.decorators import api_view
  12. from rest_framework.response import Response
  13. import MySQLdb
  14. import time
  15. import pymysql
  16.  
  17. #f = open('SQLSetup.txt', "r")
  18. f = open('../blog/SQLSetup.txt', "r")
  19. SQL_username = f.readline()
  20. SQL_username = SQL_username[0: len(SQL_username)-1]
  21. # usernameArray = usernameLine.split(":")
  22. # SQL_username = str(usernameArray[1]).strip()
  23. # if SQL_username.startswith("\"") and SQL_username.endswith("\""):
  24. # SQL_username = SQL_username[1:len(SQL_username) - 1]
  25. SQL_password = f.readline()
  26.  
  27. print("username: " + SQL_username + "\npassword: " + SQL_password)
  28. # passwordArray = passwordLine.split(":")
  29. # SQL_password = str(passwordArray[1]).strip()
  30. # if SQL_password.startswith("\"") and SQL_password.endswith("\""):
  31. # SQL_password = SQL_password[1:len(SQL_password) - 1]
  32.  
  33. db = MySQLdb.connect(host="localhost", user= SQL_username, passwd= SQL_password, db="grocery_store") # name of the database
  34. cur = db.cursor() # creates a cursor to execute queries
  35.  
  36.  
  37. def adminHomePage(request):
  38. template = loader.get_template('home/adminHomePage.html')
  39. context = getQuery()
  40.  
  41. if request.method == 'POST':
  42. delete_input = request.COOKIES.get("Deleting")
  43. if delete_input == "" or delete_input is None:
  44. name = str(request.POST.get('add_name_field'))
  45. weight = str(request.POST.get('add_weight_field'))
  46. cost = str(request.POST.get('add_cost_field'))
  47. quantity = str(request.POST.get('add_quantity_field'))
  48. category = str(request.POST.get('add_category_field'))
  49. image = str(request.POST.get('add_image_field'))
  50. desc = str(request.POST.get('add_desc_field'))
  51.  
  52. add_item_to_db(name, weight, cost, quantity, category, image, desc)
  53. template = loader.get_template("home/adminHomePage.html")
  54. context = getQuery()
  55. return HttpResponse(template.render(context, request))
  56. elif delete_input != "":
  57. delete_from_db(int(delete_input))
  58. print("In deleting statement")
  59. array = request.GET.get('item_array');
  60.  
  61.  
  62. if array is not None:
  63. the_items = array.split(" , ")
  64. the_items = the_items[0:len(the_items) - 1]
  65. i = 1
  66. the_array = []
  67. for theItem in the_items:
  68. item_stuff = theItem.split("<>")
  69. the_array.append(item_stuff)
  70. i += 1
  71. print("the_array length: : "+ str(len(the_array)))
  72. for item in the_array:
  73. if "'" in str(item[7]):
  74. desc = str(item[7]).replace("'", "")
  75. else:
  76. desc = item[7]
  77.  
  78. conn = pymysql.connect(host='localhost', port=3306, user=SQL_username, passwd=SQL_password, db='grocery_store')
  79. cur = conn.cursor()
  80. query = "UPDATE items " \
  81. "SET name = '" + item[1] + "' , weight = '" + item[2] + \
  82. "' , cost = '"+ (item[3]) + \
  83. "' , quantity = '" + (item[4]) + \
  84. "' , categories = '" + item[5] + \
  85. "' , image = '" + item[6] + \
  86. "' , description = '" + desc + \
  87. "' WHERE itemID = " + item[0] + ";"
  88. print(query)
  89. try:
  90. cur.execute(query)
  91. conn.commit()
  92. cur.close()
  93. conn.close()
  94.  
  95. except Exception as e:
  96. print(e)
  97. context = getQuery()
  98. return HttpResponse(template.render(context, request))
  99.  
  100. def getQuery():
  101. items = []
  102. conn = pymysql.connect(host='localhost', port=3306, user=SQL_username, passwd=SQL_password, db='grocery_store')
  103. cur = conn.cursor()
  104. cur.execute("SELECT * FROM Items")
  105. for row in cur.fetchall():
  106. item = {"id": str(row[0]), "name": str(row[1]), "weight": str(row[2]), "cost": str(row[3]),
  107. "quantity": str(row[4]),
  108. "tags": str(row[5]), "image": str(row[6]), "description": str(row[7])}
  109. items.append(item)
  110. context = {"items": items}
  111. cur.close()
  112. conn.close()
  113. return context
  114.  
  115. def add_item_to_db(name, weight, cost, quantity, category, image, desc):
  116. query = "INSERT INTO items " \
  117. "( name, weight, cost, quantity, categories, image, description) " \
  118. "VALUES('" + name + "', '" + weight + "', '" + cost + "', '"\
  119. + quantity + "', '" + category + "', '" + image + "', '" + desc + "');"
  120. print(query)
  121. conn = pymysql.connect(host='localhost', port=3306, user=SQL_username, passwd=SQL_password, db='grocery_store')
  122. cur = conn.cursor()
  123. cur.execute(query)
  124. conn.commit()
  125. cur.close()
  126. conn.close()
  127.  
  128. def delete_from_db(id):
  129. query = "DELETE FROM items WHERE itemID = " + str(id)
  130. conn = pymysql.connect(host='localhost', port=3306, user=SQL_username, passwd=SQL_password, db='grocery_store')
  131. cur = conn.cursor()
  132. cur.execute(query)
  133. conn.commit()
  134. cur.close()
  135. conn.close()
  136.  
  137.  
  138.  
  139. def login_controller(request):
  140. username = request.POST['Username']
  141. password = request.POST['Password']
  142. #first_person = Person.objects.raw("SELECT * FROM register_user where userid = 'username' ")[0]
  143. print("username: " + username + "\npassword: " + password)
  144. if username is 'admin@admin.com':
  145. if password is 'admin':
  146. print("This is the admin")
  147. conn = pymysql.connect(host='localhost', port=3306, user= SQL_username, passwd=SQL_password, db='grocery_store')
  148. cur = conn.cursor()
  149.  
  150. query = 'SELECT name,userid FROM register_user where email = "' + str(username) + '" AND password = "' + str(password) + '"'
  151. #return HttpResponse(query)
  152. cur.execute(query)
  153.  
  154. name = ""
  155.  
  156. for r in cur:
  157. name = (r[0],r[1])
  158.  
  159. cur.close()
  160. conn.close()
  161.  
  162. if(name != ""):
  163. return ("1",name)
  164. else:
  165. return ("0","error")
  166.  
  167.  
  168. def sign_up_controller(request):
  169. email = request.POST['email']
  170. email = "'"+str(email)+"'"
  171.  
  172. name = request.POST['name']
  173. token = name
  174. realname = name
  175. name = "'"+str(name)+"'"
  176.  
  177. password = request.POST['password']
  178. token = token + password
  179.  
  180. token = "'"+token+"'"
  181.  
  182. password = "'"+str(password)+"'"
  183.  
  184. repeat_password = request.POST['repeat_password']
  185. repeat_password = "'"+str(repeat_password)+"'"
  186.  
  187.  
  188.  
  189. #first_person = Person.objects.raw("SELECT * FROM register_user where userid = 'username' ")[0]
  190.  
  191. conn = pymysql.connect(host='localhost', port=3306, user='root', passwd=dbpswd, db='grocery_store')
  192. cur = conn.cursor()
  193.  
  194. #query = 'SELECT name FROM register_user where email = "' + str(username) + '" AND password = "' + str(password) + '"'
  195. query = "INSERT INTO register_user (name,email,password,token) VALUES ( " + name +","+ email +","+ password+","+ token + ")"
  196. #return HttpResponse(query)
  197.  
  198. try:
  199. cur.execute(query)
  200. conn.commit()
  201. response = ("1",realname)
  202.  
  203.  
  204. except Exception as e:
  205. response = ("0",e)
  206. #raise
  207. else:
  208. pass
  209. finally:
  210. cur.close()
  211. conn.close()
  212.  
  213.  
  214. return response
  215.  
  216.  
  217.  
  218. def shoppingcart(request):
  219. context = RequestContext(request)
  220. data = Cart.objects.all()
  221. #if request.method == 'POST':
  222. #do something
  223. return TemplateResponse(request, 'home/shoppingcart.html', {"data" : data})
  224. # Create your views here.
  225.  
  226.  
  227. # Login controller if request is login then
  228.  
  229. def login_register_request(request):
  230. # render simple static page
  231. """
  232. Retrieve, update or delete a code snippet.
  233. """
  234. if request.method == 'GET':
  235. return HttpResponse("This is get request")
  236.  
  237. elif request.method == 'POST':
  238. return HttpResponse("This is POST request")
  239. else :
  240. template = loader.get_template('home/adminLogin.html')
  241. context = {}
  242. return HttpResponse(template.render(context, request))
  243.  
  244. def login_register(request):
  245. template = loader.get_template('home/adminLogin.html')
  246. context = {}
  247. return HttpResponse(template.render(context, request))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement