stc

a simple time card webapp
git clone _git@git.brennen.work:stc.git
Log | Files | Refs | README

routes.py (6454B)


      1 from app import app
      2 from flask_pymongo import PyMongo
      3 from flask import render_template, redirect, url_for, flash, request
      4 from flask_login import login_required
      5 from app.branches import bp
      6 from bson.objectid import ObjectId
      7 import datetime, hashlib
      8 import os
      9 from app.branches.forms import NewBranch, UpdateBranch
     10 #from app.meetings.update import
     11 #from app.meetings.meeting import
     12 
     13 mongo = PyMongo(app)
     14 
     15 ### Define fetch_meeting ### 
     16 #TRY TO MOVE TO app.meetings.meeting.py
     17 class BranchNotFoundError(Exception):
     18     pass
     19 
     20 def fetch_branch(branch_id):
     21     branch = mongo.db.branch_collection.find_one({"_id":ObjectId(branch_id)})
     22 
     23     if branch == None:
     24         raise BranchNotFoundError(f'Branch Id {branch_id} returned None')
     25     else:
     26         return branch
     27 
     28 def get_available_branches():
     29     availableBranches = []
     30     for branch in mongo.db.branch_collection.find():
     31         availableBranches.append((branch['_id'],branch['branch_name']))
     32     return availableBranches
     33 
     34 ### BEGIN DEV ROUTES ###
     35 
     36 @bp.route('/branches/seed',methods=["GET","PUT"])
     37 @login_required
     38 def branchesSeed():
     39     seeds = [
     40             {
     41                 "branch_name": "Dillon",
     42                 "address":"730 North Montana St",
     43                 "city":"Dillon",
     44                 "state":"MT",
     45                 "zipcode": "88888"
     46             },
     47             {
     48                 "branch_name": "Salmon",
     49                 "address":"601 Lena St",
     50                 "city":"Salmon",
     51                 "state":"ID",
     52                 "zipcode": "99999"
     53             },
     54             {
     55                 "branch_name":"BLM Internship",
     56                 "address":"730 North Montana St",
     57                 "city":"Dillon",
     58                 "state":"MT",
     59                 "zipcode": "777777"
     60             }
     61             ]
     62     mongo.db.branch_collection.delete_many({})
     63     mongo.db.branch_collection.insert_many(seeds)
     64     dev_branches = mongo.db.branch_collection.find()
     65     return render_template('dev_branches.html',dev_branches=dev_branches)
     66 
     67 @bp.route('/branches/dev',methods=["GET"])
     68 @login_required
     69 def allBranches():
     70     dev_branches = mongo.db.branch_collection.find({})
     71     return render_template('dev_branches.html',dev_branches=dev_branches)
     72 
     73 ### END DEV ROUTES ###
     74 
     75 #### BEGIN ROUTES ####
     76 
     77 @bp.route('/branches',methods=["GET"])
     78 @bp.route('/branches/',methods=["GET"])
     79 @login_required
     80 def branches():
     81     branches = mongo.db.branch_collection.find({})
     82     return render_template('branches.html',branches=branches)
     83 
     84 @bp.route('/branch/<branch_id>',methods=["GET"])
     85 @bp.route('/branch/<branch_id>/',methods=["GET"])
     86 def branch(branch_id):
     87     try:
     88        branch = fetch_branch(branch_id)
     89     except BranchNotFoundError as e:
     90         return render_template('error.html',error=e)
     91     else:
     92         return render_template('branch.html',branch=branch)
     93 
     94 @bp.route('/branch/new',methods=["GET","POST"])
     95 @login_required
     96 def new_branch():
     97     form = NewBranch()
     98     if form.validate_on_submit():
     99         try:
    100             new_branch = {'branch_name':form.branch_name.data,'address':form.address.data,'city':form.city.data,'state':form.state.data,'zipcode':form.zipcode.data}
    101             if form.branch_name.data == "":
    102                 new_branch['branch_name']=form.city.data
    103         except Exception:
    104             return "Error assigning form data"
    105         else:
    106             mongo.db.branch_collection.insert_one(new_branch)
    107             flash("Created new branch!")
    108             return redirect(url_for('branches.branches'))
    109 
    110 #        if form.name.data != "":
    111 #            new_meeting['meeting_name']=form.name.data
    112     return render_template('new_branch.html',form=form)
    113 
    114 @bp.route('/branch/<branch_id>/update',methods=["GET","POST"])
    115 @login_required
    116 def update_branch(branch_id):
    117     try:
    118         branch = fetch_branch(branch_id)
    119     except BranchNotFoundError as e:
    120         return render_template('error.html',error=e)
    121     else:
    122         return render_template('update_branch.html',branch=branch)
    123 
    124 @bp.route('/branch/<branch_id>/<update>',methods=["GET","POST"])
    125 @login_required
    126 def change_branch(branch_id,update):
    127     form = UpdateBranch()
    128     try:
    129         branch = fetch_branch(branch_id)
    130     except BranchNotFoundError as e:
    131         return render_template('error.html',error=e)
    132     else:
    133         if form.validate_on_submit():
    134             if update == "branch_name":
    135                 mongo.db.branch_collection.update_one({'_id':branch['_id']},{'$set':{'branch_name':form.branch_name.data}})
    136                 flash("Updated branch name from {} to {}".format(branch['branch_name'],form.branch_name.data))
    137                 return redirect(url_for('branches.update_branch',branch_id=branch['_id']))
    138             if update == "address":
    139                 mongo.db.branch_collection.update_one({'_id':branch['_id']},{'$set':{'address':form.address.data}})
    140                 flash("Updated address from {} to {}".format(branch['address'],form.address.data))
    141                 return redirect(url_for('branches.update_branch',branch_id=branch['_id']))
    142             if update == "city":
    143                 mongo.db.branch_collection.update_one({'_id':branch['_id']},{'$set':{'city':form.city.data}})
    144                 flash("Updated city from {} to {}".format(branch['city'],form.city.data))
    145                 return redirect(url_for('branches.update_branch',branch_id=branch['_id']))
    146             if update == "state":
    147                 mongo.db.branch_collection.update_one({'_id':branch['_id']},{'$set':{'state':form.state.data}})
    148                 flash("Updated state to {}".format(form.state.data))
    149                 return redirect(url_for('branches.update_branch',branch_id=branch['_id']))
    150             if update == "zipcode":
    151                 mongo.db.branch_collection.update_one({'_id':branch['_id']},{'$set':{'zipcode':form.zipcode.data}})
    152                 flash("Updated zipcode to {}".format(form.zipcode.data))
    153                 return redirect(url_for('branches.update_branch',branch_id=branch['_id']))
    154         return render_template('form_branches.html',branch=branch,update=update,form=form)
    155 
    156 @bp.route('/branch/<branch_id>/remove',methods=["GET","POST"])
    157 @login_required
    158 def remove_branch(branch_id):
    159     try:
    160         branch = fetch_branch(branch_id)
    161     except BranchNotFoundError as e:
    162         return render_template('error.html',error=e)
    163     else:
    164         mongo.db.branch_collection.delete_one(branch)
    165         flash("Deleted branch {}".format(branch['branch_name']))
    166         return redirect(url_for('branches.branches'))