Skip to content

Commit 271e631

Browse files
author
khz
committed
working sql, nosql from sqlite, to mongoDB
1 parent a164acb commit 271e631

File tree

10 files changed

+55
-30
lines changed

10 files changed

+55
-30
lines changed

pythononwheels/start/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"logformat" : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
4646
"id_pattern" : "[0-9\-a-zA-Z]+", # the regex used to math IDs in URLs (uuid in this case)
4747
"list_separator" : ",",
48-
"date_format" : "%Y-%m-%dT%H:%M:%S",
48+
"date_format" : "%Y-%m-%d %H:%M:%S",
4949
"internal_fields" : ["created_at", "last_updated", "id"], # these are hidden in the scaffolded views
5050
"default_rest_route": "list",
5151
"list_separator" : " "
@@ -73,6 +73,8 @@
7373
},
7474
"mongodb" : {
7575
"dbname" : "testdb",
76+
"atlas" : False,
77+
"atlas_conn_str" : "mongodb+srv://<USER>:<PASSWORD>@cluster0-aetuw.mongodb.net/test", #this is just a sample
7678
"host" : "localhost",
7779
"port" : 27017,
7880
"user" : None,

pythononwheels/start/models/mongodb/basemodel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def upsert(self):
211211
if self._id == None:
212212
#print("** insert **")
213213
# insert. so set created at
214-
self.created_at = datetime.datetime.utcnow()
214+
self.created_at = datetime.datetime.utcnow().strftime(myapp["date_format"])
215215
self.last_updated = self.created_at
216216
ior = self.table.insert_one(self.to_dict())
217217
self._id = ior.inserted_id
@@ -220,6 +220,7 @@ def upsert(self):
220220
# update
221221
#print("** update **")
222222
#print(self.to_dict())
223+
self.last_updated = datetime.datetime.utcnow().strftime(myapp["date_format"])
223224
ior = self.table.update_one({"_id" : self._id}, {"$set": self.to_dict()}, upsert=False )
224225
return ior
225226

pythononwheels/start/models/tinydb/basemodel.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def upsert(self):
156156
print("update by eid:" + str(self.eid))
157157
Q = Query()
158158
#self.last_updated = datetime.datetime.now()
159-
self.last_updated = datetime.datetime.utcnow()
159+
self.last_updated = datetime.datetime.utcnow().strftime(myapp["date_format"])
160160
self.table.update(self.to_dict(),Q.id==self.id)
161161
else:
162162
#first check if id is in db:
@@ -165,7 +165,7 @@ def upsert(self):
165165
if res:
166166
#update. object is already in db
167167
print("update by id:" + str(self.id))
168-
self.last_updated = datetime.datetime.utcnow()
168+
self.last_updated = datetime.datetime.utcnow().strftime(myapp["date_format"])
169169
#self.last_updated = datetime.datetime.now()
170170
self.eid = self.table.update(self.to_dict(),Q.id==self.id)
171171
else:
@@ -290,6 +290,8 @@ def find_by_id(self, id=None):
290290
""" return by id """
291291
Q = Query()
292292
res = self.table.search(Q.id == str(id))
293+
if len(res) == 1:
294+
return self.dict_result_to_object(res)
293295
return self._return_find(res)
294296

295297
def find_random(self):
@@ -300,6 +302,8 @@ def find_random(self):
300302
randnum = random.randrange(len(res))
301303
#print(" random: " + str(randnum))
302304
res=[res[randnum]]
305+
if len(res) == 1:
306+
return self.dict_result_to_object(res)
303307
return self._return_find(res)
304308

305309
def get_all(self):
@@ -320,6 +324,8 @@ def find_one(self, *criterion):
320324
print("criterion: " + str(criterion))
321325
try:
322326
res = self.table.get(*criterion)
327+
if len(res) == 1:
328+
return self.dict_result_to_object(res)
323329
return self._return_find(res)
324330
except Exception as e:
325331
raise e

pythononwheels/start/stubs/rest_handler_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class {{handler_class_name}}(PowHandler):
4141
model=Model()
4242

4343
# these fields will be hidden by scaffolded views:
44-
show_list=["id", "title", "text"]
44+
hide_list=["id", "created_at", "last_updated"]
4545

4646
def show(self, id=None):
4747
m=Model()

pythononwheels/start/stubs/scaffold_edit_view.bs4

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@
9090

9191
{% for key in model.schema.keys() %}
9292
{% if key in cfg.myapp["internal_fields"] %}
93-
{% set class_hidden = "hidden" %}
93+
<input type="hidden" name="{{key}}" id="{{key}}" value="{{data.get(key)}}" />
9494
{% else %}
95-
{% set class_hidden = "" %}
96-
{% end %}
9795
{% set _type = elem.schema[key]["type"] %}
9896
{% if _type == "string" %}
9997
{% if "allowed" in model.schema[key].keys() %}
@@ -125,7 +123,7 @@
125123
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
126124
<div class="col-10">
127125
<input type="text" class="form-control" name="{{key}}" id="{{key}}"
128-
value="" />
126+
value="{{elem.get(key)}}" />
129127
</div>
130128
</div>
131129
{% end %}
@@ -192,6 +190,7 @@
192190
</div>
193191
</div>
194192
{% end %}
193+
{% end %}
195194
{% end %}
196195

197196

pythononwheels/start/stubs/scaffold_list_view.bs4

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
<thead class="thead-inverse">
6767
<tr>
6868
{% for key in model.schema.keys() %}
69-
<th>{{key}}</th>
69+
{% if key not in hide_list %}
70+
<th>{{key}}</th>
71+
{% end %}
7072
{% end %}
7173
<th colspan="3">Actions:</th>
7274
</tr>
@@ -77,7 +79,9 @@
7779
{% for elem in data %}
7880
<tr id="{{elem.get("id")}}">
7981
{% for key in elem.schema.keys() %}
80-
<td>{{getattr(elem, key)}}</td>
82+
{% if key not in hide_list %}
83+
<td>{{getattr(elem, key)}}</td>
84+
{% end %}
8185
{% end %}
8286
<td>
8387
<span class="glyphicon glyphicon-star" aria-hidden="true"></span>

pythononwheels/start/stubs/scaffold_new_view.bs4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
</div>
159159
</div>
160160
{% end %}
161+
{% end %}
161162
{% end %}
162163
{% end %}
163164

pythononwheels/start/stubs/scaffold_page_view.bs4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<thead class="thead-inverse">
6767
<tr>
6868
{% for key in model.schema.keys() %}
69-
{% if key in show_list %}
69+
{% if key not in hide_list %}
7070
<th>{{key}}</th>
7171
{% end %}
7272
{% end %}
@@ -78,7 +78,7 @@
7878
{% for elem in data %}
7979
<tr id="{{elem.get("id")}}">
8080
{% for key in elem.schema.keys() %}
81-
{% if key in show_list %}
81+
{% if key not in hide_list %}
8282
<td>{{getattr(elem, key)}}</td>
8383
{% end %}
8484
{% end %}

pythononwheels/start/stubs/scaffold_show_view.bs4

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
<hr>
2525
<h2>Related Data Objects:</h2>
2626
<table class="table table-bordered">
27-
{% for rel in model.get_relations() %}
28-
{% for idx, val in enumerate(getattr(data, rel)) %}
29-
<tr>
30-
<td><b><a href="/{{rel}}/{{getattr(val, "id")}}">{{idx}}</a></b></td>
31-
<td>{{val}}</td>
32-
</tr>
27+
{% try %}
28+
{% for rel in model.get_relations() %}
29+
{% for idx, val in enumerate(getattr(data, rel)) %}
30+
<tr>
31+
<td><b><a href="/{{rel}}/{{getattr(val, "id")}}">{{idx}}</a></b></td>
32+
<td>{{val}}</td>
33+
</tr>
34+
{% end %}
3335
{% end %}
36+
{% except %}
37+
No relations for this model
3438
{% end %}
3539
</table>
3640
<hr>

pythononwheels/start/stubs/sql_model_template.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,27 @@ class {{model_class_name}}(Base):
2323
# raw sqlalchemy column __init__ parameters.
2424
#
2525
schema = {
26-
'title': {'type': 'string', 'maxlength' : 35},
27-
'text': {'type': 'string'},
28-
'likes': {
29-
'type': 'integer',
30-
"sql" : { # sql attributes are handed raw to sqlalchemy Column
31-
"primary_key" : False,
32-
"default" : "123",
33-
"unique" : False,
34-
"nullable" : False
35-
}
36-
}
26+
'title' : {'type': 'string', 'maxlength' : 35},
27+
'text' : {'type': 'string'},
28+
"votes" : { "type" : "integer", "default" : 0 }
3729
}
3830

31+
# you can also use special sqlalchemy attributes which are handed raw ro sqlalchemy
32+
# like this:
33+
# schema = {
34+
# 'title': {'type': 'string', 'maxlength' : 35},
35+
# 'text': {'type': 'string'},
36+
# 'likes': {
37+
# 'type': 'integer',
38+
# "sql" : { # sql attributes are handed raw to sqlalchemy Column
39+
# "primary_key" : False,
40+
# "default" : "123",
41+
# "unique" : False,
42+
# "nullable" : False
43+
# }
44+
# }
45+
# }
46+
3947

4048
#__table_args__ = { "autoload" : True }
4149

0 commit comments

Comments
 (0)