44Created on 2020-03-21
55:author: Oshane Bailey (b4.oshany@gmail.com)
66"""
7-
7+ import json
88from pyramid .view import view_config
99from pyramid .view import view_defaults
10+ from sqlalchemy import or_
1011
1112from kotti_audit import _
13+ from kotti_audit import utils
1214from kotti_audit .fanstatic import css_and_js
1315from kotti_audit .views import BaseView
1416from kotti .resources import Content
@@ -21,58 +23,97 @@ class AuditLogViews(BaseView):
2123 @view_config (name = 'audit-log' , permission = 'view' ,
2224 renderer = 'kotti_audit:templates/audit-log.pt' )
2325 def default_view (self ):
24- """ Default view for :class:`kotti_audit.resources.CustomContent `
26+ """ Default view`
2527
2628 :result: Dictionary needed to render the template.
2729 :rtype: dict
2830 """
2931
30- return {
31- 'foo' : _ (u'bar' ),
32- }
32+ return {}
3333
3434 @view_config (name = "audit-api" , permission = "admin" ,
3535 renderer = "json" )
3636 def api_dropbox (self ):
37+ """ API view`
38+
39+ :result: Dictionary needed to render the template.
40+ :rtype: dict
41+ """
3742 limit = int (self .request .params .get ("limit" , 50 ))
3843 offset = int (self .request .params .get ("offset" , 0 ))
39- sort = self .request .params .get ("sort" , "creation_date " )
44+ sort = self .request .params .get ("sort" , "modification_date " )
4045 order = self .request .params .get ("order" , "desc" )
41- parent_id = self .context . id if self . context . __parent__ else 0
42- query = self .request .params .get ("search" , None )
46+ filters = self .request . params . get ( "filter" , None )
47+ search = self .request .params .get ("search" , None )
4348
44- nodes = Content .query ;
49+ query = Content .query ;
4550
51+ # Use the current context as the base location of the search,
52+ # otherwise, use root as the base location if no parent is found.
53+ parent_id = self .context .id if self .context .__parent__ else 0
4654 if parent_id != 0 :
47- nodes = nodes .filter (
55+ query = query .filter (
4856 Content .path .ilike ('{}%' .format (self .context .path )),
4957 Content .id != self .context .id
5058 )
5159
52- if query is not None :
53- nodes = nodes .filter (Content .title .ilike ("%{}%" .format (query )))
60+ if filters is not None :
61+ filters = json .loads (filters )
62+
63+ if 'creation_date' in filters :
64+ creation_date = filters ['creation_date' ]
65+ query = utils .date_query (query , 'creation_date' , creation_date )
66+
67+ if 'modification_date' in filters :
68+ modification_date = filters ['modification_date' ]
69+ query = utils .date_query (query , 'modification_date' , modification_date )
70+
71+ if 'title' in filters :
72+ query = query .filter (
73+ Content .title .ilike ("%{}%" .format (filters ['title' ])))
74+
75+ if 'type' in filters :
76+ query = query .filter_by (type = filters ['type' ].lower ())
77+
78+ if 'parent' in filters :
79+ query = query .filter (
80+ Content .parent .title .ilike (
81+ "%{}%" .format (filters ['parent' ])))
82+
83+
84+ if search :
85+ # TODO: Add an `or` statement to find content with similar (like)
86+ # title and parent title.
87+ query = query .filter (
88+ or_ (
89+ Content .title .ilike ("%{}%" .format (search )),
90+ Content .type == search .lower ()
91+ )
92+ )
5493
5594 if order == 'asc' :
56- nodes = nodes .order_by (Content .__dict__ .get (sort ).asc ())
95+ query = query .order_by (Content .__dict__ .get (sort ).asc ())
5796 else :
58- nodes = nodes .order_by (Content .__dict__ .get (sort ).desc ())
97+ query = query .order_by (Content .__dict__ .get (sort ).desc ())
5998
60- total_nodes = nodes .count ()
99+ total_query = query .count ()
61100 if offset > 0 :
62- nodes = nodes .offset (offset )
101+ query = query .offset (offset )
63102 if limit > 0 :
64- nodes = nodes .limit (limit )
103+ query = query .limit (limit )
65104
66- nodes_json = [{
105+ # build bootstrap table response.
106+ query_json = [{
67107 "title" : f .title ,
68- "context " : f .parent .title if f .parent is not None else '' ,
108+ "parent " : f .parent .title if f .parent is not None else '' ,
69109 "path" : f .path ,
110+ 'type' : f .type .title (),
70111 "url" : f .path ,
71112 "id" : f .id ,
72113 "creation_date" : f .creation_date .strftime ("%A %d. %B %Y - %I:%M%p %z " ) or '' ,
73114 "modification_date" : f .modification_date .strftime ("%A %d. %B %Y - %I:%M%p %z " ) or ''
74- } for f in nodes ]
115+ } for f in query ]
75116 return {
76- "rows" : nodes_json ,
77- "total" : total_nodes
117+ "rows" : query_json ,
118+ "total" : total_query
78119 }
0 commit comments