<commit_before>from app import app, db, utils
from utils import *
from models import *
from forms import AddNewFoodResourceForm, NonAdminAddNewFoodResourceForm
from flask import render_template, flash, redirect, session, url_for, request, \
	g, jsonify, current_app
from flask.ext.login import login_user, logout_user, current_user, \
	login_required
from variables import resources_info_singular, resources_info_plural, \
	days_of_week
from datetime import time
from utils import generate_password
from flask_user import login_required, signals
from flask_user.views import _endpoint_url, _send_registered_email
from flask_login import current_user, login_user, logout_user

@app.route('/')
def index():
	return render_template('base.html')

@app.route('/new', methods=['GET', 'POST'])
@app.route('/edit/<id>', methods=['GET', 'POST'])
@login_required
def new(id=None):
    form = AddNewFoodResourceForm(request.form)
    timeslots = []
    food_resource_type = ""

    # Create a new food resource. 
    if id is None:
    	title = "Add New Food Resource"
    	food_resource_type = "FARMERS_MARKET"
    # Edit an existing food resource.
    else:
    	title = "Edit Food Resource"

    # GET request.
    if request.method == 'GET' and id is not None:
    	# Populate form with information about existing food resource. 
    	food_resource = FoodResource.query.filter_by(id=id).first()
    	if food_resource is None:
    		return render_template('404.html')

    	form.name.data = food_resource.name
    	form.address_line1.data = food_resource.address.line1
    	form.address_line2.data = food_resource.address.line2
    	form.address_city.data = food_resource.address.city
    	form.address_state.data = food_resource.address.state
    	form.address_zip_code.data = food_resource.address.zip_code
    	form.phone_number.data = food_resource.phone_numbers[0].number
    	form.website.data = food_resource.url
    	form.additional_information.data = food_resource.description
    	form.is_for_family_and_children.data = food_resource.is_for_family_and_children
    	form.is_for_seniors.data = food_resource.is_for_seniors
    	form.is_wheelchair_accessible.data = food_resource.is_wheelchair_accessible
    	form.is_accepts_snap.data = food_resource.is_accepts_snap

    	# Fields that must be dyanamically updated using JavaScript.
    	for timeslot in food_resource.timeslots:
    		timeslots.append(timeslot)
    	food_resource_type = food_resource.location_type

	# POST request.
    additional_errors = []
    if request.method == 'POST' and form.validate(): 
    	food_resource_type = request.form['food-resource-type']

        # Create the food resource's timeslots.
        are_timeslots_valid = True
        for i, day_of_week in enumerate(days_of_week): 
            if (request.form[str(day_of_week['index']) + '-open-or-closed'] == "open"):
                opening_time = request.form[str(day_of_week['index']) + '-opening-time']
                start_time = get_time_from_string(opening_time)
                closing_time = request.form[str(day_of_week['index']) + '-closing-time']
                end_time = get_time_from_string(closing_time)
                timeslot = TimeSlot(day_of_week=i, start_time=start_time, 
                    end_time=end_time)
                timeslots.append(timeslot)

                # Check that timeslot is valid.
                if start_time >= end_time: 
                    are_timeslots_valid = False
                    additional_errors.append("Opening time must be before \
                    	closing time.")
                else:
                    db.session.add(timeslot)

		# Create the food resource's remaining attributes. 
		if are_timeslots_valid:

			# If editing an existing food resource, delete its current record
			# from the database. 
			if id is not None:
				fr = FoodResource.query.filter_by(id=id).first()
				db.session.delete(fr)
				db.session.commit()

			# Create food resource's address.
			address = Address(line1=form.address_line1.data, 
				line2=form.address_line2.data, 
				city=form.address_city.data, 
				state=form.address_state.data, 
				zip_code=form.address_zip_code.data)
			db.session.add(address)

			# Create food resource's phone number.
			phone_numbers = []
			home_number = PhoneNumber(number=form.phone_number.data)
			db.session.add(home_number)
			phone_numbers.append(home_number)

			# Create food resource and store all data in it.
			food_resource = FoodResource(
				name=form.name.data, 
				phone_numbers=phone_numbers,
				description=form.additional_information.data,
				timeslots=timeslots,
				address=address, 
				is_for_family_and_children = form.is_for_family_and_children.data,
    			is_for_seniors = form.is_for_seniors.data,
    			is_wheelchair_accessible = form.is_wheelchair_accessible.data,	
    			is_accepts_snap = form.is_accepts_snap.data)

			# Assign a type to the food resource. 
			food_resource.location_type = request.form['food-resource-type']

			# Commit all database changes. 
			db.session.add(food_resource)
			db.session.commit()
			return redirect(url_for('admin'))

	# If GET request is received or POST request fails due to invalid timeslots, 
	# render the page. 
	return render_template('add_resource.html', form=form, 
		days_of_week=days_of_week, resources_info=resources_info_singular, 
		additional_errors=additional_errors, title=title, timeslots=timeslots, 
		food_resource_type=food_resource_type, 
		possible_opening_times=get_possible_opening_times(), 
		possible_closing_times=get_possible_closing_times())

#Allows non-admins to add food resources
@app.route('/propose-resource', methods=['GET', 'POST'])
def guest_new_food_resource():
	form = NonAdminAddNewFoodResourceForm(request.form)
	timeslots = []
	food_resource_type = ""

	additional_errors = []
	if request.method == 'POST' and form.validate(): 
		# Checks if this guest has add resources in the past. If not,
		# creates a new FoodResourceContact
		guest_name = form.your_name.data<commit_msg>Fixed spacing issue (pythonf file had spaces instead of tabs so wasn't working properly)<commit_after>from app import app, db, utils
from utils import *
from models import *
from forms import AddNewFoodResourceForm, NonAdminAddNewFoodResourceForm
from flask import render_template, flash, redirect, session, url_for, request, \
	g, jsonify, current_app
from flask.ext.login import login_user, logout_user, current_user, \
	login_required
from variables import resources_info_singular, resources_info_plural, \
	days_of_week
from datetime import time
from utils import generate_password
from flask_user import login_required, signals
from flask_user.views import _endpoint_url, _send_registered_email
from flask_login import current_user, login_user, logout_user

@app.route('/')
def index():
	return render_template('base.html')

@app.route('/new', methods=['GET', 'POST'])
@app.route('/edit/<id>', methods=['GET', 'POST'])
@login_required
def new(id=None):
	form = AddNewFoodResourceForm(request.form)
	timeslots = []
	food_resource_type = ""

	# Create a new food resource. 
	if id is None:
		title = "Add New Food Resource"
		food_resource_type = "FARMERS_MARKET"
	# Edit an existing food resource.
	else:
		title = "Edit Food Resource"

	# GET request.
	if request.method == 'GET' and id is not None:
		# Populate form with information about existing food resource. 
		food_resource = FoodResource.query.filter_by(id=id).first()
		if food_resource is None:
			return render_template('404.html')

		form.name.data = food_resource.name
		form.address_line1.data = food_resource.address.line1
		form.address_line2.data = food_resource.address.line2
		form.address_city.data = food_resource.address.city
		form.address_state.data = food_resource.address.state
		form.address_zip_code.data = food_resource.address.zip_code
		form.phone_number.data = food_resource.phone_numbers[0].number
		form.website.data = food_resource.url
		form.additional_information.data = food_resource.description
		form.is_for_family_and_children.data = food_resource.is_for_family_and_children
		form.is_for_seniors.data = food_resource.is_for_seniors
		form.is_wheelchair_accessible.data = food_resource.is_wheelchair_accessible
		form.is_accepts_snap.data = food_resource.is_accepts_snap

		# Fields that must be dyanamically updated using JavaScript.
		for timeslot in food_resource.timeslots:
			timeslots.append(timeslot)
		food_resource_type = food_resource.location_type

	# POST request.
	additional_errors = []
	if request.method == 'POST' and form.validate(): 
		food_resource_type = request.form['food-resource-type']

		# Create the food resource's timeslots.
		are_timeslots_valid = True
		for i, day_of_week in enumerate(days_of_week): 
			if (request.form[str(day_of_week['index']) + '-open-or-closed'] == "open"):
				opening_time = request.form[str(day_of_week['index']) + '-opening-time']
				start_time = get_time_from_string(opening_time)
				closing_time = request.form[str(day_of_week['index']) + '-closing-time']
				end_time = get_time_from_string(closing_time)
				timeslot = TimeSlot(day_of_week=i, start_time=start_time, 
					end_time=end_time)
				timeslots.append(timeslot)

				# Check that timeslot is valid.
				if start_time >= end_time: 
					are_timeslots_valid = False
					additional_errors.append("Opening time must be before \
						closing time.")
				else:
					db.session.add(timeslot)

		# Create the food resource's remaining attributes. 
		if are_timeslots_valid:

			# If editing an existing food resource, delete its current record
			# from the database. 
			if id is not None:
				fr = FoodResource.query.filter_by(id=id).first()
				db.session.delete(fr)
				db.session.commit()

			# Create food resource's address.
			address = Address(line1=form.address_line1.data, 
				line2=form.address_line2.data, 
				city=form.address_city.data, 
				state=form.address_state.data, 
				zip_code=form.address_zip_code.data)
			db.session.add(address)

			# Create food resource's phone number.
			phone_numbers = []
			home_number = PhoneNumber(number=form.phone_number.data)
			db.session.add(home_number)
			phone_numbers.append(home_number)

			# Create food resource and store all data in it.
			food_resource = FoodResource(
				name=form.name.data, 
				phone_numbers=phone_numbers,
				description=form.additional_information.data,
				timeslots=timeslots,
				address=address, 
				is_for_family_and_children = form.is_for_family_and_children.data,
				is_for_seniors = form.is_for_seniors.data,
				is_wheelchair_accessible = form.is_wheelchair_accessible.data,	
				is_accepts_snap = form.is_accepts_snap.data)

			# Assign a type to the food resource. 
			food_resource.location_type = request.form['food-resource-type']

			# Commit all database changes. 
			db.session.add(food_resource)
			db.session.commit()
			return redirect(url_for('admin'))

	# If GET request is received or POST request fails due to invalid timeslots, 
	# render the page. 
	return render_template('add_resource.html', form=form, 
		days_of_week=days_of_week, resources_info=resources_info_singular, 
		additional_errors=additional_errors, title=title, timeslots=timeslots, 
		food_resource_type=food_resource_type, 
		possible_opening_times=get_possible_opening_times(), 
		possible_closing_times=get_possible_closing_times())

#Allows non-admins to add food resources
@app.route('/propose-resource', methods=['GET', 'POST'])
def guest_new_food_resource():
	form = NonAdminAddNewFoodResourceForm(request.form)
	timeslots = []
	food_resource_type = ""

	additional_errors = []
	if request.method == 'POST' and form.validate(): 
		# Checks if this guest has add resources in the past. If not,
		# creates a new FoodResourceContact
		guest_name = form.your_name.data
{'fraction_non_alphanumeric': 0.09161016949152542, 'fraction_numerical': 0.002033898305084746, 'mean_word_length': 4.164551422319475, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '856317', 'n_tokens_mistral': 4099, 'n_tokens_neox': 3667, 'n_words': 802}