Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- from flask import render_template, jsonify, redirect, request
- from FinalProject import app
- import os, json
- filename = "techDB"
- @app.route('/')
- @app.route('/home')
- def home():
- """Renders the home page."""
- return render_template(
- 'index.html',
- title='Home Page',
- year=datetime.now().year,
- )
- @app.route('/contact')
- def contact():
- """Renders the contact page."""
- return render_template(
- 'contact.html',
- title='Contact',
- year=datetime.now().year,
- message='Your contact page.'
- )
- # The page for adding data to the .json file.
- @app.route('/addtech')
- def tech():
- return render_template(
- 'addtech.html',
- title='Add technique'
- )
- # When the form is filled out and the submit button is pressed this route is called and the data is added to
- # the temp dictionary which is then added to the databse using the addToJson function.
- @app.route('/addtech', methods = ['POST'])
- def addtech():
- techName = request.form['techname']
- tempDictionary = {}
- tempDictionary['techs'] = []
- tempDictionary['techs'].append({
- 'techname': request.form['techname'],
- 'startingpos': request.form['startpos'],
- 'startinggrips': request.form['startgrip'],
- 'endpos': request.form['endpos'],
- 'endgrip': request.form['endgrip'],
- 'videolink': request.form['videolink']
- })
- addToJson(tempDictionary)
- return redirect ('/done/'+techName)
- @app.route('/about')
- def about():
- """Renders the about page."""
- return render_template(
- 'about.html',
- title='About',
- year=datetime.now().year,
- message='Your application description page.'
- )
- # Displays a thank you page
- @app.route('/done/<name>')
- def done(name):
- return render_template(
- 'done.html',
- name=name,
- title='Thanks for adding ' +name +' to our collection!'
- )
- # Retrieves the entire databse in .json format
- @app.route('/collection/api')
- def collection():
- print(json.dumps(GetUserInfo(), indent=4, sort_keys=True))
- return jsonify(GetUsesrInfo())
- # Retrieves all objects matching the search query in .json format
- @app.route('/collection/api/<techname>')
- def collection(techname):
- print(json.dump(GetJsonTech(techname), indent=4, sort_keys=True))
- return jsonify(GetJsonTech(techname)
- # This function takes in a dictionary and writes those values to the specified database, in this case techDB.json
- def addToJson (_dictionary):
- checkData()
- with open(filename+".json", 'r+') as infile:
- data = json.load(infile)
- infile.close()
- pass
- data['techs'].append(_dictionary["techs"][0])
- with open(filename+".json", "w") as outfile:
- json.dump(data, outfile)
- outfile.close()
- pass
- print("Technique has been added.")
- pass
- # This function verifies the .json. If the file exists we can use it, if not we create it.
- def checkData():
- if not os.path.exists(filename+".json"):
- file = open(filename+".json", 'w+')
- file.write("{\"techs\": []}")
- file.close()
- pass
- pass
- # This function gets all the info in the techDB and returns it.
- def GetUserInfo():
- checkData()
- with open(filename+".json", "r") as infile:
- data = json.load(infile)
- infile.close()
- pass
- return data
- # This function gets the specified user and returns it.
- def GetJsonTech(_techname):
- checkData()
- with open(filename+".json", "r") as infile:
- data = json.load(infile)
- infile.close()
- pass
- for tech in data['techs']:
- if tech.get('techname') == _techname:
- return {"Technique": tech }
- pass
- return None
- pass
Advertisement
Add Comment
Please, Sign In to add comment