Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
aa.gusti
/
opensipkd-base
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit a576c9a4
authored
Aug 28, 2025
by
aa.gustiana@gmail.com
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add Departemen model and refactor ApiViews for improved query handling
1 parent
97e10a94
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
17 deletions
opensipkd/base/models/__init__.py
opensipkd/base/models/departmen.py
opensipkd/base/views/api_base.py
opensipkd/base/models/__init__.py
View file @
a576c9a
...
...
@@ -6,3 +6,4 @@ from .wilayah import *
from
.partner
import
*
from
.targets
import
*
from
.user_area
import
*
from
.departmen
import
*
opensipkd/base/models/departmen.py
0 → 100644
View file @
a576c9a
from
sqlalchemy
import
(
Column
,
Integer
,
ForeignKey
,
String
,
SmallInteger
)
from
sqlalchemy.orm
import
(
relationship
,
backref
)
from
..models
import
DBSession
,
Base
from
..models
import
(
NamaModel
,
TABLE_ARGS
)
class
Departemen
(
Base
,
NamaModel
):
__tablename__
=
'departemen'
__table_args__
=
(
TABLE_ARGS
,)
id
=
Column
(
Integer
,
primary_key
=
True
)
parent_id
=
Column
(
Integer
,
ForeignKey
(
'public.departemen.id'
))
kategori
=
Column
(
String
(
32
))
alamat
=
Column
(
String
(
255
))
singkat
=
Column
(
String
(
32
))
level_id
=
Column
(
SmallInteger
)
children
=
relationship
(
"Departemen"
,
backref
=
backref
(
'parent'
,
remote_side
=
[
id
]))
def
get_parents
(
self
,
start
=
False
):
allparents
=
[]
if
start
:
allparents
.
append
(
self
.
nama
)
p
=
self
.
parent
while
p
:
allparents
.
append
(
p
.
nama
)
p
=
p
.
parent
return
allparents
@property
def
parents
(
self
,
start
=
False
):
return
self
.
get_parents
()
@property
def
name_get
(
self
):
allparents
=
self
.
get_parents
(
True
)
return
'/'
.
join
(
reversed
(
allparents
))
@property
def
uraian_all
(
self
):
allparents
=
self
.
get_parents
(
True
)
return
'/'
.
join
(
reversed
(
allparents
))
@classmethod
def
get_list
(
cls
):
return
DBSession
.
query
(
cls
.
id
,
cls
.
nama
)
.
order_by
(
cls
.
nama
)
.
all
()
opensipkd/base/views/api_base.py
View file @
a576c9a
...
...
@@ -4,7 +4,6 @@ from deform import Form
from
pyramid.response
import
Response
from
pyramid.exceptions
import
HTTPNotFound
from
opensipkd.base.models
import
DBSession
from
opensipkd.tools.pbb
import
FixSppt
from
opensipkd.tools.buttons
import
btn_save
,
btn_cancel
from
.
import
api_messages
from
..tools
import
obj2json
...
...
@@ -66,11 +65,8 @@ class ApiViews:
return
Form
(
schema
,
buttons
=
buttons
,
autocomplete
=
self
.
autocomplete
)
def
filter_ids
(
self
,
query
,
**
kw
):
# table = kw.get("table", self.table)
ids
=
FixSppt
(
self
.
id
)
.
row_dotted
.
split
(
"."
)
filters
=
dict
(
zip
(
self
.
pkey
,
ids
))
return
query
.
filter_by
(
**
filters
)
def
get_filters
(
self
,
query
,
**
kw
):
return
query
def
get_orders
(
self
,
query
,
**
kw
):
table
=
kw
.
get
(
"table"
,
self
.
table
)
...
...
@@ -80,26 +76,39 @@ class ApiViews:
query
=
query
.
order_by
(
*
(
getattr
(
table
,
k
)
for
k
in
self
.
orders
))
return
query
def
get_joins
(
self
,
query
,
**
kw
):
return
query
def
get_groups
(
self
,
query
,
**
kw
):
return
query
def
query
(
self
,
**
kw
):
table
=
kw
.
get
(
"table"
,
self
.
table
)
filter_ids
=
kw
.
get
(
"filters"
,
self
.
filter_ids
)
orders
=
kw
.
get
(
"orders"
,
self
.
get_orders
)
get_filters
=
kw
.
get
(
"filters"
,
self
.
get_filters
)
get_joins
=
kw
.
get
(
"joins"
,
self
.
get_joins
)
get_groups
=
kw
.
get
(
"groups"
,
self
.
get_groups
)
get_orders
=
kw
.
get
(
"orders"
,
self
.
get_orders
)
query
=
self
.
db_session
.
query
(
table
)
if
self
.
id
:
query
=
filter_ids
(
query
,
table
=
table
)
else
:
query
=
orders
(
query
,
table
=
table
)
query
=
query
.
limit
(
self
.
psize
)
.
offset
(
(
self
.
page
-
1
)
*
self
.
psize
)
query
=
get_joins
(
query
,
table
=
table
)
query
=
get_groups
(
query
,
table
=
table
)
query
=
get_filters
(
query
,
table
=
table
)
query
=
get_orders
(
query
,
table
=
table
)
query
=
query
.
limit
(
self
.
psize
)
.
offset
((
self
.
page
-
1
)
*
self
.
psize
)
return
query
def
query_id
(
self
,
**
kw
):
table
=
kw
.
get
(
"table"
,
self
.
table
)
orders
=
kw
.
get
(
"orders"
,
self
.
get_orders
)
get_joins
=
kw
.
get
(
"joins"
,
self
.
get_joins
)
get_groups
=
kw
.
get
(
"groups"
,
self
.
get_groups
)
get_filters
=
kw
.
get
(
"filters"
,
self
.
get_filters
)
get_orders
=
kw
.
get
(
"orders"
,
self
.
get_orders
)
if
hasattr
(
table
,
"query_id"
)
and
self
.
id
:
query
=
table
.
query_id
(
self
.
id
)
query
=
orders
(
query
,
table
=
table
)
query
=
get_joins
(
query
,
table
=
table
)
query
=
get_groups
(
query
,
table
=
table
)
query
=
get_filters
(
query
,
table
=
table
)
query
=
get_orders
(
query
,
table
=
table
)
query
=
query
.
limit
(
self
.
psize
)
.
offset
(
(
self
.
page
-
1
)
*
self
.
psize
)
return
query
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment