Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
hub
ViralHostRangeDB
Commits
71856b08
Commit
71856b08
authored
Oct 01, 2019
by
Bryan BRANCOTTE
Browse files
Adding color for the export function, related to
#48
,
#78
and
#16
parent
8e29f209
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/viralhostrange/requirements.txt
View file @
71856b08
...
...
@@ -4,6 +4,7 @@ psycopg2-binary
mod_wsgi
django-crontab
pandas<=0.25
jinja2
openpyxl
xlrd
django-crispy-forms
...
...
src/viralhostrange/viralhostrangedb/views.py
View file @
71856b08
...
...
@@ -11,6 +11,7 @@ from django.contrib import messages
from
django.contrib.auth.decorators
import
login_required
from
django.contrib.auth.mixins
import
LoginRequiredMixin
from
django.contrib.staticfiles.templatetags.staticfiles
import
static
from
django.core.cache
import
cache
from
django.core.files
import
storage
from
django.core.files.temp
import
NamedTemporaryFile
from
django.core.mail
import
EmailMultiAlternatives
...
...
@@ -657,6 +658,22 @@ def custom_css(request, slug):
return
HttpResponse
(
css
,
content_type
=
"text/css"
)
def
panda_color_mapping
(
v
):
key
=
'html_color_%s'
%
str
(
v
)
color
=
cache
.
get
(
key
)
if
color
is
not
None
:
return
color
if
v
is
not
None
and
(
isinstance
(
v
,
float
)
or
isinstance
(
v
,
int
)):
try
:
color
=
'background-color:'
+
models
.
GlobalViralHostResponseValue
.
get_html_color_for
(
v
)
cache
.
set
(
key
,
color
,
60
)
return
color
except
Exception
:
pass
cache
.
set
(
key
,
''
,
60
)
return
''
def
download_responses
(
request
):
form
=
forms
.
BrowseForm
(
user
=
request
.
user
,
...
...
@@ -746,11 +763,10 @@ def download_responses(request):
with
NamedTemporaryFile
(
suffix
=
".xlsx"
)
as
f
:
# write in a temp file
with
pd
.
ExcelWriter
(
f
.
name
)
as
writer
:
pd
.
DataFrame
(
data
,
index
=
row_header
,
columns
=
col_header
,
)
\
.
to_excel
(
writer
)
df_data
=
pd
.
DataFrame
(
data
,
columns
=
col_header
,
index
=
row_header
)
df_data
=
df_data
.
style
.
applymap
(
func
=
panda_color_mapping
)
df_data
.
to_excel
(
writer
)
df_legend
.
to_excel
(
writer
,
startcol
=
0
,
startrow
=
len
(
row_header
)
+
3
)
if
len
(
options
)
>
0
:
df_options
.
to_excel
(
writer
,
startcol
=
0
,
startrow
=
len
(
row_header
)
+
3
+
len
(
legend_name
)
+
3
,
index
=
None
)
...
...
@@ -816,10 +832,10 @@ def data_source_download(request, pk):
index
=
row_header
,
columns
=
col_header
)
\
.
to_excel
(
writer
,
sheet_name
=
'raw_responses'
)
pd
.
DataFrame
(
data_mapped
,
index
=
row_header
,
columns
=
col_header
)
\
.
to_excel
(
writer
,
sheet_name
=
'mapped_responses'
)
df_data
=
pd
.
DataFrame
(
data_mapped
,
columns
=
col_header
,
index
=
row_header
)
df_data
=
df_data
.
style
.
applymap
(
func
=
panda_color_mapping
)
df_data
.
to_excel
(
writer
,
sheet_name
=
'mapped_responses'
)
# then read the temp file and send it to the client
with
open
(
f
.
name
,
"rb"
)
as
excel
:
response
=
HttpResponse
(
excel
.
read
(),
...
...
@@ -924,7 +940,6 @@ def host_or_virus_download(request, is_host, instance):
data
[
0
][
col_pos
[
int
(
pk
)]]
=
value
[
'ratio'
]
*
value
[
'total'
]
*
100
/
actual_total
# Prepare the legend
mapping
=
models
.
GlobalViralHostResponseValue
.
objects_mappable
().
order_by
(
'value'
)
legend_name
=
[]
legend_value
=
[]
legend
=
[]
...
...
@@ -951,11 +966,11 @@ def host_or_virus_download(request, is_host, instance):
# write in a temp file
with
pd
.
ExcelWriter
(
f
.
name
)
as
writer
:
df_title
.
to_excel
(
writer
)
pd
.
DataFrame
(
data
,
index
=
row_header
,
columns
=
col_header
,
)
\
.
to_excel
(
writer
,
startcol
=
0
,
startrow
=
2
)
df_data
=
pd
.
DataFrame
(
data
,
columns
=
col_header
,
index
=
row_header
)
df_data
=
df_data
.
style
.
applymap
(
func
=
panda_color_mapping
)
df_data
.
to_excel
(
writer
,
startcol
=
0
,
startrow
=
2
)
df_legend
.
to_excel
(
writer
,
startcol
=
0
,
startrow
=
len
(
row_header
)
+
3
+
2
)
if
len
(
options
)
>
0
:
df_options
.
to_excel
(
writer
,
startcol
=
0
,
startrow
=
len
(
row_header
)
+
3
+
len
(
legend_name
)
+
3
+
2
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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