Skip to content

Commit ebac85f

Browse files
committed
add first version of uliweb_apijson.apijson and demo project
1 parent 04263ce commit ebac85f

26 files changed

+872
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
uliweb-apijson is a subset and slightly different variation of [apijson](https://github.com/TommyLemon/APIJSON/blob/master/Document.md)
2+
3+
You can try:
4+
5+
- [Demo uliweb project](demo/README.md)
6+
- [uliweb-apijson document](uliweb_apijson/apijson/README.md)

demo/.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.pyc
2+
*.bak
3+
local_settings.ini
4+
build
5+
dist
6+
*.egg-info
7+
.idea
8+
_git
9+
data
10+
database.db

demo/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## How to run uliweb-apijson demo?
2+
3+
pip install dependent packages, in demo root directory run:
4+
5+
```
6+
pip install six
7+
pip install -r requirements.txt
8+
```
9+
10+
In demo root directory,run commands to init db:
11+
12+
```
13+
uliweb syncdb
14+
uliweb dbinit
15+
```
16+
17+
In demo root directory, run debug server:
18+
19+
```
20+
uliweb runserver
21+
```
22+
23+
Then you can access http://localhost:8000 to try demo.
24+
25+
![](doc/imgs/demo_screenshot.png)

demo/apps/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__version__ = '0.1'
2+
__url__ = ''
3+
__author__ = ''
4+
__email__ = ''
File renamed without changes.

demo/apps/apijson_demo/__init__.py

Whitespace-only changes.

demo/apps/apijson_demo/dbinit.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#coding=utf-8
2+
from uliweb import models
3+
from uliweb.orm import set_dispatch_send
4+
5+
set_dispatch_send(False)
6+
7+
User = models.user
8+
Privacy = models.privacy
9+
Comment = models.comment
10+
Moment = models.moment
11+
12+
user_list = [
13+
{
14+
"username": "usera",
15+
"nickname": "User A",
16+
"email": "usera@localhost",
17+
},
18+
{
19+
"username": "userb",
20+
"nickname": "User B",
21+
"email": "userb@localhost",
22+
},
23+
{
24+
"username": "userc",
25+
"nickname": "User C",
26+
"email": "userc@localhost",
27+
},
28+
]
29+
30+
privacy_list = [
31+
{
32+
"username" : "usera",
33+
"certified" : True,
34+
"phone" : "13333333333",
35+
"balance" : 100,
36+
"password" : "hash_of_123",
37+
"paypassword" : "hash_of_sudfy8e7r",
38+
},
39+
{
40+
"username" : "userb",
41+
"certified" : True,
42+
"phone" : "12222222222",
43+
"balance" : 130,
44+
"password" : "hash_of_dfdfd",
45+
"paypassword" : "hash_of_234erere",
46+
},
47+
{
48+
"username" : "userc",
49+
"certified" : True,
50+
"phone" : "14323424234",
51+
"balance" : 600,
52+
"password" : "hash_of_w3erere",
53+
"paypassword" : "hash_of_ghtwertr",
54+
},
55+
]
56+
57+
moment_list = [
58+
{
59+
"username" : "usera",
60+
"date" : "2018-11-1",
61+
"content" : "test moment",
62+
},
63+
{
64+
"username" : "userb",
65+
"date" : "2018-11-2",
66+
"content" : "test moment from b",
67+
},
68+
{
69+
"username" : "userc",
70+
"date" : "2018-11-6",
71+
"content" : "test moment from c",
72+
},
73+
]
74+
75+
comment_list = [
76+
{
77+
"username" : "usera",
78+
"to_username" : "userb",
79+
"moment_id" : 1,
80+
"date" : "2018-12-1",
81+
"content" : "comment haha",
82+
},
83+
{
84+
"username" : "userb",
85+
"to_username" : "usera",
86+
"moment_id" : 2,
87+
"date" : "2018-12-2",
88+
"content" : "comment xixi",
89+
},
90+
{
91+
"username" : "userc",
92+
"to_username" : "usera",
93+
"moment_id" : 3,
94+
"date" : "2018-12-9",
95+
"content" : "comment hoho",
96+
},
97+
]
98+
99+
for d in user_list:
100+
if not User.get(User.c.username==d["username"]):
101+
print("create user '%s'"%(d["username"]))
102+
User(**d).save()
103+
for d in privacy_list:
104+
user = User.get(User.c.username==d["username"])
105+
if user:
106+
d["user_id"] = user.id
107+
print("create privacy record for user '%s'"%(d["username"]))
108+
Privacy(**d).save()
109+
else:
110+
print("error: unknown user '%s'"%(d["username"]))
111+
112+
for d in moment_list:
113+
user = User.get(User.c.username==d["username"])
114+
if user:
115+
d["user_id"] = user.id
116+
print("create moment record for user '%s'"%(d["username"]))
117+
Moment(**d).save()
118+
else:
119+
print("error: unknown user '%s'"%(d["username"]))
120+
121+
122+
for d in comment_list:
123+
user = User.get(User.c.username==d["username"])
124+
if user:
125+
d["user_id"] = user.id
126+
d["to_id"] = User.get(User.c.username==d["to_username"]).id
127+
print("create comment record for user '%s'"%(d["username"]))
128+
Comment(**d).save()
129+
else:
130+
print("error: unknown user '%s'"%(d["username"]))

demo/apps/apijson_demo/models.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#coding=utf-8
2+
3+
from uliweb.orm import *
4+
5+
class Privacy(Model):
6+
user_id = Reference("user")
7+
certified = Field(bool)
8+
phone = Field(str)
9+
balance = Field(DECIMAL)
10+
password = Field(str)
11+
paypassword = Field(str)
12+
13+
class Moment(Model):
14+
user_id = Reference("user")
15+
date = Field(datetime.datetime, auto_now_add=True)
16+
content = Field(TEXT)
17+
18+
class Comment(Model):
19+
user_id = Reference("user")
20+
to_id = Reference("user")
21+
moment_id = Reference("moment")
22+
date = Field(datetime.datetime, auto_now_add=True)
23+
content = Field(TEXT)

demo/apps/apijson_demo/settings.ini

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[MODELS]
2+
privacy = 'apijson_demo.models.Privacy'
3+
comment = 'apijson_demo.models.Comment'
4+
moment = 'apijson_demo.models.Moment'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{{extend "layout_demo.html"}}
2+
3+
{{block content_main}}
4+
{{use "ui.vue"}}
5+
{{use "ui.iview"}}
6+
7+
<div id="app">
8+
9+
<tabs v-model:value="tab_current" type="card" :animated="false">
10+
<tab-pane label="apijson get" name="tab_get"></tab-pane>
11+
<tab-pane label="apijson post" name="tab_post"></tab-pane>
12+
<tab-pane label="apijson put" name="tab_put"></tab-pane>
13+
</tabs>
14+
<Row v-if="tab_current==='tab_get'">
15+
<i-col span="3"><div align="center">POST URL</div></i-col>
16+
<i-col span="8"><i-input value="/apijson/get" readonly/></i-col>
17+
</Row>
18+
<Row v-if="tab_current==='tab_get'">
19+
<i-col span="3"><div align="center">demo request data list</div></i-col>
20+
<i-col span="8">
21+
<i-select v-model="request_data">
22+
<i-option v-for="item in request_data_list" :value="item.value" :key="item.value">{ item.label }</i-option>
23+
</i-select>
24+
</i-col>
25+
</Row>
26+
<Row>
27+
<i-col span="3"><div align="center">request data</div></i-col>
28+
<i-col span="8"><i-input v-model="request_data" type="textarea" :autosize="{minRows: 3,maxRows: 15}" placeholder="request data" /></i-col>
29+
<i-col span="1"><div align="center"><i-button type="primary" size="large" @click="post_request_data" v-bind:disabled="!can_post">Post</i-button></div></i-col>
30+
</Row>
31+
<Row>
32+
<i-col span="3"><div align="center">response data</div></i-col>
33+
<i-col span="8"><i-input v-model="response_data" type="textarea" readonly :autosize="{minRows: 5,maxRows: 30}" /></i-col>
34+
</Row>
35+
</div>
36+
<script>
37+
var vm = new Vue({
38+
el: '#app',
39+
delimiters: ['{', '}'],
40+
data: {
41+
request_data_list : {{=request_data_list_json}},
42+
request_data : "",
43+
can_post : true,
44+
response_data : "",
45+
tab_current : "tab_get"
46+
},
47+
methods: {
48+
init_default: function(){
49+
vm.request_data = vm.request_data_list[0].value
50+
},
51+
post_request_data: function(){
52+
vm.can_post = false
53+
$.ajax({
54+
type: "POST",
55+
contentType: 'application/json',
56+
url: "{{=url_for('uliweb_apijson.apijson.views.ApiJson.get')}}",
57+
data: vm.request_data,
58+
success: function (data) {
59+
vm.response_data = JSON.stringify(data,null,2)
60+
}
61+
})
62+
63+
}
64+
},
65+
watch: {
66+
request_data : function(n,o){
67+
vm.can_post = true
68+
}
69+
}
70+
})
71+
vm.init_default()
72+
</script>
73+
{{end content_main}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{extend "site/layout0.html"}}
2+
3+
{{block title}}uliweb-apijson demo{{end title}}
4+
5+
{{block header_container_logo}}{{end header_container_logo}}
6+
7+
{{block mainmenu}}
8+
{{<< mainmenu('apijson')}}
9+
{{end mainmenu}}
10+
11+
{{block sidemenu}}{{end sidemenu}}
12+
13+
{{block header_custom_menu}}{{end header_custom_menu}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This directory is used to store template files.

demo/apps/apijson_demo/views.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#coding=utf-8
2+
from uliweb import expose, functions
3+
from json import dumps
4+
5+
@expose('/')
6+
def index():
7+
request_data_list = [
8+
{
9+
"label":"Single record query: with id as parameter",
10+
"value":'''{
11+
"user":{
12+
"id":1
13+
}
14+
}''',
15+
},
16+
{
17+
"label":"Single record query: no parameter",
18+
"value":'''{
19+
"user":{
20+
}
21+
}''',
22+
},
23+
{
24+
"label":"Single record query: @column",
25+
"value":'''{
26+
"user":{
27+
"@column": "id,username,email"
28+
}
29+
}''',
30+
},
31+
{
32+
"label":"Array query",
33+
"value":'''{
34+
"[]":{
35+
"@count":2,
36+
"@page":0,
37+
"user":{
38+
"@column":"id,username,nickname,email",
39+
"@order":"id-"
40+
}
41+
}
42+
}''',
43+
},
44+
]
45+
return {
46+
"request_data_list_json":dumps(request_data_list)
47+
}

demo/apps/settings.ini

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[GLOBAL]
2+
DEBUG = False
3+
DEBUG_CONSOLE = False
4+
DEBUG_TEMPLATE = False
5+
6+
INSTALLED_APPS = [
7+
'uliweb.contrib.staticfiles',
8+
'uliweb.contrib.template',
9+
'uliweb.contrib.upload',
10+
'uliweb.contrib.orm',
11+
'uliweb.contrib.session',
12+
'uliweb.contrib.cache',
13+
'uliweb.contrib.auth',
14+
'uliweb.contrib.i18n',
15+
'uliweb.contrib.flashmessage',
16+
'uliweb_apps.site',
17+
'uliweb_comui',
18+
'uliweb_apijson.apijson',
19+
'apijson_demo',
20+
]
21+
22+
[MENUS]
23+
MAINMENU = {
24+
'subs':[
25+
{'name': 'apijson', 'link':'/', 'title':u'APIJSON Demo'},
26+
]
27+
}
28+
29+
[APIJSON_MODEL]
30+
#overwrite user table to public for test
31+
user = {
32+
"public" : True,
33+
"user_id_field" : "id",
34+
"secret_fields" : ["password"],
35+
"default_filter_by_self" : True
36+
}

demo/doc/imgs/demo_screenshot.png

26.6 KB
Loading
File renamed without changes.

demo/requirements.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
six
2+
SQLAlchemy
3+
-e git+https://github.com/limodou/uliweb3#egg=uliweb
4+
-e git+https://github.com/uliwebext/uliweb-ui#egg=uliweb_ui
5+
-e git+https://github.com/uliwebext/uliweb-layout#egg=uliweb_layout
6+
-e git+https://github.com/uliwebext/uliweb-menu#egg=uliweb_menu
7+
-e git+https://github.com/limodou/uliweb-apps#egg=uliweb_apps
8+
-e git+https://github.com/zhangchunlin/uliweb-comapps#egg=uliweb_comapps
9+
-e git+https://github.com/zhangchunlin/uliweb-apijson#egg=uliweb_apijson

0 commit comments

Comments
 (0)