def delete(connection, binderID): """ Delete a binder """ userID = current_user.get_id() qry = text( """DELETE FROM Binders WHERE userID = :userID AND binderID = :binderID""" ) try: connection.execute(qry, binderID=binderID, userID=userID) except IntegrityError: log("Failed to delete binder via API") return "FAILED", status.HTTP_403_FORBIDDEN log("Deleted binder via API") return "OK", status.HTTP_204_NO_CONTENT
def register(): """ Register a new user """ form = RegistrationForm() if request.method == 'POST': if form.validate_on_submit(): #qry = text("""(SELECT 1 from UsersUnverified WHERE username=:username) # UNION # (SELECT 1 FROM UsersUnverified WHERE email=:email)""") #unverified_users = engine.execute(qry, # username=form.username.data, # email=form.email.data).fetchall() #qry = text("""(SELECT 1 from Users WHERE username=:username) # UNION # (SELECT 1 FROM Users WHERE email=:email)""") #users = engine.execute(qry, # username=form.username.data, # email=form.email.data).fetchall() qry = text( """(SELECT 1 from UsersUnverified WHERE username=:username)""") unverified_users = engine.execute( qry, username=form.username.data).fetchall() qry = text("""(SELECT 1 from Users WHERE username=:username)""") users = engine.execute(qry, username=form.username.data).fetchall() if unverified_users or users: flash("User already exists") else: #qry = text("""INSERT INTO UsersUnverified # (username, email, password, registrationCode) # VALUES (:username, :email, :password, :registrationCode)""") #engine.execute(qry, # username=form.username.data, # email=form.email.data, # password=generate_password_hash(form.password.data), # registrationCode=generate_registration_code()) qry = text("""INSERT INTO Users (username, email, password, createdOn) VALUES (:username, :email, :password, NOW())""") engine.execute(qry, username=form.username.data, email=form.email.data, password=generate_password_hash( form.password.data)) log('New user registered') return render_template("users/post_registration.html", username=form.username.data) else: log('User registration form has invalid data') flash("Looks like you did something wrong.") return render_template('users/register.html', form=form)
def add_test(connection, filmTypeID): """ Add a new film test """ userID = current_user.get_id() json = request.get_json() nextFilmTestID = next_id(connection, 'filmTestID', 'FilmTests') qry = text("""INSERT INTO FilmTests (userID, filmTestID, filmTypeID, enlargerID, enlargerLensID, filterID, stepTabletID, headHeight, filmSize, lux, fstop, exposureTime, developer, devTime, devTemperature, prebath, stop, agitation, rotaryRPM, notes) VALUES (:userID, :filmTestID, :filmTypeID, :enlargerID, :enlargerLensID, :filterID, :stepTabletID, :headHeight, :filmSize, :lux, :fstop, :exposureTime, :developer, :devTime, :devTemperature, :prebath, :stop, :agitation, :rotaryRPM, :notes)""") try: connection.execute(qry, userID=userID, filmTestID=nextFilmTestID, filmTypeID=filmTypeID, enlargerID=json['data']['enlargerID'], enlargerLensID=json['data']['enlargerLensID'], filterID=json['data']['filterID'], stepTabletID=json['data']['stepTabletID'], headHeight=zero_to_none(json['data']['headHeight']), filmSize=json['data']['filmSize'], lux=json['data']['lux'], fstop=json['data']['fstop'], exposureTime=key_or_none(json, 'exposureTime'), developer=json['data']['developer'], devTime=time_to_seconds(json['data']['devTime']), devTemperature=json['data']['devTemperature'], prebath=json['data']['prebath'], stop=json['data']['stop'], agitation=json['data']['agitation'], rotaryRPM=zero_to_none(json['data']['rotaryRPM']), notes=json['data']['notes']) except IntegrityError: log("Failed to create new film test via API") return "FAILED", status.HTTP_409_CONFLICT json['data']['id'] = str(nextFilmTestID) json['data']['created_on'] = datetime.datetime.now() resp = make_response(jsonify(json)) log("Created new film test via API") return resp, status.HTTP_201_CREATED
def patch(connection, binderID): """ Update a binder """ userID = current_user.get_id() json = request.get_json() qry = text("""UPDATE Binders SET name = :name, notes = :notes WHERE userID = :userID AND binderID = :binderID""") try: connection.execute(qry, name=json['data']['name'], notes=json['data']['notes'], userID=userID, binderID=binderID) except IntegrityError: log("Failed to update binder via API") return "FAILED", status.HTTP_409_CONFLICT resp = make_response(jsonify(json)) resp.headers['Location'] = "/binders/" + str(binderID) return resp, status.HTTP_204_NO_CONTENT
def add_step_tablet(connection): """ Add a new step tablet """ userID = current_user.get_id() json = request.get_json() nextStepTabletID = next_id(connection, 'stepTabletID', 'StepTablets') qry = text("""INSERT INTO StepTablets (userID, stepTabletID, name) VALUES (:userID, :stepTabletID, :name)""") try: connection.execute(qry, userID=userID, stepTabletID=nextStepTabletID, name=json['data']['name']) except IntegrityError as e: log("Failed to create new film test via API") print(e) return "FAILED", status.HTTP_409_CONFLICT json['data']['id'] = str(nextStepTabletID) json['data']['created_on'] = datetime.datetime.now() resp = make_response(jsonify(json)) log("Created new step tablet via API") return resp, status.HTTP_201_CREATED
def post(connection): """ Insert a new binder """ userID = current_user.get_id() json = request.get_json() nextBinderID = next_id(connection, 'binderID', 'Binders') qry = text("""INSERT INTO Binders (binderID, userID, name, notes) VALUES (:binderID, :userID, :name, :notes)""" ) try: connection.execute(qry, binderID=nextBinderID, userID=userID, name=json['data']['name'], notes=json['data']['notes']) except IntegrityError: log("Failed to create new binder via API") return "FAILED", status.HTTP_409_CONFLICT json['data']['id'] = str(nextBinderID) json['data']['project_count'] = str(0) json['data']['created_on'] = datetime.datetime.now() resp = make_response(jsonify(json)) log("Created new binder via API") return resp, status.HTTP_201_CREATED
def login(): """ Log a user in """ connection = engine.connect() transaction = connection.begin() form = LoginForm() if request.method == 'POST': if form.validate_on_submit(): username = form.username.data password = form.password.data qry = text("""SELECT userID, password FROM Users WHERE username = :username""") user = connection.execute(qry, username=username).fetchone() if user: if check_password_hash(user.password.decode(), password): login_user(User(user.userID), remember=True) qry = text("""UPDATE Users SET lastLogin = NOW() WHERE userID = :userID""") connection.execute(qry, userID=user.userID) transaction.commit() log("User logged in") return redirect("/") transaction.rollback() return render_template('users/login.html', form=form)
def user_prints(binderID, projectID, filmID): """ Manage user's prints for a film """ connection = engine.connect() transaction = connection.begin() userID = current_user.get_id() form = PrintForm() form.populate_select_fields(connection, filmID) if request.method == 'POST': if request.form['button'] == 'addPrint': if form.validate_on_submit(): nextPrintID = functions.next_id(connection, 'printID', 'Prints') log("Next Print ID: %s" % nextPrintID) # If user included a file, let's upload it. Otherwise skip it. if form.file.data: nextFileID = functions.next_id(connection, 'fileID', 'Files') files.upload_file(request, connection, transaction, nextFileID) else: nextFileID = None qry = text("""INSERT INTO Prints (printID, paperDeveloperID, filmID, exposureNumber, userID, paperID, paperFilterID, enlargerLensID, enlargerID, fileID, aperture, ndFilter, headHeight, exposureTime, printType, size, notes) VALUES (:printID, :paperDeveloperID, :filmID, :exposureNumber, :userID, :paperID, :paperFilterID, :enlargerLensID, :enlargerID, :fileID, :aperture, :ndFilter, :headHeight, :exposureTime, :printType, :size, :notes)""") insert(connection, qry, "Print", printID=nextPrintID, filmID=filmID, userID=userID, fileID=nextFileID, paperDeveloperID=zero_to_none( form.paperDeveloperID.data), exposureNumber=form.exposureNumber.data, paperID=zero_to_none(form.paperID.data), paperFilterID=zero_to_none(form.paperFilterID.data), enlargerLensID=zero_to_none(form.enlargerLensID.data), enlargerID=zero_to_none(form.enlargerID.data), aperture=form.aperture.data, ndFilter=form.ndFilter.data, headHeight=form.headHeight.data, exposureTime=functions.time_to_seconds( form.exposureTime.data), printType=form.printType.data, size=form.size.data, notes=form.notes.data) film = functions.get_film_details(connection, binderID, projectID, filmID) qry = text("""SELECT printID, exposureNumber, PaperDevelopers.name AS paperDeveloperName, PaperDevelopers.dilution AS paperDeveloperDilution, Papers.name AS paperName, PaperFilters.name AS paperFilterName, printType, size, aperture, ndFilter, headHeight, Prints.notes, fileID, EnlargerLenses.name AS lens, Enlargers.name AS enlarger, SECONDS_TO_DURATION(exposureTime) AS exposureTime FROM Prints LEFT OUTER JOIN Papers ON Papers.paperID = Prints.paperID AND Papers.userID = Prints.userID LEFT OUTER JOIN PaperFilters ON PaperFilters.paperFilterID = Prints.paperFilterID LEFT OUTER JOIN PaperDevelopers ON PaperDevelopers.paperDeveloperID = Prints.paperDeveloperID LEFT OUTER JOIN EnlargerLenses ON EnlargerLenses.enlargerLensID = Prints.enlargerLensID AND EnlargerLenses.userID = Prints.userID LEFT OUTER JOIN Enlargers ON Enlargers.enlargerID = Prints.enlargerID AND Enlargers.userID = Prints.userID WHERE filmID = :filmID AND Prints.userID = :userID""") prints = connection.execute(qry, userID=userID, filmID=filmID) transaction.commit() return render_template('darkroom/prints.html', form=form, binderID=binderID, projectID=projectID, film=film, prints=prints, view='prints')