Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
aa.gusti
/
opensipkd-tools
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 d84d05c9
authored
Jul 25, 2022
by
aagusti
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
app-mobile-api
1 parent
cd2cc954
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
155 additions
and
6 deletions
opensipkd/tools/__init__.py
opensipkd/tools/alembic_helpers.py
opensipkd/tools/buttons.py
opensipkd/tools/form_api.py
opensipkd/tools/__init__.py
View file @
d84d05c
...
...
@@ -13,6 +13,7 @@ import colander
import
pytz
import
io
from
pyramid.httpexceptions
import
HTTPNotFound
from
pyramid.threadlocal
import
get_current_registry
from
json
import
JSONEncoder
...
...
@@ -451,10 +452,20 @@ def image_validator(node, value):
raise
colander
.
Invalid
(
node
,
f
'Extension harus salahsatu dari {img_exts}'
)
def
file_response
(
request
,
f
,
filename
,
type
):
response
=
request
.
response
response
.
content_type
=
str
(
type
)
response
.
content_disposition
=
'filename='
+
filename
response
.
write
(
f
.
read
())
return
response
class
Upload
(
SaveFile
):
def
save_to_file
(
self
,
input_file
,
ext
):
fullpath
=
self
.
create_fullpath
(
ext
)
def
save_to_file
(
self
,
input_file
,
ext
,
filename
=
None
):
if
filename
:
fullpath
=
os
.
path
.
join
(
self
.
dir_path
,
filename
,
ext
)
else
:
fullpath
=
self
.
create_fullpath
(
ext
)
output_file
=
open
(
fullpath
,
'wb'
)
input_file
.
seek
(
0
)
while
True
:
...
...
@@ -465,14 +476,13 @@ class Upload(SaveFile):
output_file
.
close
()
return
fullpath
def
save
(
self
,
request
,
name
,
exts
=
None
):
def
save
(
self
,
request
,
name
,
exts
=
None
,
filename
=
None
):
input_file
=
request
.
POST
[
name
]
.
file
ext
=
get_ext
(
request
.
POST
[
name
]
.
filename
)
if
exts
and
ext
not
in
exts
:
raise
InvalidExtension
(
exts
)
filename
=
self
.
save_to_file
(
input_file
,
ext
)
print
(
filename
)
filename
=
self
.
save_to_file
(
input_file
,
ext
,
filename
=
filename
)
head
,
filename
=
os
.
path
.
split
(
filename
)
return
filename
...
...
@@ -499,6 +509,15 @@ class Upload(SaveFile):
d
[
filename
]
=
self
.
save_fp
(
upload
)
return
d
def
download
(
self
,
request
,
filename
):
out_filename
=
os
.
path
.
join
(
self
.
dir_path
,
filename
)
if
not
os
.
path
.
exists
(
out_filename
):
raise
HTTPNotFound
ext
=
get_ext
(
filename
)[
1
:]
if
ext
in
[
'gif'
,
'png'
,
'jpeg'
,
'jpg'
]:
ext
=
'image/'
+
ext
with
open
(
out_filename
,
'rb'
)
as
f
:
return
file_response
(
request
,
f
,
filename
,
ext
)
class
UploadBin
(
Upload
):
"""
...
...
opensipkd/tools/alembic_helpers.py
0 → 100644
View file @
d84d05c
# http://www.derstappen-it.de/tech-blog/sqlalchemie-alembic-check-if-table-has-column
from
alembic
import
op
from
sqlalchemy.engine
import
reflection
def
table_has_column
(
table
,
column
):
engine
=
op
.
get_bind
()
insp
=
reflection
.
Inspector
.
from_engine
(
engine
)
has_column
=
False
for
col
in
insp
.
get_columns
(
table
):
if
column
not
in
col
[
'name'
]:
continue
has_column
=
True
return
has_column
opensipkd/tools/buttons.py
View file @
d84d05c
...
...
@@ -11,7 +11,7 @@ btn_filter = Button('filter', title='Filter', css_class="btn-info")
btn_cancel
=
Button
(
'cancel'
,
title
=
'Batal'
,
css_class
=
"btn-warning"
)
btn_reset
=
Button
(
'reset'
,
title
=
'Reset'
,
css_class
=
"btn-warning"
,
type
=
"reset"
)
btn_close
=
Button
(
'close'
,
title
=
'Tutup'
,
css_class
=
"btn-danger"
)
btn_password
=
Button
(
'reset'
,
title
=
'Reset'
,
css_class
=
"btn-warning"
)
btn_print
=
Button
(
'print'
,
title
=
'Print'
,
css_class
=
"btn-info"
,
type
=
"button"
)
btn_pdf
=
Button
(
'pdf'
,
title
=
'PDF'
,
css_class
=
"btn-info"
,
type
=
"button"
)
...
...
@@ -54,3 +54,7 @@ force_buttons = [btn_force, btn_cancel]
flow_2_buttons
=
[
btn_prev
,
btn_next
,
btn_cancel
]
pdf_txt_buttons
=
[
btn_pdf
,
btn_txt
,
btn_close
]
pdf_buttons
=
[
btn_pdf
,
btn_close
]
btn_login
=
Button
(
'login'
,
title
=
'Masuk'
,
css_class
=
"btn-success"
)
btn_register
=
Button
(
'register'
,
title
=
'Register'
,
css_class
=
"btn-warning"
)
btn_password
=
Button
(
'get_password'
,
title
=
'Get Password'
,
css_class
=
"btn-danger"
)
\ No newline at end of file
opensipkd/tools/form_api.py
0 → 100644
View file @
d84d05c
import
traceback
import
colander
import
deform.widget
from
icecream
import
ic
field_widgets
=
(
deform
.
widget
.
TextInputWidget
,
deform
.
widget
.
MoneyInputWidget
,
deform
.
widget
.
AutocompleteInputWidget
,
deform
.
widget
.
TimeInputWidget
,
deform
.
widget
.
DateInputWidget
,
deform
.
widget
.
DateTimeInputWidget
,
deform
.
widget
.
TextAreaWidget
,
deform
.
widget
.
RichTextWidget
,
deform
.
widget
.
PasswordWidget
,
deform
.
widget
.
HiddenWidget
,
deform
.
widget
.
CheckboxWidget
,
deform
.
widget
.
SelectWidget
,
deform
.
widget
.
Select2Widget
,
deform
.
widget
.
SelectizeWidget
,
deform
.
widget
.
RadioChoiceWidget
,
deform
.
widget
.
CheckboxChoiceWidget
,
deform
.
widget
.
CheckedInputWidget
,
deform
.
widget
.
CheckedPasswordWidget
,
deform
.
widget
.
MappingWidget
,
deform
.
widget
.
SequenceWidget
,
deform
.
widget
.
FileUploadWidget
,
deform
.
widget
.
DatePartsWidget
,
deform
.
widget
.
TextAreaCSVWidget
,
deform
.
widget
.
TextInputCSVWidget
,)
field2remove
=
(
"_parent"
,
"counter"
,
"renderer"
,
"resource_registry"
,
"requirements"
,
"bindings"
)
def
form2dict
(
obj
):
d
=
{}
if
getattr
(
obj
,
"__dict__"
,
str
(
obj
)):
d
=
obj
.
__dict__
for
k
,
v
in
d
.
items
():
try
:
if
type
(
v
)
==
colander
.
Invalid
:
ic
(
dir
(
v
))
d
[
k
]
=
v
.
asdict
()
elif
v
and
hasattr
(
v
,
"__dict__"
)
and
\
getattr
(
v
,
"__dict__"
,
str
(
v
)):
d
[
k
]
=
form2dict
(
v
)
elif
isinstance
(
v
,
list
):
lv
=
[]
for
l
in
v
:
if
l
and
hasattr
(
l
,
"__dict__"
)
and
\
getattr
(
l
,
"__dict__"
,
str
(
l
)):
lv
.
append
(
form2dict
(
l
))
d
[
k
]
=
lv
elif
getattr
(
v
,
"__dict__"
,
str
(
v
))
==
{}:
d
[
k
]
=
{}
if
k
in
field2remove
:
d
[
k
]
=
""
if
k
==
"widget"
and
type
(
v
)
in
field_widgets
:
# if hasattr(d[k], "udpate"):
d
[
k
]
.
update
({
"template"
:
v
.
template
})
if
k
==
"missing"
and
v
==
colander
.
drop
:
d
[
k
]
=
True
except
Exception
as
e
:
print
(
e
)
traceback
.
print_exc
()
ic
(
k
,
v
)
return
d
def
remove_child
(
data
):
resp
=
[]
for
d
in
data
:
if
"children"
in
d
and
d
[
"children"
]:
# try:
d
[
"children"
]
=
remove_child
(
d
[
"children"
])
# except:
# pass
res
=
{}
for
x
in
d
:
if
x
not
in
field2remove
:
res
[
x
]
=
d
[
x
]
if
x
==
"_cstruct"
and
d
[
x
]
==
colander
.
null
:
res
[
x
]
=
None
resp
.
append
(
res
)
return
resp
def
formfield2dict
(
obj
):
resp
=
form2dict
(
obj
)
children
=
resp
[
"children"
]
children
=
remove_child
(
children
)
table
=
"table"
in
resp
and
resp
[
"table"
]
or
None
# for child in children:
# c_order = child["schema"]["_order"]
# count = 0
# for f in fields:
# if f["_order"] == c_order:
# f["children"] = (child["children"])
# fields[count]=f
# count += 1
# ic(resp)
return
dict
(
children
=
children
,
buttons
=
resp
[
"buttons"
],
action
=
resp
[
"action"
],
table
=
table
)
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