-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
108 lines (87 loc) · 2.78 KB
/
logic.py
File metadata and controls
108 lines (87 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import zipfile
import os
import uuid
import csv
from bs4 import BeautifulSoup
from django.template.loader import render_to_string
from django.utils import translation
from django.conf import settings
from core import files, models as core_models
def html_table_to_csv(html):
filepath = files.get_temp_file_path_from_name(
'{0}.csv'.format(uuid.uuid4())
)
soup = BeautifulSoup(str(html), 'lxml')
with open(filepath, "w", encoding="utf-8") as f:
wr = csv.writer(f)
for table in soup.find_all("table"):
for row in table.find_all("tr"):
cells = [cell.string for cell in row.findChildren(['th', 'td'])]
wr.writerow(cells)
wr.writerow([])
f.close()
return filepath
def export_csv(request, article, article_files):
elements = [
'general.html',
'authors.html',
'files.html',
'dates.html',
'funding.html',
]
context = {
'article': article,
'journal': request.journal,
'files': article_files,
}
html = ''
for element in elements:
html = html + render_to_string(
'export/elements/{element}'.format(element=element),
context,
)
csv_file_path = html_table_to_csv(html)
zip_file_name = 'export_{}_{}_csv.zip'.format(article.journal.code, article.pk)
zip_path = os.path.join(files.TEMP_DIR, zip_file_name)
zip_file = zipfile.ZipFile(zip_path, mode='w')
zip_file.write(
csv_file_path,
'article_data.csv'
)
for file in article_files:
zip_file.write(
file.self_article_path(),
file.original_filename,
)
zip_file.close()
return files.serve_temp_file(zip_path, zip_file_name)
def export_html(request, article, article_files):
with translation.override(settings.LANGUAGE_CODE):
html_file_path = files.create_temp_file(
render_to_string(
'export/export.html',
context={
'article': article,
'journal': request.journal,
'files': article_files,
}
),
'{code}-{pk}.html'.format(
code=article.journal.code,
pk=article.pk,
)
)
zip_file_name = 'export_{}_{}_html.zip'.format(article.journal.code, article.pk)
zip_path = os.path.join(files.TEMP_DIR, zip_file_name)
zip_file = zipfile.ZipFile(zip_path, mode='w')
zip_file.write(
html_file_path,
'article_data.html'
)
for file in article_files:
zip_file.write(
file.self_article_path(),
file.original_filename,
)
zip_file.close()
return files.serve_temp_file(zip_path, zip_file_name)