Advertisement
Guest User

Untitled

a guest
Jan 31st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 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('userId')
  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.     print userName
  63.     cursor.execute(
  64.         """
  65.        SELECT *
  66.        FROM [dbo].[Scores]
  67.        WHERE Score IS NULL AND UserName = ?
  68.        """, userName)
  69.    
  70.     ID = cursor.fetchall()
  71.    
  72.     i = random.randrange(len(ID)) # get random index
  73.     image = ID[i][2]
  74.        
  75.     return image
  76.    
  77. '''
  78. Creates a user
  79. '''
  80. @app.route('/createUser/')
  81. def createUser():
  82.     #Getting user name and password from http request.
  83.     userName = request.args.get('userName')
  84.     password = request.args.get('passWord')
  85.    
  86.     #Checking if user exists
  87.     cursor.execute(
  88.     """
  89.    select count(1)
  90.    from [dbo].[Users]
  91.    where UserName = ?
  92.    """, userName)
  93.    
  94.     row = cursor.fetchall()[0][0]
  95.    
  96.     if row == 0:
  97.         #creating user
  98.         cursor.execute("INSERT INTO [dbo].[Users](UserName, UserPassWord) values (?, ?)", userName, password)
  99.         cursor.commit()
  100.        
  101.         #fetching user ID
  102.         cursor.execute(
  103.             """
  104.            select userID
  105.            from [dbo].[Users]
  106.            where UserName = ?
  107.            """, userName)
  108.    
  109.        
  110.         userID = cursor.fetchall()[0][0]
  111.         #setting dir
  112.         direc = "D:\\DTU\\FlotGrim\\images\\exploretagsselfiegirl\\"
  113.         os.chdir(direc)
  114.        
  115.         #fetching all image names
  116.         userList = glob.glob("*")
  117.        
  118.         inputList = []
  119.         temp = []
  120.         #looping over image names and creating lists
  121.         for i in range(len(userList)):
  122.             temp.append([userID, userName, userList[i]])
  123.            
  124.             #making new lists at 500 to limit size of call
  125.             if i % 500 == 0:
  126.                 inputList.append(temp)
  127.                 temp = []
  128.        
  129.         #inserting to database
  130.         for inputs in inputList:
  131.             cursor.executemany("""
  132.                      INSERT INTO [dbo].[Scores]
  133.                      (UserID, UserName, ImageID)
  134.                      VALUES (?,?,?)
  135.                      """, inputs)
  136.             cursor.commit()
  137.            
  138.         return 'Success creating user'
  139.     else:
  140.         return 'User already exists'
  141.    
  142. if __name__ == "__main__":
  143.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement