Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
project-api
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
project-api
Commits
d12a8c42
Commit
d12a8c42
authored
Mar 31, 2018
by
fangzhipeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新获取数据方式
parent
9b7ff7a1
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
9 deletions
+34
-9
common/util/http.py
common/util/http.py
+1
-1
daylife/dao/models.py
daylife/dao/models.py
+12
-3
daylife/dao/record_dao.py
daylife/dao/record_dao.py
+7
-3
daylife/record_service.py
daylife/record_service.py
+1
-1
util/json_util.py
util/json_util.py
+13
-1
No files found.
common/util/http.py
View file @
d12a8c42
...
...
@@ -13,4 +13,4 @@ class BaseRes:
返回json串
:return:
"""
return
json_util
.
to_json
(
self
)
\ No newline at end of file
return
json_util
.
to_json
(
self
.
__dict__
)
\ No newline at end of file
daylife/dao/models.py
View file @
d12a8c42
# coding: utf-8
from
sqlalchemy
import
Column
,
DateTime
,
In
teger
,
String
,
text
from
sqlalchemy
import
Column
,
DateTime
,
In
dex
,
Integer
,
String
,
Table
,
text
from
sqlalchemy.ext.declarative
import
declarative_base
...
...
@@ -7,12 +7,18 @@ Base = declarative_base()
metadata
=
Base
.
metadata
t_test
=
Table
(
'test'
,
metadata
,
Column
(
'test'
,
String
(
2
))
)
class
UserInfo
(
Base
):
__tablename__
=
'user_info'
id
=
Column
(
Integer
,
primary_key
=
True
,
nullable
=
False
)
phone
=
Column
(
String
(
20
),
nullable
=
False
,
unique
=
True
)
nickname
=
Column
(
String
(
50
),
primary_key
=
True
,
nullable
=
False
,
server_default
=
text
(
"''"
))
nickname
=
Column
(
String
(
50
),
primary_key
=
True
,
nullable
=
False
,
unique
=
True
,
server_default
=
text
(
"''"
))
create_at
=
Column
(
DateTime
,
nullable
=
False
,
server_default
=
text
(
"CURRENT_TIMESTAMP"
))
update_at
=
Column
(
DateTime
,
nullable
=
False
,
server_default
=
text
(
"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
))
...
...
@@ -23,12 +29,15 @@ class UserRecord(Base):
id
=
Column
(
Integer
,
primary_key
=
True
)
user_id
=
Column
(
Integer
,
nullable
=
False
)
content
=
Column
(
String
(
500
),
nullable
=
False
)
cr
ae
te_at
=
Column
(
DateTime
,
nullable
=
False
,
server_default
=
text
(
"CURRENT_TIMESTAMP"
))
cr
ea
te_at
=
Column
(
DateTime
,
nullable
=
False
,
server_default
=
text
(
"CURRENT_TIMESTAMP"
))
update_at
=
Column
(
DateTime
,
nullable
=
False
,
server_default
=
text
(
"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
))
class
UserToken
(
Base
):
__tablename__
=
'user_token'
__table_args__
=
(
Index
(
'uk_device_type_user_id'
,
'user_id'
,
'device_type'
,
unique
=
True
),
)
id
=
Column
(
Integer
,
primary_key
=
True
)
user_id
=
Column
(
Integer
,
nullable
=
False
)
...
...
daylife/dao/record_dao.py
View file @
d12a8c42
...
...
@@ -6,7 +6,8 @@ session.commit()
session.close()
"""
from
daylife.dao
import
sqlengin
from
daylife.dao.models
import
UserRecord
from
daylife.dao.models
import
UserRecord
,
UserInfo
from
util
import
json_util
def
add_record
(
user_record
):
...
...
@@ -18,6 +19,9 @@ def add_record(user_record):
def
select_by_user_id
(
user_id
):
session
=
sqlengin
.
getSession
()
items
=
session
.
query
(
UserRecord
)
.
filter
(
UserRecord
.
user_id
==
user_id
)
.
order_by
(
UserRecord
.
id
.
desc
())
.
all
()
items
=
session
.
execute
(
'select t.*, tt.nickname from user_record t, user_info tt WHERE t.user_id=tt.id AND t.user_id=:user_id ORDER BY t.create_at DESC '
,
{
'user_id'
:
user_id
})
.
fetch
all
()
session
.
close
()
return
items
\ No newline at end of file
return
[
dict
(
x
.
items
())
for
x
in
items
]
if
__name__
==
'__main__'
:
print
(
json_util
.
to_json
([
dict
(
x
.
items
())
for
x
in
select_by_user_id
(
1
)]))
\ No newline at end of file
daylife/record_service.py
View file @
d12a8c42
...
...
@@ -18,7 +18,7 @@ def get_my_record():
"""
user_id
=
request
.
headers
.
get
(
'user_id'
)
items
=
record_dao
.
select_by_user_id
(
user_id
)
return
http
.
BaseRes
(
data
=
json_util
.
convert_db_to_json_obj
(
items
)
)
.
to_json
()
return
http
.
BaseRes
(
data
=
items
)
.
to_json
()
@
record
.
route
(
'/my_follow'
)
...
...
util/json_util.py
View file @
d12a8c42
# coding=utf-8
import
datetime
import
decimal
import
json
import
time
from
daylife.dao.sqlengin
import
AlchemyEncoder
class
MyJSONEncoder
(
json
.
JSONEncoder
):
def
default
(
self
,
obj
):
if
isinstance
(
obj
,
(
datetime
.
datetime
,)):
return
int
(
time
.
mktime
(
obj
.
timetuple
())
*
1000
)
elif
isinstance
(
obj
,
(
decimal
.
Decimal
,)):
return
{
"val"
:
str
(
obj
),
"_spec_type"
:
"decimal"
}
else
:
return
super
()
.
default
(
obj
)
def
to_json
(
obj
):
"""
转换为json输出
:param obj:
:return:
"""
return
json
.
dumps
(
obj
,
default
=
lambda
item
:
item
.
__dict__
,
ensure_ascii
=
False
)
return
json
.
dumps
(
obj
,
cls
=
MyJSONEncoder
,
ensure_ascii
=
False
)
def
db_to_json
(
obj
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment