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 cd2cc954
authored
Jul 12, 2022
by
aagusti
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
perbaikan Upload
1 parent
5833d1af
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
143 additions
and
41 deletions
opensipkd/tools/__init__.py
opensipkd/tools/api.py
opensipkd/tools/pbb.py
opensipkd/tools/report.py
opensipkd/tools/__init__.py
View file @
cd2cc95
...
@@ -4,16 +4,17 @@ import re
...
@@ -4,16 +4,17 @@ import re
import
mimetypes
import
mimetypes
import
csv
import
csv
import
calendar
import
calendar
# from datetime import (date, datetime, timedelta, )
import
datetime
import
datetime
from
random
import
choice
from
random
import
choice
from
string
import
(
ascii_uppercase
,
ascii_lowercase
,
digits
,
)
from
string
import
(
ascii_uppercase
,
ascii_lowercase
,
digits
,
)
import
locale
import
locale
import
colander
import
pytz
import
pytz
import
io
import
io
from
pyramid.threadlocal
import
get_current_registry
from
pyramid.threadlocal
import
get_current_registry
# from calendar import monthrange
from
json
import
JSONEncoder
from
json
import
JSONEncoder
import
logging
import
logging
...
@@ -115,6 +116,7 @@ def round_up(n):
...
@@ -115,6 +116,7 @@ def round_up(n):
def
get_settings
():
def
get_settings
():
return
get_current_registry
()
.
settings
return
get_current_registry
()
.
settings
def
get_params
(
params
,
alternate
=
None
):
def
get_params
(
params
,
alternate
=
None
):
"""
"""
Digunakan untuk mengambil nilai dari konfigurasi sesuai params yang disebut
Digunakan untuk mengambil nilai dari konfigurasi sesuai params yang disebut
...
@@ -125,7 +127,8 @@ def get_params(params, alternate=None):
...
@@ -125,7 +127,8 @@ def get_params(params, alternate=None):
get_params('devel', False)
get_params('devel', False)
"""
"""
settings
=
get_settings
()
settings
=
get_settings
()
result
=
settings
and
params
in
settings
and
settings
[
params
]
.
strip
()
or
None
result
=
settings
and
params
in
settings
and
settings
[
params
]
.
strip
()
or
None
return
result
and
result
or
alternate
return
result
and
result
or
alternate
...
@@ -411,14 +414,44 @@ class SaveFile(object):
...
@@ -411,14 +414,44 @@ class SaveFile(object):
if
not
os
.
path
.
exists
(
full_path
):
if
not
os
.
path
.
exists
(
full_path
):
return
full_path
return
full_path
def
save
(
self
,
content
,
filename
=
None
):
def
save
(
self
,
content
,
ext
=
''
,
filename
=
None
):
fullpath
=
self
.
create_fullpath
()
if
filename
:
fullpath
=
os
.
path
.
join
(
self
.
dir_path
,
filename
)
else
:
fullpath
=
self
.
create_fullpath
(
ext
=
ext
)
f
=
open
(
fullpath
,
'wb'
)
f
=
open
(
fullpath
,
'wb'
)
if
type
(
content
)
==
io
.
BytesIO
:
f
.
write
(
content
.
getbuffer
())
else
:
f
.
write
(
content
)
f
.
write
(
content
)
f
.
close
()
f
.
close
()
return
fullpath
return
fullpath
class
InvalidExtension
(
Exception
):
def
__init__
(
self
,
exts
):
self
.
error
=
f
"Supported extensions {exts}"
class
MemoryTmpStore
(
dict
):
""" Instances of this class implement the
:class:`defWorm.interfaces.FileUploadTempStore` interface"""
def
preview_url
(
self
,
uid
):
return
None
mem_tmp_store
=
MemoryTmpStore
()
img_exts
=
[
'.png'
,
'.jpg'
,
'.pdf'
,
'.jpeg'
]
def
image_validator
(
node
,
value
):
ext
=
get_ext
(
value
[
"filename"
])
if
ext
not
in
img_exts
:
raise
colander
.
Invalid
(
node
,
f
'Extension harus salahsatu dari {img_exts}'
)
class
Upload
(
SaveFile
):
class
Upload
(
SaveFile
):
def
save_to_file
(
self
,
input_file
,
ext
):
def
save_to_file
(
self
,
input_file
,
ext
):
fullpath
=
self
.
create_fullpath
(
ext
)
fullpath
=
self
.
create_fullpath
(
ext
)
...
@@ -432,10 +465,17 @@ class Upload(SaveFile):
...
@@ -432,10 +465,17 @@ class Upload(SaveFile):
output_file
.
close
()
output_file
.
close
()
return
fullpath
return
fullpath
def
save
(
self
,
request
,
name
):
def
save
(
self
,
request
,
name
,
exts
=
None
):
input_file
=
request
.
POST
[
name
]
.
file
input_file
=
request
.
POST
[
name
]
.
file
ext
=
get_ext
(
request
.
POST
[
name
]
.
filename
)
ext
=
get_ext
(
request
.
POST
[
name
]
.
filename
)
return
self
.
save_to_file
(
input_file
,
ext
)
if
exts
and
ext
not
in
exts
:
raise
InvalidExtension
(
exts
)
filename
=
self
.
save_to_file
(
input_file
,
ext
)
print
(
filename
)
head
,
filename
=
os
.
path
.
split
(
filename
)
return
filename
def
save_fp
(
self
,
upload
):
def
save_fp
(
self
,
upload
):
if
'fp'
not
in
upload
or
upload
[
'fp'
]
==
b
''
:
if
'fp'
not
in
upload
or
upload
[
'fp'
]
==
b
''
:
...
@@ -460,17 +500,6 @@ class Upload(SaveFile):
...
@@ -460,17 +500,6 @@ class Upload(SaveFile):
return
d
return
d
class
MemoryTmpStore
(
dict
):
""" Instances of this class implement the
:class:`defWorm.interfaces.FileUploadTempStore` interface"""
def
preview_url
(
self
,
uid
):
return
None
mem_tmp_store
=
MemoryTmpStore
()
class
UploadBin
(
Upload
):
class
UploadBin
(
Upload
):
"""
"""
Compatibility to previous
Compatibility to previous
...
@@ -497,12 +526,14 @@ class CSVRenderer(object):
...
@@ -497,12 +526,14 @@ class CSVRenderer(object):
# karena tak mau hapus, maka dibikin try
# karena tak mau hapus, maka dibikin try
try
:
try
:
fout
=
io
.
BytesIO
()
#
fout
=
io
.
BytesIO
()
#
fcsv
=
csv
.
writer
(
fout
,
delimiter
=
','
,
quotechar
=
'"'
,
quoting
=
csv
.
QUOTE_MINIMAL
)
fcsv
=
csv
.
writer
(
fout
,
delimiter
=
','
,
quotechar
=
'"'
,
quoting
=
csv
.
QUOTE_MINIMAL
)
fcsv
.
writerow
(
value
.
get
(
'header'
,
[]))
fcsv
.
writerow
(
value
.
get
(
'header'
,
[]))
fcsv
.
writerows
(
value
.
get
(
'rows'
,
[]))
fcsv
.
writerows
(
value
.
get
(
'rows'
,
[]))
except
:
except
:
fout
=
io
.
StringIO
()
fout
=
io
.
StringIO
()
fcsv
=
csv
.
writer
(
fout
,
delimiter
=
','
,
quotechar
=
'"'
,
quoting
=
csv
.
QUOTE_MINIMAL
)
fcsv
=
csv
.
writer
(
fout
,
delimiter
=
','
,
quotechar
=
'"'
,
quoting
=
csv
.
QUOTE_MINIMAL
)
fcsv
.
writerow
(
value
.
get
(
'header'
,
[]))
fcsv
.
writerow
(
value
.
get
(
'header'
,
[]))
fcsv
.
writerows
(
value
.
get
(
'rows'
,
[]))
fcsv
.
writerows
(
value
.
get
(
'rows'
,
[]))
...
@@ -916,7 +947,8 @@ satuan = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh',
...
@@ -916,7 +947,8 @@ satuan = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh',
def
terbilang_
(
n
):
def
terbilang_
(
n
):
n
=
int
(
n
)
n
=
int
(
n
)
if
n
>=
1000000000
:
if
n
>=
1000000000
:
hasil
=
terbilang_
(
n
/
1000000000
)
+
[
'milyar'
]
+
terbilang_
(
n
%
100000000
)
hasil
=
terbilang_
(
n
/
1000000000
)
+
[
'milyar'
]
+
terbilang_
(
n
%
100000000
)
elif
n
>
1000000
:
elif
n
>
1000000
:
hasil
=
terbilang_
(
n
/
1000000
)
+
[
'juta'
]
+
terbilang_
(
n
%
1000000
)
hasil
=
terbilang_
(
n
/
1000000
)
+
[
'juta'
]
+
terbilang_
(
n
%
1000000
)
elif
n
>=
2000
:
elif
n
>=
2000
:
...
...
opensipkd/tools/api.py
View file @
cd2cc95
import
base64
import
base64
import
hashlib
import
hashlib
import
hmac
import
hmac
import
json
import
sys
import
sys
from
datetime
import
datetime
from
datetime
import
datetime
import
requests
from
pyramid_rpc.jsonrpc
import
JsonRpcError
from
pyramid_rpc.jsonrpc
import
JsonRpcError
from
opensipkd.tools
import
get_settings
,
get_random_number
LIMIT
=
1000
LIMIT
=
1000
CODE_OK
=
0
CODE_OK
=
0
MSG_OK
=
'Data Submitted'
MSG_OK
=
'Data Submitted'
...
@@ -44,7 +48,6 @@ class JsonRpcDataFoundError(JsonRpcError):
...
@@ -44,7 +48,6 @@ class JsonRpcDataFoundError(JsonRpcError):
message
=
"Error Data Found"
message
=
"Error Data Found"
# error terkait authentication dan authorization
class
JsonRpcInvalidLoginError
(
JsonRpcError
):
class
JsonRpcInvalidLoginError
(
JsonRpcError
):
code
=
-
41001
code
=
-
41001
message
=
"Invalid RPC User/Password"
message
=
"Invalid RPC User/Password"
...
@@ -278,6 +281,66 @@ def json_rpc_header(userid, password, key=None):
...
@@ -278,6 +281,66 @@ def json_rpc_header(userid, password, key=None):
return
dict
(
userid
=
userid
,
signature
=
encoded_signature
,
key
=
key
)
return
dict
(
userid
=
userid
,
signature
=
encoded_signature
,
key
=
key
)
def
get_mandatory
(
data
,
values
):
for
value
in
values
:
if
value
not
in
data
or
not
data
[
value
]:
raise
JsonRpcInvalidDataError
(
message
=
"{} Not Found"
.
format
(
value
))
def
validate_time
(
request
):
global
lima_menit
env
=
request
.
environ
time_stamp
=
int
(
env
[
'HTTP_KEY'
])
now
=
get_seconds
()
settings
=
get_settings
()
if
'diff_server_time'
in
settings
and
settings
[
"diff_server_time"
]:
lima_menit
=
int
(
settings
[
"diff_server_time"
])
if
not
request
.
devel
and
abs
(
now
-
time_stamp
)
>
lima_menit
:
log
.
info
(
f
"req time {time_stamp} server time {now}"
)
raise
JsonRpcInvalidTimeError
return
time_stamp
def
get_jsonrpc
(
method
,
params
):
return
dict
(
jsonrpc
=
'2.0'
,
method
=
method
,
params
=
params
,
id
=
int
(
get_random_number
(
6
)))
def
send_rpc
(
auth
,
message
):
"""
Digunakan untuk mengirim data dengan methode JSONRPC 2.0 with os-auth
:param auth: Dict
{"user": user,
"url": url,
"key": key,
"method": method,
"timeout": optional,
}
:param message: Dict
:return: Dict
"""
userid
=
auth
[
'user'
]
password
=
auth
[
'key'
]
url
=
auth
[
'url'
]
headers
=
json_rpc_header
(
userid
,
password
)
params
=
dict
(
data
=
message
)
data
=
get_jsonrpc
(
auth
[
"method"
],
params
)
timeout
=
'timeout'
in
auth
and
int
(
auth
[
'timeout'
])
or
5
log
.
info
(
"URL:{} timeout:{} detik"
.
format
(
url
,
timeout
))
log
.
warning
(
"REQUEST {}"
.
format
(
data
))
try
:
results
=
requests
.
post
(
url
,
json
=
data
,
headers
=
headers
,
timeout
=
timeout
)
# data=jsondata,
except
Exception
as
e
:
log
.
warning
(
str
(
e
))
return
if
results
.
status_code
!=
200
:
log
.
info
(
results
)
log
.
info
(
results
.
text
)
return
rows
=
results
.
text
and
json
.
loads
(
results
.
text
)
or
None
log
.
info
(
"RESPONSE {}"
.
format
(
rows
))
return
rows
##############################
##############################
# Web Service Authentication #
# Web Service Authentication #
##############################
##############################
...
...
opensipkd/tools/pbb.py
View file @
cd2cc95
...
@@ -3,6 +3,8 @@
...
@@ -3,6 +3,8 @@
# from types import (
# from types import (
# StringType,
# StringType,
# UnicodeType)
# UnicodeType)
from
sqlalchemy
import
func
from
..tools
import
FixLength
,
get_settings
from
..tools
import
FixLength
,
get_settings
# from models import pbbDBSession
# from models import pbbDBSession
# from models.ref import TempatPembayaran
# from models.ref import TempatPembayaran
...
@@ -223,7 +225,8 @@ def hitung_denda(piutang_pokok, jatuh_tempo, tanggal=None, max_denda=24):
...
@@ -223,7 +225,8 @@ def hitung_denda(piutang_pokok, jatuh_tempo, tanggal=None, max_denda=24):
if
not
tanggal
:
if
not
tanggal
:
tanggal
=
datetime
.
now
()
.
date
()
tanggal
=
datetime
.
now
()
.
date
()
if
ensure_datetime
(
tanggal
)
<
ensure_datetime
(
jatuh_tempo
):
# + timedelta(days=1):
if
ensure_datetime
(
tanggal
)
<
ensure_datetime
(
jatuh_tempo
):
# + timedelta(days=1):
return
0
return
0
kini
=
datetime
.
now
()
kini
=
datetime
.
now
()
...
@@ -243,7 +246,8 @@ def hitung_denda(piutang_pokok, jatuh_tempo, tanggal=None, max_denda=24):
...
@@ -243,7 +246,8 @@ def hitung_denda(piutang_pokok, jatuh_tempo, tanggal=None, max_denda=24):
def
nop_formatted
(
row
):
def
nop_formatted
(
row
):
if
type
(
row
)
==
str
:
if
type
(
row
)
==
str
:
row
=
FixNop
(
row
)
row
=
FixNop
(
row
)
return
"
%
s.
%
s-
%
s.
%
s-
%
s.
%
s.
%
s"
%
(
row
.
kd_propinsi
,
row
.
kd_dati2
,
row
.
kd_kecamatan
,
return
"
%
s.
%
s-
%
s.
%
s-
%
s.
%
s.
%
s"
%
(
row
.
kd_propinsi
,
row
.
kd_dati2
,
row
.
kd_kecamatan
,
row
.
kd_kelurahan
,
row
.
kd_blok
,
row
.
no_urut
,
row
.
kd_jns_op
)
row
.
kd_kelurahan
,
row
.
kd_blok
,
row
.
no_urut
,
row
.
kd_jns_op
)
...
@@ -251,10 +255,15 @@ def nop_to_id(row):
...
@@ -251,10 +255,15 @@ def nop_to_id(row):
if
type
(
row
)
in
(
StringType
,
UnicodeType
):
if
type
(
row
)
in
(
StringType
,
UnicodeType
):
row
=
FixNop
(
row
)
row
=
FixNop
(
row
)
return
"
%
s
%
s
%
s
%
s
%
s
%
s
%
s"
%
(
row
.
kd_propinsi
,
row
.
kd_dati2
,
row
.
kd_kecamatan
,
return
"
%
s
%
s
%
s
%
s
%
s
%
s
%
s"
%
(
row
.
kd_propinsi
,
row
.
kd_dati2
,
row
.
kd_kecamatan
,
row
.
kd_kelurahan
,
row
.
kd_blok
,
row
.
no_urut
,
row
.
kd_jns_op
)
row
.
kd_kelurahan
,
row
.
kd_blok
,
row
.
no_urut
,
row
.
kd_jns_op
)
def
query_nop
(
table
):
return
func
.
concat
(
table
.
kd_propinsi
,
'.'
,
table
.
kd_dati2
,
'-'
,
table
.
kd_kecamatan
,
'.'
,
table
.
kd_kelurahan
,
'-'
,
table
.
kd_blok
,
'.'
,
table
.
no_urut
,
'-'
,
table
.
kd_jns_op
)
# JEnis REstitusi Kompensasi
# JEnis REstitusi Kompensasi
...
@@ -376,6 +385,7 @@ def filter_nop(qry, nop, tahun=None):
...
@@ -376,6 +385,7 @@ def filter_nop(qry, nop, tahun=None):
qry
=
qry
.
filter_by
(
thn_pajak_sppt
=
tahun
)
qry
=
qry
.
filter_by
(
thn_pajak_sppt
=
tahun
)
return
qry
return
qry
# def filter_nop(qry, param, cls=None):
# def filter_nop(qry, param, cls=None):
# nop = FixNop(param)
# nop = FixNop(param)
# if cls:
# if cls:
...
...
opensipkd/tools/report.py
View file @
cd2cc95
...
@@ -4,23 +4,21 @@ import ntpath
...
@@ -4,23 +4,21 @@ import ntpath
import
csv
import
csv
import
io
import
io
from
datetime
import
datetime
from
datetime
import
datetime
#from z3c.rml import rml2pdf
#
from z3c.rml import rml2pdf
import
subprocess
import
subprocess
import
logging
import
logging
from
..tools
import
get_settings
,
get_params
from
..tools
import
get_settings
,
get_params
log
=
logging
.
getLogger
(
__name__
)
log
=
logging
.
getLogger
(
__name__
)
rpt_path
=
""
def
get_root_path
():
#
def get_root_path():
_here
=
os
.
path
.
dirname
(
__file__
)
#
_here = os.path.dirname(__file__)
return
_here
#
return _here
def
get_logo
():
def
get_logo
(
base_path
=
""
):
path
=
get_params
(
'report_img'
,
os
.
path
.
join
(
os
.
path
.
dirname
(
get_root_path
()),
'static/img'
))
path
=
get_params
(
'report_img'
,
os
.
path
.
join
(
base_path
,
'static/img'
))
# FIXME opensipkd/static to be changed by active app folder
return
path
+
"/logo.png"
,
path
+
"/line.png"
return
path
+
"/logo.png"
,
path
+
"/line.png"
...
@@ -29,7 +27,7 @@ def waktu():
...
@@ -29,7 +27,7 @@ def waktu():
def
open_rml_row
(
row_tpl_filename
):
def
open_rml_row
(
row_tpl_filename
):
f
=
open
(
r
pt_path
+
r
ow_tpl_filename
)
f
=
open
(
row_tpl_filename
)
row_rml
=
f
.
read
()
row_rml
=
f
.
read
()
f
.
close
()
f
.
close
()
return
row_rml
return
row_rml
...
@@ -40,11 +38,11 @@ def open_rml_pdf(tpl_filename, **kwargs):
...
@@ -40,11 +38,11 @@ def open_rml_pdf(tpl_filename, **kwargs):
# log.warning("Not Supported")
# log.warning("Not Supported")
# return
# return
pdf_filename
=
tpl_filename
+
'.pdf'
pdf_filename
=
tpl_filename
+
'.pdf'
f
=
open
(
tpl_filename
)
f
=
open
(
rpt_path
+
tpl_filename
)
rml
=
f
.
read
()
rml
=
f
.
read
()
f
.
close
()
f
.
close
()
logo
,
line
=
get_logo
()
# path + "/img/logo.png"
base_path
=
kwargs
.
get
(
'base_path'
)
logo
,
line
=
get_logo
(
base_path
)
params
=
{}
params
=
{}
for
key
,
value
in
kwargs
.
items
():
for
key
,
value
in
kwargs
.
items
():
...
@@ -72,7 +70,7 @@ def openfile_response(request, filepath):
...
@@ -72,7 +70,7 @@ def openfile_response(request, filepath):
return
response
return
response
def
file_response
(
request
,
f
,
filenam
e
,
filetype
=
None
):
def
file_response
(
request
,
f
=
None
,
filename
=
Non
e
,
filetype
=
None
):
"""
"""
:param request:
:param request:
:param f: object file
:param f: object file
...
@@ -92,7 +90,7 @@ def file_response(request, f, filename, filetype=None):
...
@@ -92,7 +90,7 @@ def file_response(request, f, filename, filetype=None):
filetype
=
''
.
join
(
t
[
-
1
:])
filetype
=
''
.
join
(
t
[
-
1
:])
response
=
request
.
response
response
=
request
.
response
response
.
content_type
=
"application/"
+
filetype
response
.
content_type
=
f
"application/{filetype}"
response
.
content_disposition
=
'filename='
+
fname
response
.
content_disposition
=
'filename='
+
fname
if
filetype
==
"txt"
:
if
filetype
==
"txt"
:
response
.
charset
=
"UTF-8"
response
.
charset
=
"UTF-8"
...
@@ -106,7 +104,6 @@ def set_response(request, filename):
...
@@ -106,7 +104,6 @@ def set_response(request, filename):
ext
=
'image/'
+
ext
ext
=
'image/'
+
ext
elif
ext
in
[
'pdf'
]:
elif
ext
in
[
'pdf'
]:
ext
=
'application/'
+
ext
ext
=
'application/'
+
ext
with
open
(
filename
,
'rb'
)
as
f
:
with
open
(
filename
,
'rb'
)
as
f
:
return
file_response
(
request
,
f
,
filename
,
ext
)
return
file_response
(
request
,
f
,
filename
,
ext
)
...
...
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