Advertisement
Guest User

Untitled

a guest
Jan 31st, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.23 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Jan 26 16:30:19 2017
  4.  
  5. @author: nja
  6. """
  7. import pyodbc
  8. from flask import Flask
  9. from flask import request
  10. import glob
  11. import os
  12. import random
  13.  
  14.  
  15. connection = pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};'
  16.                                'Server=tcp:nja-training.database.windows.net,1433;'
  17.                                'Database=RateMe;Uid=nja-admin@nja-training;'
  18.                                'Pwd={Porsche911};'
  19.                                'Encrypt=yes;'
  20.                                'TrustServerCertificate=no;'
  21.                                'Connection Timeout=30;')
  22.                                
  23. cursor = connection.cursor()
  24.  
  25. app = Flask(__name__)
  26.  
  27. @app.route('/postScore/')
  28. def postScore():
  29.     direc = "D:\\DTU\\FlotGrim\\images\\exploretagsselfiegirl\\"
  30.     os.chdir(direc)
  31.    
  32.     score = request.args.get('score')
  33.     imageId = request.args.get('imageId')
  34.     userName = request.args.get('userName')
  35.    
  36.     cursor.execute(
  37.     """
  38.    select userID
  39.    from [dbo].[Users]
  40.    where UserName = ?
  41.    """, userName)
  42.    
  43.     ID = cursor.fetchall()[0][0]
  44.  
  45.     cursor.execute(
  46.         """
  47.        UPDATE [dbo].[Scores]
  48.        SET [Score] = ?
  49.        WHERE UserID = ? AND ImageId = ?
  50.        """, score, ID, imageId)
  51.    
  52.     cursor.commit()
  53.    
  54.     return "Success posting score"
  55. '''
  56. Returns an image and removes it from the database
  57. '''
  58. @app.route('/getImage/')
  59. def getImage():
  60.     userName = request.args.get('userName')
  61.    
  62.     cursor.execute(
  63.         """
  64.        SELECT *
  65.        FROM [dbo].[Scores]
  66.        WHERE Score IS NULL AND UserName = ?
  67.        """, userName)
  68.    
  69.     ID = cursor.fetchall()
  70.    
  71.     i = random.randrange(len(ID)) # get random index
  72.     image = ID[i][2]
  73.        
  74.     return image
  75.    
  76. '''
  77. Creates a user
  78. '''
  79. @app.route('/createUser/')
  80. def createUser():
  81.     #Getting user name and password from http request.
  82.     userName = request.args.get('userName')
  83.     password = request.args.get('passWord')
  84.    
  85.     #Checking if user exists
  86.     cursor.execute(
  87.     """
  88.    select count(1)
  89.    from [dbo].[Users]
  90.    where UserName = ?
  91.    """, userName)
  92.    
  93.     row = cursor.fetchall()[0][0]
  94.    
  95.     if row == 0:
  96.         #creating user
  97.         cursor.execute("INSERT INTO [dbo].[Users](UserName, UserPassWord) values (?, ?)", userName, password)
  98.         cursor.commit()
  99.        
  100.         #fetching user ID
  101.         cursor.execute(
  102.             """
  103.            select userID
  104.            from [dbo].[Users]
  105.            where UserName = ?
  106.            """, userName)
  107.    
  108.        
  109.         userID = cursor.fetchall()[0][0]
  110.         #setting dir
  111.         direc = "D:\\DTU\\FlotGrim\\images\\exploretagsselfiegirl\\"
  112.         os.chdir(direc)
  113.        
  114.         #fetching all image names
  115.         userList = glob.glob("*")
  116.        
  117.         inputList = []
  118.         temp = []
  119.         #looping over image names and creating lists
  120.         for i in range(len(userList)):
  121.             temp.append([userID, userName, userList[i]])
  122.            
  123.             #making new lists at 500 to limit size of call
  124.             if i % 500 == 0:
  125.                 inputList.append(temp)
  126.                 temp = []
  127.        
  128.         #inserting to database
  129.         for inputs in inputList:
  130.             cursor.executemany("""
  131.                      INSERT INTO [dbo].[Scores]
  132.                      (UserID, UserName, ImageID)
  133.                      VALUES (?,?,?)
  134.                      """, inputs)
  135.             cursor.commit()
  136.            
  137.         return 'Success creating user'
  138.     else:
  139.         return 'User already exists'
  140.  
  141. @app.route('/userLogin/')
  142. def userLogin():
  143.     #Getting user name and password from http request.
  144.     userName = request.args.get('userName')
  145.     password = request.args.get('passWord')
  146.    
  147.     #Checking if user exists
  148.     cursor.execute(
  149.     """
  150.    select passWord
  151.    from [dbo].[Users]
  152.    where UserName = ?
  153.    """, userName)
  154.    
  155.     dbPassword = cursor.fetchall()[0][0]
  156.    
  157.     if len(dbPassword) == 0:
  158.         return "No user found"
  159.     if dbPassword == password:
  160.         return "Success. Loged in"
  161.     else:
  162.         return "Error. Wrong password"
  163.  
  164. if __name__ == "__main__":
  165.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement