-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
124 lines (97 loc) · 3.54 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
#!/usr/bin/env python
# Functions that handle database management
import main
import words
from userDB import getUserInfo
import logging
import os
import webapp2
from google.appengine.api import users
from google.appengine.ext.webapp import template
# Help from http://nealbuerger.com/2013/12/google-app-engine-import-csv-to-datastore/
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext import db
# Clear out the entire phrase data store, or part of it (eventually)
class ResetDBEntries(webapp2.RequestHandler):
def get(self):
user_info = getUserInfo(self.request.url)
# TODO: Get from request
oldDbName = ''
newDbName = 'Approved Words'
q = words.OsagePhraseDB.all()
numEntries = 0
numReset = 0
# TODO: repeat until all are reset.
for p in q.run():
numEntries += 1
if p.dbName is None or p.dbName == oldDbName:
p.dbName = newDbName
p.put()
numReset += 1
# TODO: delete them, with message.
self.response.out.write('!!! Total DB entries = %d.' % numEntries)
self.response.out.write('!!! Reset dbname from >%s< to >%s< for %d entries total.' % (
oldDbName, newDbName, numReset))
class ManageDbName(webapp2.RequestHandler):
def get(self):
user_info = getUserInfo(self.request.url)
submitType = self.request.get('deleteDB', '')
dbName = self.request.get('dbName', '')
clear = self.request.get('clear', '')
confirmDelete = self.request.get('confirmDelete', None)
confirmAdd = self.request.get('confirmAdd', None)
q = words.OsageDbName.all()
if submitType == "deleteDB" and dbName:
if not confirmDelete:
self.response.out.write('Deleting db Name = %s is not confirmed.\n' % dbName)
return
# Wipe out DB
for p in q.run():
pName = p.dbName
if dbName == "*ALL*" or dbName == pName:
words.OsageDbName.delete(p)
self.response.out.write('Deleted db Name = %s.\n' % dbName)
return
# No name -> return list of all.
if not dbName:
nameList = []
for p in q.run():
nameList.append(p.dbName)
self.response.out.write('db Names = %s.\n' % nameList)
return
q.filter("dbName =", dbName)
result = q.get()
if not confirmAdd:
self.response.out.write('Adding db Name = %s is not confirmed.\n' % dbName)
return
if result:
self.response.out.write('db Name = %s is already defined.\n' % dbName)
else:
entry = words.OsageDbName(dbName=dbName);
entry.put()
self.response.out.write('db Name = %s has been added.\n' % dbName)
# Show simple interface for CSV upload.
class SolicitUpload(webapp2.RequestHandler):
def get(self):
# upload_url = blobstore.create_upload_url('upload')
upload_url = '/words/uploadCSV/'
user_info = getUserInfo(self.request.url)
#logging.info('$$$$$$$$$ upload_url %s' % upload_url)
q = words.OsageDbName.all()
dbNameList = [p.dbName for p in q.run()]
logging.info('dbNameList = %s' % dbNameList)
template_values = {
'language': main.Language,
'upload_url':upload_url,
'dbNames': dbNameList,
}
path = os.path.join(os.path.dirname(__file__), 'wordsUpload.html')
self.response.out.write(template.render(path, template_values))
# Dispatch requests.
app = webapp2.WSGIApplication([
('/db/manageDB/', SolicitUpload),
('/db/handleDB/', ManageDbName),
('/db/resetDbEntries/', ResetDBEntries),
], debug=True)