Handle vehicle name uniqueness
Vehicle names now must be only unique per owner, not globally. Also errors are displayed to the forms on creation and edit of vehicles.
This commit is contained in:
parent
c5305caf36
commit
19b1e3b7ae
@ -11,6 +11,9 @@ a folder that will serve as configuration directory and fill in the
|
||||
information. The directory will be used as volume during container
|
||||
operation.
|
||||
|
||||
## start database
|
||||
`docker run --name pitstops_db -e MYSQL_ROOT_PASSWORD=$SOMESECUREPASSWORD$ -e MYSQL_DATABASE=pitstops -d mysql:latest`
|
||||
|
||||
## run in development
|
||||
|
||||
Include the development version of the code as volume, so the app gets
|
||||
@ -22,3 +25,4 @@ debugging during development.
|
||||
|
||||
## run in production
|
||||
`docker run --name pitstops -d -v /data/pitstops/:/data -v /configs/pitstops/:/app/config -p 80:5000 rollerverbrauch`
|
||||
|
||||
|
@ -21,11 +21,7 @@ app.config.from_object(__name__)
|
||||
db = SQLAlchemy(app)
|
||||
mail = Mail(app)
|
||||
|
||||
from rollerverbrauch.tools import \
|
||||
VehicleStats, \
|
||||
db_log_add, \
|
||||
db_log_delete, \
|
||||
db_log_update
|
||||
import rollerverbrauch.tools as tools
|
||||
|
||||
from rollerverbrauch.forms import \
|
||||
CreatePitstopForm, \
|
||||
@ -58,8 +54,8 @@ def user_registered_sighandler(app, user, confirm_token):
|
||||
db.session.add(new_vehicle)
|
||||
user.vehicles.append(new_vehicle)
|
||||
db.session.commit()
|
||||
db_log_add(user)
|
||||
db_log_add(new_vehicle)
|
||||
tools.db_log_add(user)
|
||||
tools.db_log_add(new_vehicle)
|
||||
|
||||
|
||||
@app.before_first_request
|
||||
@ -88,9 +84,11 @@ def edit_vehicle(vid):
|
||||
form = EditVehicleForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
if not tools.check_vehicle_name_is_unique(current_user, form.name):
|
||||
return render_template('editVehicleForm.html', form=form)
|
||||
vehicle.name = form.name.data
|
||||
db.session.commit()
|
||||
db_log_update(vehicle)
|
||||
tools.db_log_update(vehicle)
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form.name.default = vehicle.name
|
||||
@ -112,7 +110,7 @@ def delete_vehicle(vid):
|
||||
if form.validate_on_submit():
|
||||
db.session.delete(vehicle)
|
||||
db.session.commit()
|
||||
db_log_delete(vehicle)
|
||||
tools.db_log_delete(vehicle)
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
return render_template('deleteVehicleForm.html', form=form, vehicle=vehicle)
|
||||
@ -124,11 +122,14 @@ def create_vehicle():
|
||||
form = EditVehicleForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
new_vehicle = Vehicle(form.name.data)
|
||||
vehicle_name = form.name.data
|
||||
if not tools.check_vehicle_name_is_unique(current_user, form.name):
|
||||
return render_template('createVehicleForm.html', form=form)
|
||||
new_vehicle = Vehicle(vehicle_name)
|
||||
db.session.add(new_vehicle)
|
||||
current_user.vehicles.append(new_vehicle)
|
||||
db.session.commit()
|
||||
db_log_add(new_vehicle)
|
||||
tools.db_log_add(new_vehicle)
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
return render_template('createVehicleForm.html', form=form)
|
||||
@ -170,7 +171,7 @@ def create_pit_stop_form(vid):
|
||||
db.session.add(new_stop)
|
||||
vehicle.pitstops.append(new_stop)
|
||||
db.session.commit()
|
||||
db_log_add(new_stop)
|
||||
tools.db_log_add(new_stop)
|
||||
return redirect(url_for('get_pit_stops', _anchor= 'v' + str(vehicle.id)))
|
||||
|
||||
form.odometer.default = last_pitstop.odometer
|
||||
@ -214,5 +215,5 @@ def get_account_page():
|
||||
def get_statistics():
|
||||
stats = []
|
||||
for vehicle in current_user.vehicles:
|
||||
stats.append(VehicleStats(vehicle))
|
||||
stats.append(tools.VehicleStats(vehicle))
|
||||
return render_template('statistics.html', data=stats)
|
||||
|
@ -40,10 +40,11 @@ class User(db.Model, UserMixin):
|
||||
class Vehicle(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||
name = db.Column(db.String(255), unique=True)
|
||||
name = db.Column(db.String(255))
|
||||
pitstops = db.relationship(
|
||||
'Pitstop'
|
||||
)
|
||||
__table_args__ = (db.UniqueConstraint('owner_id', 'name', name='_owner_name_uniq'),)
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
@ -47,3 +47,21 @@ def db_log_delete(entity):
|
||||
|
||||
def db_log_update(entity):
|
||||
logging.info('db_update: %s' % str(entity))
|
||||
|
||||
|
||||
def check_vehicle_name_is_unique(current_user, name_field):
|
||||
"""
|
||||
Checks if the vehicle name given in the name_field is unique for the vehicles of the current user. An error is added
|
||||
to the field it the name is not unique.
|
||||
|
||||
:param current_user: the user currently logged in
|
||||
:param name_field: the form field to enter the name to
|
||||
:return: True if the name is unique, False otherwise.
|
||||
"""
|
||||
vehicle_name = name_field.data
|
||||
for vehicle in current_user.vehicles:
|
||||
if vehicle.name == vehicle_name:
|
||||
name_field.default = vehicle_name
|
||||
name_field.errors.append('Vehicle "%s" already exists.' % vehicle_name)
|
||||
return False
|
||||
return True
|
Loading…
Reference in New Issue
Block a user