Skip to content

Commit c50bc9d

Browse files
author
Bibek Pandey
authored
Merge pull request #220 from the-deep/hotfix-xlsx-escape-1.2.4
[Hotfix] Don't escape character for xlsx
2 parents 9d7e9ec + f48c6d6 commit c50bc9d

File tree

5 files changed

+66
-38
lines changed

5 files changed

+66
-38
lines changed

deep/settings.py

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,8 @@
3939

4040
# Application definition
4141

42-
INSTALLED_APPS = [
43-
# DJANGO APPS
44-
'django.contrib.admin',
45-
'django.contrib.auth',
46-
'django.contrib.contenttypes',
47-
'django.contrib.messages',
48-
'django.contrib.sessions',
49-
'django.contrib.staticfiles',
50-
'django.contrib.gis',
51-
'django.contrib.postgres',
52-
53-
# LIBRARIES
54-
'autofixture',
55-
'channels',
56-
'corsheaders',
57-
'crispy_forms',
58-
'django_filters',
59-
'djangorestframework_camel_case',
60-
'drf_dynamic_fields',
61-
'rest_framework',
62-
'reversion',
63-
'storages',
64-
'django_premailer',
65-
'raven.contrib.django.raven_compat',
66-
67-
# DEEP APPS
42+
DEEP_APPS = [
43+
# DEEP CORE APPS
6844
'analysis_framework',
6945
'ary',
7046
'category_editor',
@@ -93,6 +69,32 @@
9369
'redis_store',
9470
]
9571

72+
INSTALLED_APPS = [
73+
# DJANGO APPS
74+
'django.contrib.admin',
75+
'django.contrib.auth',
76+
'django.contrib.contenttypes',
77+
'django.contrib.messages',
78+
'django.contrib.sessions',
79+
'django.contrib.staticfiles',
80+
'django.contrib.gis',
81+
'django.contrib.postgres',
82+
83+
# LIBRARIES
84+
'autofixture',
85+
'channels',
86+
'corsheaders',
87+
'crispy_forms',
88+
'django_filters',
89+
'djangorestframework_camel_case',
90+
'drf_dynamic_fields',
91+
'rest_framework',
92+
'reversion',
93+
'storages',
94+
'django_premailer',
95+
'raven.contrib.django.raven_compat',
96+
] + DEEP_APPS
97+
9698
MIDDLEWARE = [
9799
'django.middleware.security.SecurityMiddleware',
98100
'django.contrib.sessions.middleware.SessionMiddleware',
@@ -294,6 +296,7 @@ def add_username_attribute(record):
294296
"""
295297
Append username(email) to logs
296298
"""
299+
record.username = ''
297300
if hasattr(record, 'request'):
298301
if hasattr(record.request, 'user') and\
299302
not record.request.user.is_anonymous():
@@ -315,9 +318,11 @@ def add_username_attribute(record):
315318
},
316319
'formatters': {
317320
'simple': {
318-
'format': '%(asctime)s ' + os.environ.get('EBS_HOSTNAME', '') +
319-
' DJANGO-' + os.environ.get('EBS_ENV_TYPE', '') +
320-
': %(username)s %(message)s',
321+
'format':
322+
'%(asctime)s {} DJANGO-{} : %(username)s %(message)s'.format(
323+
os.environ.get('EBS_HOSTNAME', ''),
324+
os.environ.get('EBS_ENV_TYPE', '')
325+
),
321326
'datefmt': '%Y-%m-%dT%H:%M:%S',
322327
},
323328
},
@@ -327,8 +332,10 @@ def add_username_attribute(record):
327332
'class': 'logging.handlers.SysLogHandler',
328333
'filters': ['add_username_attribute'],
329334
'formatter': 'simple',
330-
'address': (os.environ.get('PAPERTRAIL_HOST'),
331-
int(os.environ.get('PAPERTRAIL_PORT')))
335+
'address': (
336+
os.environ.get('PAPERTRAIL_HOST'),
337+
int(os.environ.get('PAPERTRAIL_PORT')),
338+
)
332339
},
333340
},
334341
'loggers': {
@@ -344,6 +351,16 @@ def add_username_attribute(record):
344351
'handlers': ['SysLog'],
345352
'propagate': True,
346353
},
354+
**{
355+
# TODO: move all apps to sub directory deep
356+
# and use deep as logger
357+
app: {
358+
'level': 'DEBUG',
359+
'handlers': ['SysLog'],
360+
'propagate': True,
361+
}
362+
for app in DEEP_APPS
363+
}
347364
},
348365
}
349366

export/formats/xlsx.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
from openpyxl.writer.excel import save_virtual_workbook
55

66
from utils.common import (
7-
get_valid_xml_string as xstr,
7+
get_valid_xml_string,
88
parse_date,
99
parse_time,
1010
parse_number,
1111
)
1212

1313

14+
def xstr(string):
15+
return get_valid_xml_string(string, escape=False)
16+
17+
1418
class WorkBook:
1519
"""
1620
An xlsx workbook

utils/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from xml.sax.saxutils import escape
1+
from xml.sax.saxutils import escape as xml_escape
22
from datetime import timedelta, datetime
33
from django.conf import settings
44

@@ -63,9 +63,9 @@ def is_valid_xml_char_ordinal(c):
6363
)
6464

6565

66-
def get_valid_xml_string(string):
66+
def get_valid_xml_string(string, escape=True):
6767
if string:
68-
s = escape(string)
68+
s = xml_escape(string) if escape else string
6969
return ''.join(c for c in s
7070
if is_valid_xml_char_ordinal(c))
7171
return ''

utils/extractor/formats/html.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from urllib.parse import urljoin
33

44
import logging
5+
import base64
56
import traceback
67
import re
78
import requests
@@ -44,9 +45,15 @@ def process(doc, url):
4445
try:
4546
fp = tempfile.NamedTemporaryFile(dir=settings.BASE_DIR)
4647
img_src = urljoin(url, img.get('src'))
47-
r = requests.get(img_src, stream=True)
48-
write_file(r, fp)
48+
if re.search(r'http[s]?://', img_src):
49+
r = requests.get(img_src, verify=False, stream=True)
50+
write_file(r, fp)
51+
else:
52+
image = base64.b64decode(img_src.split(',')[1])
53+
fp.write(image)
4954
images.append(fp)
55+
except requests.exceptions.ConnectionError:
56+
logger.warn(traceback.format_exc())
5057
except Exception:
5158
logger.error(traceback.format_exc())
5259

utils/extractor/web_document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, url):
3131
r = requests.head(url, headers=DEFAULT_HEADERS)
3232
except requests.exceptions.RequestException:
3333
# If we can't get header, assume html and try to continue.
34-
r = requests.get(url, headers=DEFAULT_HEADERS)
34+
r = requests.get(url, verify=False, headers=DEFAULT_HEADERS)
3535
doc = r.content
3636
super().__init__(doc, type, params=params)
3737
return

0 commit comments

Comments
 (0)