Guest User

Untitled

a guest
Jan 4th, 2017
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1.  
  2. from datetime import datetime
  3. from flask import render_template, jsonify, redirect, request
  4. from FinalProject import app
  5. import os, json
  6.  
  7. filename = "techDB"
  8.  
  9. @app.route('/')
  10. @app.route('/home')
  11. def home():
  12.     """Renders the home page."""
  13.     return render_template(
  14.         'index.html',
  15.         title='Home Page',
  16.         year=datetime.now().year,
  17.     )
  18.  
  19. @app.route('/contact')
  20. def contact():
  21.     """Renders the contact page."""
  22.     return render_template(
  23.         'contact.html',
  24.         title='Contact',
  25.         year=datetime.now().year,
  26.         message='Your contact page.'
  27.     )
  28.  
  29. # The page for adding data to the .json file.
  30. @app.route('/addtech')
  31. def tech():
  32.     return render_template(
  33.         'addtech.html',
  34.         title='Add technique'
  35.         )
  36.  
  37. # When the form is filled out and the submit button is pressed this route is called and the data is added to
  38. # the temp dictionary which is then added to the databse using the addToJson function.
  39. @app.route('/addtech', methods = ['POST'])
  40. def addtech():
  41.     techName = request.form['techname']
  42.  
  43.     tempDictionary = {}
  44.     tempDictionary['techs'] = []
  45.     tempDictionary['techs'].append({
  46.         'techname': request.form['techname'],
  47.         'startingpos': request.form['startpos'],
  48.         'startinggrips': request.form['startgrip'],
  49.         'endpos': request.form['endpos'],
  50.         'endgrip': request.form['endgrip'],
  51.         'videolink': request.form['videolink']
  52.         })
  53.  
  54.     addToJson(tempDictionary)
  55.  
  56.     return redirect ('/done/'+techName)
  57.  
  58.  
  59. @app.route('/about')
  60. def about():
  61.     """Renders the about page."""
  62.     return render_template(
  63.         'about.html',
  64.         title='About',
  65.         year=datetime.now().year,
  66.         message='Your application description page.'
  67.     )
  68.  
  69. # Displays a thank you page
  70. @app.route('/done/<name>')
  71. def done(name):
  72.     return render_template(
  73.         'done.html',
  74.         name=name,
  75.         title='Thanks for adding ' +name +' to our collection!'
  76.         )
  77.  
  78. # Retrieves the entire databse in .json format
  79. @app.route('/collection/api')
  80. def collection():
  81.  
  82.     print(json.dumps(GetUserInfo(), indent=4, sort_keys=True))
  83.     return jsonify(GetUsesrInfo())
  84.  
  85. # Retrieves all objects matching the search query in .json format
  86. @app.route('/collection/api/<techname>')
  87. def collection(techname):
  88.     print(json.dump(GetJsonTech(techname), indent=4, sort_keys=True))
  89.     return jsonify(GetJsonTech(techname)
  90.  
  91. # This function takes in a dictionary and writes those values to the specified database, in this case techDB.json
  92. def addToJson (_dictionary):
  93.     checkData()
  94.  
  95.     with open(filename+".json", 'r+') as infile:
  96.         data = json.load(infile)
  97.         infile.close()
  98.         pass
  99.  
  100.     data['techs'].append(_dictionary["techs"][0])
  101.  
  102.     with open(filename+".json", "w") as outfile:
  103.         json.dump(data, outfile)
  104.         outfile.close()
  105.         pass
  106.  
  107.     print("Technique has been added.")
  108.     pass
  109.  
  110. # This function verifies the .json. If the file exists we can use it, if not we create it.
  111. def checkData():
  112.     if not os.path.exists(filename+".json"):
  113.         file = open(filename+".json", 'w+')
  114.         file.write("{\"techs\": []}")
  115.         file.close()
  116.         pass
  117.     pass
  118.  
  119. # This function gets all the info in the techDB and returns it.
  120. def GetUserInfo():
  121.     checkData()
  122.  
  123.     with open(filename+".json", "r") as infile:
  124.         data = json.load(infile)
  125.         infile.close()
  126.         pass
  127.  
  128.     return data
  129.  
  130. # This function gets the specified user and returns it.
  131. def GetJsonTech(_techname):
  132.     checkData()
  133.  
  134.     with open(filename+".json", "r") as infile:
  135.        data = json.load(infile)
  136.        infile.close()
  137.        pass
  138.  
  139.     for tech in data['techs']:
  140.         if tech.get('techname') == _techname:
  141.             return {"Technique": tech  }
  142.         pass
  143.  
  144.     return None
  145.  
  146.     pass
Advertisement
Add Comment
Please, Sign In to add comment