Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize tables on first load in forms #365

Merged
merged 5 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# ignore todo files
TODO
*.todo

# Ignore auto-generated documentation
docs/gramex*.rst
Expand Down
19 changes: 13 additions & 6 deletions gramex/apps/forms/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,28 @@
<body>
{% include 'template-navbar-view-form.html' %}
<div class="container my-3">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<ul class="nav nav-tabs" id="formTab" role="tablist">
<li class="nav-item" role="presentation">
<a class="nav-link active" id="questions-tab" data-toggle="tab" href="#questions" role="tab" aria-controls="questions" aria-selected="true">Questions</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="responses-tab" data-toggle="tab" href="#responses" role="tab" aria-controls="responses" aria-selected="false">Responses</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-content" id="formTabContent">
<div class="tab-pane fade show active" id="questions" role="tabpanel" aria-labelledby="questions-tab">
<div class="my-5" id="view-form">
<form></form>
<small class="text-muted">You are currently previewing the form.</small>
</div>
</div>
<div class="tab-pane fade w-100" id="responses" role="tabpanel" aria-labelledby="responses-tab">
<div class="formhandler pt-3 mt-3" data-src="../analytics/?db={{ form_id }}" data-export="false" data-count="true" data-page-size="5"></div>
<div class="formhandler pt-3 mt-3" data-export="false" data-count="true" data-page-size="5"></div>
</div>
</div>
<p>
<a class="btn btn-success" href="{{ base }}/create?id={{ form_id }}">Edit</a>
<a class="btn btn-primary" href="{{ base }}/view/{{ form_id }}">View form</a>
</p>
</div>

Expand All @@ -61,9 +63,14 @@
<script>
/* exported hljs, dragula, form_id */
const form_id = '{% raw form_id %}'
$('.formhandler').formhandler({
columns: [{name: "*" }, { name: "response", hide: true}]
})
fetch(`../analytics/?db=${form_id}`)
.then(response => response.json())
.then(function(response) {
$('.formhandler').formhandler({
data: response['each_form'],
columns: [{name: "*" }, { name: "response", hide: true}]
})
})
</script>
<script src="{{ base }}/js/view-form.js"></script>
</body>
Expand Down
18 changes: 1 addition & 17 deletions gramex/apps/forms/form_builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
import re
import sqlite3
import gramex.data
from io import BytesIO
from ast import literal_eval
Expand All @@ -22,7 +20,7 @@


def modify_columns(handler, data):
if handler.request.method == 'GET':
if handler.request.method == 'GET' and len(data):
# process json response
s = data['response'].apply(literal_eval)

Expand Down Expand Up @@ -95,17 +93,3 @@ def screenshots(kwargs, host):
except Exception:
app_log.exception('Screenshot failed')
raise


def db_check(handler):
"""Create forms.db if it doesn't exist."""
db_path = var['FORMS_URL']
db_path = re.sub(r'^sqlite:///', '', db_path)
if(not os.path.isfile(db_path)):
conn = sqlite3.connect(db_path)
conn.execute('CREATE TABLE "new_table" (`id` INTEGER PRIMARY KEY AUTOINCREMENT,\
`metadata` TEXT, `config` TEXT, `thumbnail` TEXT, `html` TEXT, `user` TEXT)')
conn.close()
return "created database"
else:
return "db exists"
49 changes: 32 additions & 17 deletions gramex/apps/forms/gramex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ url:
table: $FORMS_TABLE
id: $FORMS_ID
modify: form_builder.after_publish(handler, data)
columns:
metadata: TEXT
config: TEXT
thumbnail: TEXT
html: TEXT
user: TEXT
id:
type: INTEGER
primary_key: true
autoincrement: true
forms/thumbnail-$*:
pattern: /$YAMLURL/thumbnail/(.*)
handler: FileHandler
Expand Down Expand Up @@ -94,27 +104,32 @@ url:
function: form_builder.endpoint(handler)
headers:
Access-Control-Allow-Origin: '*' # Allow CORS from any domain
forms/dbcheck-$*:
pattern: /$YAMLURL/dbcheck
handler: FunctionHandler
kwargs:
function: form_builder.db_check
forms/analytics-$*:
pattern: /$YAMLURL/analytics/(.*)
handler: FormHandler
kwargs:
url: 'sqlite:///$GRAMEXDATA/forms/form_{db}.db' # Pick any database
table: analytics # Pick any table name to create
id: id # The "id" column is primary key
# Define your table's columns
columns:
response: TEXT # Use any SQL type allowed by DB
id:
type: INTEGER # Define an integer ID column
primary_key: true # as a primary key
autoincrement: true # that auto-increments
form_id: INTEGER
modify: form_builder.modify_columns(handler, data)
each_form:
url: 'sqlite:///$GRAMEXDATA/forms/form_{db}.db' # Pick any database
table: analytics # Pick any table name to create
id: id # The "id" column is primary key
columns:
response: TEXT # Use any SQL type allowed by DB
id:
type: INTEGER # Define an integer ID column
primary_key: true # as a primary key
autoincrement: true # that auto-increments
form_id: INTEGER
modify: form_builder.modify_columns(handler, data)
analytics:
url: 'sqlite:///$GRAMEXDATA/forms/forms.db' # Pick any database
table: analytics # Pick any table name to create
id: id # The "id" column is primary key
columns:
response: TEXT # Use any SQL type allowed by DB
id:
type: INTEGER # Define an integer ID column
primary_key: true # as a primary key
autoincrement: true # that auto-increments

forms/snippets/all:
pattern: /$YAMLURL/snippets/
Expand Down
4 changes: 4 additions & 0 deletions gramex/apps/forms/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@
position: sticky;
top: 5%;
}
// disable events while previewing the form at /form/id
#formTabContent form {
pointer-events: none;
}