Skip to content

Commit

Permalink
frontend: support access control flags
Browse files Browse the repository at this point in the history
  • Loading branch information
div72 committed Jul 29, 2022
1 parent 223b11c commit 370c6a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion formie/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ def all_forms():
@auth.login_required
def new_form():
if request.method == "POST":
acf: ACF = ACF(0)
if request.args.get("hide_results", "false") == "true":
acf |= ACF.HIDE_RESULTS
if request.args.get("disallow_anon_answer", "false") == "true":
acf |= ACF.DISALLOW_ANON_ANSWER

schema = request.json
error = validate_schema(schema)

Expand All @@ -236,7 +242,7 @@ def new_form():
try:
schema_str = json.dumps(schema) # TODO: fetch original instead
fields = decode_fields(schema)
form = Form(schema=schema_str, created_at=datetime.datetime.now(), creator_id=g.user.id)
form = Form(schema=schema_str, created_at=datetime.datetime.now(), creator_id=g.user.id, access_control_flags=acf.value)
db.session.add(form)
db.session.commit()
create_model(str(form.id), fields).__table__.create(db.engine)
Expand Down
6 changes: 5 additions & 1 deletion formie/static/new_form.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
let fields_div = document.getElementById('fields');

let hide_results = document.getElementById('hide_results');
let disallow_anon_answer = document.getElementById('disallow_anon_answer');

var index = 0;

function new_field() {
Expand Down Expand Up @@ -71,7 +74,8 @@ function make_schema() {

function submit_form() {
let req = new XMLHttpRequest();
req.open('POST', document.location.href, false);
const params = new URLSearchParams({"hide_results": hide_results.checked, "disallow_anon_answer": disallow_anon_answer.checked});
req.open('POST', document.location.href + `?${params}`, false);
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify(make_schema()));
if (req.status === 400) {
Expand Down
4 changes: 3 additions & 1 deletion formie/templates/forms/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
{% block content %}
<button onclick="new_field()">New field</button>
<div id="fields"></div>
<button onclick="submit_form()">Create Form</button>
<br><input id="hide_results" type="checkbox">Make results private.</input>
<br><input id="disallow_anon_answer" type="checkbox">Block non-logged in users from answering.</input>
<br><button onclick="submit_form()">Create Form</button>
{% endblock %}

0 comments on commit 370c6a3

Please sign in to comment.