110067722: Post add-service endpoint saves the service and maps it to the user.

This commit is contained in:
Rebecca Law
2015-12-15 10:32:26 +00:00
parent 350ccda208
commit 43f2605ac4
5 changed files with 16 additions and 14 deletions
+8 -5
View File
@@ -10,11 +10,14 @@ def insert_new_service(service_name, user):
limit=1000, limit=1000,
active=False, active=False,
restricted=True) restricted=True)
try:
add_service(service) add_service(service)
service.users.append(user) service.users.append(user)
db.session.commit() db.session.commit()
return service.id return service.id
except Exception as e:
print(e)
raise e
def get_service_by_id(id): def get_service_by_id(id):
+4 -2
View File
@@ -1,7 +1,8 @@
from flask import render_template, jsonify, redirect from flask import render_template, jsonify, redirect, session
from flask_login import login_required from flask_login import login_required
from app.main import main from app.main import main
from app.main.dao import services_dao, users_dao
from app.main.forms import AddServiceForm from app.main.forms import AddServiceForm
@@ -17,7 +18,8 @@ def process_add_service():
form = AddServiceForm() form = AddServiceForm()
if form.validate_on_submit(): if form.validate_on_submit():
user = users_dao.get_user_by_id(session['user_id'])
services_dao.insert_new_service(form.service_name.data, user)
return redirect('/dashboard') return redirect('/dashboard')
else: else:
return jsonify(form.errors), 400 return jsonify(form.errors), 400
-2
View File
@@ -91,8 +91,6 @@ class Service(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False, unique=True) name = db.Column(db.String(255), nullable=False, unique=True)
token_id = db.Column(db.BigInteger, index=True, unique=True)
created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False) created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
active = db.Column(db.Boolean, index=False, unique=False, nullable=False) active = db.Column(db.Boolean, index=False, unique=False, nullable=False)
limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False) limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False)
+1 -4
View File
@@ -2,7 +2,7 @@
Revision ID: 60_add_service Revision ID: 60_add_service
Revises: 50_alter_verify_code_type Revises: 50_alter_verify_code_type
Create Date: 2015-12-14 16:55:56.612005 Create Date: 2015-12-15 09:25:09.000431
""" """
@@ -19,7 +19,6 @@ def upgrade():
op.create_table('services', op.create_table('services',
sa.Column('id', sa.Integer(), nullable=False), sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False), sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('token_id', sa.BigInteger(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False), sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('active', sa.Boolean(), nullable=False), sa.Column('active', sa.Boolean(), nullable=False),
sa.Column('limit', sa.BigInteger(), nullable=False), sa.Column('limit', sa.BigInteger(), nullable=False),
@@ -27,7 +26,6 @@ def upgrade():
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name') sa.UniqueConstraint('name')
) )
op.create_index(op.f('ix_services_token_id'), 'services', ['token_id'], unique=True)
op.create_table('user_to_service', op.create_table('user_to_service',
sa.Column('user_id', sa.Integer(), nullable=True), sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('service_id', sa.Integer(), nullable=True), sa.Column('service_id', sa.Integer(), nullable=True),
@@ -40,6 +38,5 @@ def upgrade():
def downgrade(): def downgrade():
### commands auto generated by Alembic - please adjust! ### ### commands auto generated by Alembic - please adjust! ###
op.drop_table('user_to_service') op.drop_table('user_to_service')
op.drop_index(op.f('ix_services_token_id'), table_name='services')
op.drop_table('services') op.drop_table('services')
### end Alembic commands ### ### end Alembic commands ###
+3 -1
View File
@@ -1,4 +1,4 @@
from app.main.dao import verify_codes_dao from app.main.dao import verify_codes_dao, services_dao
from tests.app.main import create_test_user from tests.app.main import create_test_user
@@ -24,3 +24,5 @@ def test_should_add_service_and_redirect_to_next_page(notifications_admin, notif
response = client.post('/add-service', data={'service_name': 'testing the post'}) response = client.post('/add-service', data={'service_name': 'testing the post'})
assert response.status_code == 302 assert response.status_code == 302
assert response.location == 'http://localhost/dashboard' assert response.location == 'http://localhost/dashboard'
saved_service = services_dao.find_service_by_service_name('testing the post')
assert saved_service is not None