Commit e2fd649c authored by buttle's avatar buttle

Added mongoengine. Got some of it working

parent a03e42fc
......@@ -18,14 +18,19 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from flask import Flask
from flask_pymongo import PyMongo
#from flask_pymongo import PyMongo
from flask_mongoengine import MongoEngine
from flask_babel import Babel
from flask_wtf.csrf import CSRFProtect
import sys, os
#from mongoengine import connect
#db = connect("GNGforms")
app = Flask(__name__)
app.config.from_pyfile('config.cfg')
mongo = PyMongo(app)
#mongo = PyMongo(app)
db = MongoEngine(app)
babel = Babel(app)
csrf = CSRFProtect()
......
......@@ -5,7 +5,7 @@ DEFAULT_LANGUAGE = 'en'
TMP_DIR = '/tmp'
## MongoDB config
MONGO_URI = "mongodb://localhost:27017/GNGforms"
MONGODB_SETTINGS = {'host': 'mongodb://localhost:27017/GNGforms'}
# 86400 seconds = 24h
TOKEN_EXPIRATION = 86400
......
......@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from GNGforms import mongo
from GNGforms import db
from GNGforms import persistence
import pprint
......@@ -26,19 +26,19 @@ def migrateMongoSchema(schemaVersion):
if schemaVersion < 2:
# changes in schema version 2
# replace "expireDate" for "expiryConditions"
for form in mongo.db.forms.find():
for form in db.forms.find():
if 'expireDate' in form:
expireDate=form['expireDate']
form["expiryConditions"] = {"expireDate": expireDate}
mongo.db.forms.save(form)
mongo.db.forms.update_one({"_id": form["_id"]}, {"$unset": {'expireDate' :1}})
db.forms.save(form)
db.forms.update_one({"_id": form["_id"]}, {"$unset": {'expireDate' :1}})
else:
break
# get rid of from ObjectIDs
for form in mongo.db.forms.find():
for form in db.forms.find():
if not isinstance(form['author'], str):
form['author']=str(form['author'])
mongo.db.forms.save(form)
db.forms.save(form)
else:
break
schemaVersion=2
......@@ -46,13 +46,13 @@ def migrateMongoSchema(schemaVersion):
if schemaVersion < 3:
# changes in schema version 2
# change the editor's list to a dict and include notification preferences
for form in mongo.db.forms.find():
for form in db.forms.find():
if isinstance(form['editors'], list):
newEditorsDict={}
for editor in form['editors']:
newEditorsDict[editor]={'notification': form['notification']}
form['editors']=newEditorsDict
mongo.db.forms.update_one({"_id": form["_id"]}, {"$unset": {'notification' :1}, "$set": {"editors": form['editors']}})
db.forms.update_one({"_id": form["_id"]}, {"$unset": {'notification' :1}, "$set": {"editors": form['editors']}})
else:
break
schemaVersion=3
......@@ -60,43 +60,43 @@ def migrateMongoSchema(schemaVersion):
if schemaVersion < 4:
# this originally didn't work
# get rid of from ObjectIDs
for form in mongo.db.forms.find():
for form in db.forms.find():
form['author']=str(form['author'])
mongo.db.forms.save(form)
db.forms.save(form)
# this originally didn't work
for form in mongo.db.forms.find():
for form in db.forms.find():
newEditorsDict={}
for editor in form['editors']:
newEditorsDict[editor]={'notification': form['notification']}
if not newEditorsDict:
newEditorsDict[form['author']]={'notification': {'newEntry': False}}
form['editors']=newEditorsDict
mongo.db.forms.update_one({"_id": form["_id"]}, {"$unset": {'notification' :1}, "$set": {"editors": form['editors']}})
db.forms.update_one({"_id": form["_id"]}, {"$unset": {'notification' :1}, "$set": {"editors": form['editors']}})
schemaVersion=4
if schemaVersion < 5:
for site in mongo.db.sites.find():
for site in db.sites.find():
site["siteName"]="GNGforms"
mongo.db.sites.save(site)
db.sites.save(site)
schemaVersion=5
if schemaVersion < 6:
# Add field conditions
for form in mongo.db.forms.find():
for form in db.forms.find():
form["expiryConditions"]["fields"]={}
conditions=form["expiryConditions"]
mongo.db.forms.update_one({"_id": form["_id"]}, {"$set": {"expiryConditions": conditions}})
db.forms.update_one({"_id": form["_id"]}, {"$set": {"expiryConditions": conditions}})
schemaVersion=6
if schemaVersion < 7:
# Add log array
for form in mongo.db.forms.find():
mongo.db.forms.update_one({"_id": form["_id"]}, {"$set": {"log": []}})
for form in db.forms.find():
db.forms.update_one({"_id": form["_id"]}, {"$set": {"log": []}})
schemaVersion=7
if schemaVersion < 8:
# Add expired bool
forms=[persistence.Form(_id=form['_id']) for form in mongo.db.forms.find()]
forms=[persistence.Form(_id=form['_id']) for form in db.forms.find()]
for form in forms:
for editor in form.data['editors']:
form.data['editors'][editor]["notification"]["expiredForm"]=True
......@@ -107,31 +107,31 @@ def migrateMongoSchema(schemaVersion):
if schemaVersion < 9:
# Add site footnote. To be displayed at foot of forms.
for site in mongo.db.sites.find():
mongo.db.sites.update_one({"_id": site["_id"]}, {"$set": {"defaultFormFootNote": {
for site in db.sites.find():
db.sites.update_one({"_id": site["_id"]}, {"$set": {"defaultFormFootNote": {
'markdown': "",
'html': "",
'enabled': False }} })
for form in mongo.db.forms.find():
mongo.db.forms.update_one({"_id": form["_id"]}, {"$set": {"showFootNote": False }})
for form in db.forms.find():
db.forms.update_one({"_id": form["_id"]}, {"$set": {"showFootNote": False }})
schemaVersion=9
if schemaVersion < 10:
# Changed site footnote to personalDataConsent.
for site in mongo.db.sites.find():
for site in db.sites.find():
consent=site["defaultFormFootNote"]
mongo.db.sites.update_one({"_id": site["_id"]}, {"$unset": {'defaultFormFootNote' :1}, "$set": {"personalDataConsent": consent} })
db.sites.update_one({"_id": site["_id"]}, {"$unset": {'defaultFormFootNote' :1}, "$set": {"personalDataConsent": consent} })
for form in mongo.db.forms.find():
for form in db.forms.find():
boolean=form["showFootNote"]
mongo.db.forms.update_one({"_id": form["_id"]}, {"$unset": {'showFootNote' :1}, "$set": {"requireDataConsent": boolean }})
db.forms.update_one({"_id": form["_id"]}, {"$unset": {'showFootNote' :1}, "$set": {"requireDataConsent": boolean }})
schemaVersion=10
if schemaVersion < 11:
# Added smtp config per Site.
for site in mongo.db.sites.find():
for site in db.sites.find():
smtpConfig={
"host": "smtp.%s" % site["hostname"],
"port": 25,
......@@ -140,20 +140,20 @@ def migrateMongoSchema(schemaVersion):
"password": "",
"noreplyAddress": site["noreplyEmailAddress"]
}
mongo.db.sites.update_one({"_id": site["_id"]}, {"$unset": {'noreplyEmailAddress' :1},"$set": {"smtpConfig": smtpConfig} })
db.sites.update_one({"_id": site["_id"]}, {"$unset": {'noreplyEmailAddress' :1},"$set": {"smtpConfig": smtpConfig} })
schemaVersion=11
if schemaVersion < 12:
# Add restrictedAccess
for form in mongo.db.forms.find():
mongo.db.forms.update_one({"_id": form["_id"]}, {"$set": {"restrictedAccess": False}})
for form in db.forms.find():
db.forms.update_one({"_id": form["_id"]}, {"$set": {"restrictedAccess": False}})
schemaVersion=12
if schemaVersion == 12:
# Add admin prefs. to forms
for form in mongo.db.forms.find():
mongo.db.forms.update_one({"_id": form["_id"]}, {"$set": {"adminPreferences": { "public": True }}})
for form in db.forms.find():
db.forms.update_one({"_id": form["_id"]}, {"$set": {"adminPreferences": { "public": True }}})
schemaVersion=13
# this can't be a good migration setup :(
......
This diff is collapsed.
......@@ -4,7 +4,7 @@
<div class="container">
<div class="row col-md-3"></div>
<div class="row col-md-7" style="font-size:1.5em">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Return to form{%endtrans%}" onClick="location.href='/forms/view/{{ form._id }}'">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Return to form{%endtrans%}" onClick="location.href='/forms/view/{{ form.id }}'">
{{ form.slug }}
<hr />
</div>
......@@ -56,7 +56,7 @@
<td>{{ editor.username }}</td>
<td>{{ editor.email }}</td>
<td class="text-right">
{% if editor._id|string == form.author %}
{% if editor.id|string == form.author %}
({%trans%}Author{%endtrans%})
{% else %}
<input class="btn btn-xs btn-danger" type="button" value="{%trans%}Remove{%endtrans%}" onClick="js:removeEditor('{{editor._id}}');">
......
......@@ -82,7 +82,7 @@ jQuery(function($) {
disabledAttrs: disabledAttrs,
controlOrder: {{ config['FORMBUILDER_CONTROL_ORDER'] | tojson }},
i18n: {
locale: "{{ config['LANGUAGES'][g.current_user.data['language']][1] }}",
locale: "{{ config['LANGUAGES'][g.current_user.language][1] }}",
location: "{{host_url}}static/formBuilder-languages/"
},
formData: {{ session['formStructure'] | tojson }}
......
This diff is collapsed.
......@@ -35,19 +35,19 @@
</tr>
<tr>
<td>{%trans%}Created{%endtrans%}</td>
<td>{{ user.data.created }}</td>
<td>{{ user.created }}</td>
</tr>
<tr>
<td>{%trans%}Language{%endtrans%}</td>
<td>{{ config['LANGUAGES'][user.data.language][0] }}</td>
<td>{{ config['LANGUAGES'][user.language][0] }}</td>
</tr>
<tr>
<td>{%trans%}Email{%endtrans%}</td>
<td>{{ user.data.email }}</td>
<td>{{ user.email }}</td>
</tr>
<tr>
<td>{%trans%}Validated email{%endtrans%}</td>
<td> {% if user.data.validatedEmail %}
<td> {% if user.validatedEmail %}
<span>{%trans%}True{%endtrans%}</span>
{% else %}
<span style="color:red">{%trans%}False{%endtrans%}</span>
......@@ -64,11 +64,11 @@
</td>
</tr>
</table>
{% if g.isAdmin and not user._id == g.current_user._id %}
{% if g.isAdmin and not user.id == g.current_user.id %}
{% if user.forms|length %}
<input class="btn-danger btn btn-sm" type="button" value="{%trans%}Delete user and authored forms{%endtrans%}" onClick="location.href='/admin/users/delete/{{user._id}}'">
<input class="btn-danger btn btn-sm" type="button" value="{%trans%}Delete user and authored forms{%endtrans%}" onClick="location.href='/admin/users/delete/{{user.id}}'">
{% else %}
<input class="btn-danger btn btn-sm" type="button" value="{%trans%}Delete user{%endtrans%}" onClick="location.href='/admin/users/delete/{{user._id}}'">
<input class="btn-danger btn btn-sm" type="button" value="{%trans%}Delete user{%endtrans%}" onClick="location.href='/admin/users/delete/{{user.id}}'">
{% endif %}
{% endif %}
......@@ -94,10 +94,10 @@
{% for form in user.forms %}
<tr>
<td><a href="/forms/view/{{form._id}}">{{form.slug}}</a></td>
<td><a href="/forms/view/{{form.id}}">{{form.slug}}</a></td>
<td>{{form.created}}</td>
<td>
{% if user._id|string == form.author %}
{% if user.id|string == form.author %}
{%trans%}True{%endtrans%}
{% else %}
{%trans%}False{%endtrans%}
......@@ -128,7 +128,7 @@ $(document).ready(function() {
});
document.getElementById('toggle_blocked').addEventListener('click', function(evt){
$.ajax({
url : "/admin/users/toggle-blocked/{{ user._id }}",
url : "/admin/users/toggle-blocked/{{ user.id }}",
type: "POST",
dataType: "json",
beforeSend: function(xhr, settings) {
......@@ -151,7 +151,7 @@ document.getElementById('toggle_blocked').addEventListener('click', function(evt
});
document.getElementById('toggle_admin').addEventListener('click', function(evt){
$.ajax({
url : "/admin/users/toggle-admin/{{ user._id }}",
url : "/admin/users/toggle-admin/{{ user.id }}",
type: "POST",
dataType: "json",
beforeSend: function(xhr, settings) {
......
......@@ -20,24 +20,24 @@
<div class="container">
<div class="row col-md-8" style="font-size:1.5em">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Form details{%endtrans%}" onClick="location.href='/forms/view/{{ form._id }}'">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Form details{%endtrans%}" onClick="location.href='/forms/view/{{ form.id }}'">
{{ form.slug }}
</div>
<div class="row col-md-4" style="text-align:right">
<input id="editModeButton" class="btn-primary btn btn-sm" type="button" value="{%trans%}Enable edition{%endtrans%}" onClick="js:setEditMode();">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Download CSV{%endtrans%}" onClick="location.href='/forms/csv/{{ form._id }}'">
<input class="btn-danger btn btn-sm" type="button" value="{%trans%}Delete all{%endtrans%}" onClick="location.href='/forms/delete-entries/{{ form._id }}'" {% if not form.totalEntries %}disabled{%endif%} >
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Download CSV{%endtrans%}" onClick="location.href='/forms/csv/{{ form.id }}'">
<input class="btn-danger btn btn-sm" type="button" value="{%trans%}Delete all{%endtrans%}" onClick="location.href='/forms/delete-entries/{{ form.id }}'" {% if not form.totalEntries %}disabled{%endif%} >
</div>
</div>
<div class="container">
<hr style="margin-bottom:5px" />
{% if form.editors[g.current_user._id|string] is defined %}
{% if form.editors[g.current_user.id|string] is defined %}
<div id="toggle_notification" class="btn-group btn-toggle">
<button id="notification_true" class="btn btn-xs btn-default {% if form.data.editors[g.current_user._id|string]['notification']['newEntry'] %}btn-success{% endif %}">{%trans%}True{%endtrans%}</button>
<button id="notification_false" class="btn btn-xs btn-default {% if not form.data.editors[g.current_user._id|string]['notification']['newEntry'] %}btn-primary{% endif %}">{%trans%}False{%endtrans%}</button>
<button id="notification_true" class="btn btn-xs btn-default {% if form.editors[g.current_user.id|string]['notification']['newEntry'] %}btn-success{% endif %}">{%trans%}True{%endtrans%}</button>
<button id="notification_false" class="btn btn-xs btn-default {% if not form.editors[g.current_user.id|string]['notification']['newEntry'] %}btn-primary{% endif %}">{%trans%}False{%endtrans%}</button>
</div>
{%trans%}Notify me on each new entry{%endtrans%}
{% endif %}
......@@ -68,7 +68,7 @@
</table>
</div>
{% if form.editors[g.current_user._id|string] is defined %}
{% if form.editors[g.current_user.id|string] is defined %}
<script>
var csrftoken = "{{ csrf_token() }}";
var fieldIndex={{fieldIndex|tojson}};
......@@ -147,7 +147,7 @@ $('#entriesTable tbody').on('click', '.saveCellEdition', function () {
var $cell = $(this).parents("td");
$cell.html(newValue);
$.ajax({
url : "/forms/change-entry-field-value/{{ form._id }}",
url : "/forms/change-entry-field-value/{{ form.id }}",
type: "POST",
dataType: "json",
contentType: 'application/json;charset=UTF-8',
......@@ -179,7 +179,7 @@ $('#entriesTable tbody').on('click', '.delete-row-icon', function () {
return
}
$.ajax({
url : "/forms/delete-entry/{{ form._id }}",
url : "/forms/delete-entry/{{ form.id }}",
type: "POST",
dataType: "json",
contentType: 'application/json;charset=UTF-8',
......@@ -202,7 +202,7 @@ $('#entriesTable tbody').on('click', '.delete-row-icon', function () {
$('#entriesTable tbody').on('click', '.undo-delete-row', function () {
var $row = $(this).parents('tr');
$.ajax({
url : "/forms/undo-delete-entry/{{ form._id }}",
url : "/forms/undo-delete-entry/{{ form.id }}",
type: "POST",
dataType: "json",
contentType: 'application/json;charset=UTF-8',
......@@ -223,7 +223,7 @@ $('#entriesTable tbody').on('click', '.undo-delete-row', function () {
});
document.getElementById('toggle_notification').addEventListener('click', function(evt){
$.ajax({
url : "/form/toggle-notification/{{ form._id }}",
url : "/form/toggle-notification/{{ form.id }}",
type: "POST",
dataType: "json",
beforeSend: function(xhr, settings) {
......
......@@ -28,9 +28,9 @@
</thead>
{% for form in forms %}
<tr>
<td><a href="/forms/view/{{form._id}}">{{form.slug}}</a></td>
<td><a href="/forms/view/{{form.id}}">{{form.slug}}</a></td>
<td>{{form.created}}</td>
<td><a href="/admin/users/id/{{form.user._id}}">{{form.user.username}}</a></td>
<td><a href="/admin/users/id/{{form.user.id}}">{{form.user.username}}</a></td>
<td>{{form.isPublic()}}</td>
<td>{{form.entries|length}}</td>
{% if g.isRootUser %}
......
......@@ -9,7 +9,7 @@
<div class="row col-md-2"></div>
<div class="row col-md-8">
<div style="font-size:1.5em">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Return to form{%endtrans%}" onClick="location.href='/forms/view/{{ form._id }}'">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Return to form{%endtrans%}" onClick="location.href='/forms/view/{{ form.id }}'">
{{ form.slug }}
<hr />
{%trans%}Log{%endtrans%}
......@@ -23,7 +23,7 @@
<th>{%trans%}Message{%endtrans%}</th>
</tr>
</thead>
{% for entry in form.data.log %}
{% for entry in form.log %}
<tr>
<td>{{entry[0]}}</td>
<td>{{entry[1]}}</td>
......
......@@ -30,11 +30,11 @@
</thead>
{% for user in users %}
<tr>
<td><a href="/admin/users/{{user._id}}">{{user.username}}</a></td>
<td><a href="/admin/users/{{user.id}}">{{user.username}}</a></td>
{% if g.isRootUser %}
<td>{{user.hostname}}</td>
{% endif %}
<td>{{user.data.created}}</td>
<td>{{user.created}}</td>
<td>{{user.enabled}}</td>
<td>{{user.email}}</td>
<td>{{user.forms|length}}</td>
......
......@@ -33,10 +33,10 @@
</thead>
{% for form in forms %}
<tr>
<td><a href="/forms/view/{{form._id}}">{{form.slug}}</a></td>
<td><a href="/forms/view/{{form.id}}">{{form.slug}}</a></td>
<td>{{form.created}}</td>
<td>{{form.lastEntryDate}}</td>
<td><a style="padding-right:1em" href="/forms/entries/{{form._id}}">{{form.entries|length}}</a></td>
<td><a style="padding-right:1em" href="/forms/entries/{{form.id}}">{{form.entries|length}}</a></td>
<td>{{form.isPublic()}}</td>
<td>{{form.isShared()}}</td>
</tr>
......
......@@ -4,7 +4,7 @@
<div class="container">
<div class="row col-md-3"></div>
<div class="row col-md-7" style="font-size:1.5em">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Return to form{%endtrans%}" onClick="location.href='/forms/view/{{ form._id }}'">
<input class="btn-primary btn btn-sm" type="button" value="{%trans%}Return to form{%endtrans%}" onClick="location.href='/forms/view/{{ form.id }}'">
{{ form.slug }}
<hr />
</div>
......@@ -40,8 +40,8 @@
{%trans%}Only visible to people with a user account (login required){%endtrans%}
<div style="padding-top:0.7em">
<span id="toggle_restrictedAccess" class="btn-group btn-toggle">
<button id="restrictedAccess_true" class="btn btn-xs btn-default {% if form.data.restrictedAccess %}btn-success{% endif %}">{%trans%}Restricted{%endtrans%}</button>
<button id="restrictedAccess_false" class="btn btn-xs btn-default {% if not form.data.restrictedAccess %}btn-primary{% endif %}">{%trans%}Public form{%endtrans%}</button>
<button id="restrictedAccess_true" class="btn btn-xs btn-default {% if form.restrictedAccess %}btn-success{% endif %}">{%trans%}Restricted{%endtrans%}</button>
<button id="restrictedAccess_false" class="btn btn-xs btn-default {% if not form.restrictedAccess %}btn-primary{% endif %}">{%trans%}Public form{%endtrans%}</button>
</span>
</div>
</div>
......@@ -67,7 +67,7 @@
{{ editor.email }}
</td>
<td class="text-right">
{% if editor._id|string == form.author %}
{% if editor.id|string == form.author %}
({%trans%}Author{%endtrans%})
{% else %}
<input class="btn btn-xs btn-danger" type="button" value="{%trans%}Remove{%endtrans%}" onClick="js:removeEditor('{{editor._id}}');">
......@@ -97,7 +97,7 @@
var csrftoken = "{{ csrf_token() }}";
function removeEditor(editor_id){
$.ajax({
url : "/forms/remove-editor/{{form._id}}/"+editor_id,
url : "/forms/remove-editor/{{form.id}}/"+editor_id,
type: "POST",
dataType: "json",
beforeSend: function(xhr, settings) {
......@@ -115,7 +115,7 @@ function removeEditor(editor_id){
}
document.getElementById('toggle_sharedEntries').addEventListener('click', function(evt){
$.ajax({
url : "/form/toggle-shared-entries/{{ form._id }}",
url : "/form/toggle-shared-entries/{{ form.id }}",
type: "POST",
dataType: "json",
beforeSend: function(xhr, settings) {
......@@ -143,7 +143,7 @@ document.getElementById('toggle_sharedEntries').addEventListener('click', functi
document.getElementById('toggle_restrictedAccess').addEventListener('click', function(evt){
$.ajax({
url : "/form/toggle-restricted-access/{{ form._id }}",
url : "/form/toggle-restricted-access/{{ form.id }}",
type: "POST",
dataType: "json",
beforeSend: function(xhr, settings) {
......
......@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from GNGforms import app, mongo
from GNGforms import app, db
from flask import g, flash, redirect, render_template, url_for
from flask_babel import gettext
from unidecode import unidecode
......@@ -247,21 +247,16 @@ def isValidEmail(email):
return False
return True
def writeCSV(form):
fieldnames=[]
fieldheaders={}
for field in form.fieldIndex:
fieldnames.append(field['name'])
fieldheaders[field['name']]=field['label']
csv_name='%s/%s.csv' % (app.config['TMP_DIR'], form.slug)
print(csv_name)
with open(csv_name, mode='w') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames, extrasaction='ignore')
writer.writerow(fieldheaders)
for entry in form.data['entries']:
for entry in form.entries:
writer.writerow(entry)
return csv_name
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment