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

This commit is contained in:
Rebecca Law
2015-12-15 09:28:15 +00:00
parent 350ccda208
commit 43f2605ac4
5 changed files with 16 additions and 14 deletions

View File

@@ -10,11 +10,14 @@ def insert_new_service(service_name, user):
limit=1000,
active=False,
restricted=True)
add_service(service)
service.users.append(user)
db.session.commit()
return service.id
try:
add_service(service)
service.users.append(user)
db.session.commit()
return service.id
except Exception as e:
print(e)
raise e
def get_service_by_id(id):

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 app.main import main
from app.main.dao import services_dao, users_dao
from app.main.forms import AddServiceForm
@@ -17,7 +18,8 @@ def process_add_service():
form = AddServiceForm()
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')
else:
return jsonify(form.errors), 400

View File

@@ -91,8 +91,6 @@ class Service(db.Model):
id = db.Column(db.Integer, primary_key=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)
active = db.Column(db.Boolean, index=False, unique=False, nullable=False)
limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False)

View File

@@ -2,7 +2,7 @@
Revision ID: 60_add_service
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',
sa.Column('id', sa.Integer(), 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('active', sa.Boolean(), nullable=False),
sa.Column('limit', sa.BigInteger(), nullable=False),
@@ -27,7 +26,6 @@ def upgrade():
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_index(op.f('ix_services_token_id'), 'services', ['token_id'], unique=True)
op.create_table('user_to_service',
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('service_id', sa.Integer(), nullable=True),
@@ -40,6 +38,5 @@ def upgrade():
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_table('user_to_service')
op.drop_index(op.f('ix_services_token_id'), table_name='services')
op.drop_table('services')
### end Alembic commands ###

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
@@ -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'})
assert response.status_code == 302
assert response.location == 'http://localhost/dashboard'
saved_service = services_dao.find_service_by_service_name('testing the post')
assert saved_service is not None