generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #722 from hackforla/636-forms-api-backend
636 forms api backend
- Loading branch information
Showing
20 changed files
with
1,011 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"""initial_form_api | ||
Revision ID: cfc4e41b69d3 | ||
Revises: e4c8bb426528 | ||
Create Date: 2024-05-05 17:14:51.771328 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'cfc4e41b69d3' | ||
down_revision = 'e4c8bb426528' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table('field_properties', | ||
sa.Column('properties_id', sa.Integer(), nullable=False), | ||
sa.Column('description', sa.Text(), nullable=True), | ||
sa.Column('field_type', sa.String(length=50), nullable=False), | ||
sa.Column('choices', sa.JSON(), nullable=True), | ||
sa.CheckConstraint("field_type IN ('date', 'dropdown', 'multiple_choice', 'email', 'file_upload', 'group', 'long_text', 'number', 'short_text', 'yes_no')", name='chk_field_type'), | ||
sa.PrimaryKeyConstraint('properties_id') | ||
) | ||
op.create_table('field_validations', | ||
sa.Column('validations_id', sa.Integer(), nullable=False), | ||
sa.Column('required', sa.Boolean(), nullable=False), | ||
sa.Column('max_length', sa.Integer(), nullable=True), | ||
sa.PrimaryKeyConstraint('validations_id') | ||
) | ||
op.create_table('forms', | ||
sa.Column('form_id', sa.Integer(), nullable=False), | ||
sa.Column('title', sa.String(length=255), nullable=False), | ||
sa.Column('description', sa.Text(), nullable=True), | ||
sa.Column('created_at', sa.DateTime(), nullable=True), | ||
sa.PrimaryKeyConstraint('form_id') | ||
) | ||
op.create_table('field_groups', | ||
sa.Column('group_id', sa.Integer(), nullable=False), | ||
sa.Column('form_id', sa.Integer(), nullable=False), | ||
sa.Column('title', sa.String(length=255), nullable=False), | ||
sa.Column('description', sa.Text(), nullable=True), | ||
sa.ForeignKeyConstraint(['form_id'], ['forms.form_id'], ), | ||
sa.PrimaryKeyConstraint('group_id') | ||
) | ||
op.create_table('fields', | ||
sa.Column('field_id', sa.Integer(), nullable=False), | ||
sa.Column('ref', sa.String(length=255), nullable=False), | ||
sa.Column('properties_id', sa.Integer(), nullable=False), | ||
sa.Column('validations_id', sa.Integer(), nullable=False), | ||
sa.Column('group_id', sa.Integer(), nullable=True), | ||
sa.ForeignKeyConstraint(['group_id'], ['field_groups.group_id'], ), | ||
sa.ForeignKeyConstraint(['properties_id'], ['field_properties.properties_id'], ), | ||
sa.ForeignKeyConstraint(['validations_id'], ['field_validations.validations_id'], ), | ||
sa.PrimaryKeyConstraint('field_id') | ||
) | ||
op.create_table('responses', | ||
sa.Column('answer_id', sa.Integer(), nullable=False), | ||
sa.Column('user_id', sa.Integer(), nullable=False), | ||
sa.Column('field_id', sa.String(length=255), nullable=False), | ||
sa.Column('answer_text', sa.Text(), nullable=True), | ||
sa.ForeignKeyConstraint(['field_id'], ['fields.field_id'], ), | ||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), | ||
sa.PrimaryKeyConstraint('answer_id') | ||
) | ||
with op.batch_alter_table('role', schema=None) as batch_op: | ||
batch_op.create_unique_constraint('role', ['name']) | ||
with op.batch_alter_table('user', schema=None) as batch_op: | ||
batch_op.alter_column('lastName', | ||
existing_type=sa.VARCHAR(length=255), | ||
nullable=True, | ||
existing_server_default=sa.text("'Unknown'")) | ||
|
||
def downgrade() -> None: | ||
with op.batch_alter_table('role', schema=None) as batch_op: | ||
batch_op.drop_constraint('role', type_='unique') | ||
op.drop_table('responses') | ||
op.drop_table('fields') | ||
op.drop_table('field_groups') | ||
op.drop_table('forms') | ||
op.drop_table('field_validations') | ||
op.drop_table('field_properties') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from openapi_server.repositories.forms import FormsRepository | ||
from openapi_server.models.database import DataAccessLayer | ||
|
||
def create_form(body): | ||
forms_repo = FormsRepository(DataAccessLayer.session()) | ||
|
||
form_id = forms_repo.add_form(body) | ||
form = forms_repo.get_form_json(form_id) | ||
if form: | ||
return form, 200 | ||
return {}, 404 | ||
|
||
def get_form(form_id): | ||
forms_repo = FormsRepository(DataAccessLayer.session()) | ||
form = forms_repo.get_form_json(form_id) | ||
if form: | ||
return form, 200 | ||
return f"Form with id {form_id} does not exist.", 404 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from openapi_server.repositories.forms import FormsRepository | ||
from openapi_server.repositories.user_repo import UserRepository | ||
from openapi_server.models.database import DataAccessLayer | ||
|
||
def update_responses(body, form_id, token_info): | ||
with DataAccessLayer.session() as session: | ||
user_repo = UserRepository(session) | ||
forms_repo = FormsRepository(session) | ||
user = user_repo.get_user(token_info['Username']) | ||
|
||
form = forms_repo.get_form(form_id) | ||
if not form: | ||
return f"Form with id {form_id} does not exist.", 404 | ||
|
||
valid_field_ids = form.get_field_ids() | ||
for response in body: | ||
response["user_id"] = user.id | ||
if response["field_id"] not in valid_field_ids: | ||
return f"Form {form_id} does not contain field id {response['field_id']}", 400 | ||
|
||
forms_repo.add_user_responses(user.id, body) | ||
|
||
return {}, 204 | ||
|
||
def get_responses(form_id, token_info): | ||
with DataAccessLayer.session() as session: | ||
user_repo = UserRepository(session) | ||
forms_repo = FormsRepository(session) | ||
|
||
form = forms_repo.get_form_json(form_id) | ||
if not form: | ||
return f"Form with id {form_id} does not exist.", 404 | ||
|
||
user = user_repo.get_user(token_info['Username']) | ||
responses = forms_repo.get_user_responses(user.id, form_id) | ||
if responses: | ||
return responses, 200 | ||
return [], 202 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.