Skip to content

Commit

Permalink
added delete empty line for validation files
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsarik committed Nov 22, 2017
1 parent 1a6cd9a commit dfc97dc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
90 changes: 60 additions & 30 deletions src/customers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4404,18 +4404,18 @@ def copy_file_kml(old_path, new_path):
doc = ''

try:
with open(old_path) as f:
doc = parser.parse(f).getroot()

error = validation_kml(doc, old_path)

print '!!!!!!!!!!!!!!! COPY DOC ERROR =================== ', error
# print '!!!!!!!!!!!!!!! COPY DOC ERROR =================== ', error

if error:
if os.path.exists(old_path):
os.remove(old_path)
return doc, error

with open(old_path) as f:
doc = parser.parse(f).getroot()

command_line = 'cp {0} {1}'.format(old_path, new_path)
proc = Popen(command_line, shell=True)
proc.wait()
Expand Down Expand Up @@ -4444,38 +4444,68 @@ def get_data_kml(path):
return doc, error


def validation_kml(kml_name, kml_path):
error_msg = ''
file_name = kml_path.split('/')[-1]
file_size = os.path.getsize(kml_path)
xml = html.parse(kml_path)
def delete_empty_lines(file_path):
# print '!!!!!!!!!!!!!!!!!! delete_empty_lines =========================== ', file_path

error = ''

try:
string = open(file_path).readlines()
os.remove(file_path)
new_file_kml = open(file_path, 'w+')

xml_extendeddata = len(xml.xpath("//extendeddata")) / 2
xml_coordinates = len(xml.xpath("//coordinates")) / 2
xml_point = len(xml.xpath("//point")) / 2
xml_polygon = len(xml.xpath("//polygon")) / 2
xml_placemark = len(xml.xpath("//placemark")) / 2
for i in string:
new_line = i

if file_size >= 10000000:
error_msg = 'Error!! An error occurred while loading the file "{0}". \
The file size is more than 10Mb'.format(file_name)
return error_msg
if not i.isspace():
new_file_kml.write(i)

if xml_extendeddata >= 1000 or xml_coordinates >= 1000 \
or xml_point >= 1000 or xml_polygon >= 1000 or xml_placemark >= 1000:
new_file_kml.close()
except Exception, e:
error = e

error_msg = 'Error!! An error occurred while loading the file "{0}". \
The file has more than 1000 objects'.format(file_name)
return error_msg
return error

# try:
# schema_ogc = Schema("ogckml22.xsd")
# schema_gx = Schema("kml22gx.xsd")

# schema_ogc.assertValid(kml_name)
# schema_gx.assertValid(kml_name)
# except Exception, e:
# return str(e)
def validation_kml(kml_name, kml_path):
error_msg = ''
file_name = kml_path.split('/')[-1]
file_size = os.path.getsize(kml_path)

updated_file = delete_empty_lines(kml_path)

try:
xml = html.parse(kml_path)

xml_extendeddata = len(xml.xpath("//extendeddata")) / 2
xml_coordinates = len(xml.xpath("//coordinates")) / 2
xml_point = len(xml.xpath("//point")) / 2
xml_polygon = len(xml.xpath("//polygon")) / 2
xml_placemark = len(xml.xpath("//placemark")) / 2

if file_size >= 10000000:
error_msg = 'Error!! An error occurred while loading the file "{0}". \
The file size is more than 10Mb'.format(file_name)
return error_msg

if xml_extendeddata >= 1000 or xml_coordinates >= 1000 \
or xml_point >= 1000 or xml_polygon >= 1000 or xml_placemark >= 1000:

error_msg = 'Error!! An error occurred while loading the file "{0}". \
The file has more than 1000 objects'.format(file_name)
return error_msg

# try:
# schema_ogc = Schema("ogckml22.xsd")
# schema_gx = Schema("kml22gx.xsd")

# schema_ogc.assertValid(kml_name)
# schema_gx.assertValid(kml_name)
# except Exception, e:
# return str(e)
except Exception, e:
print '!!!!!!!!!!!!!!!!!! ERROR VALIDATION KML =========================== ', e
return e

return error_msg

Expand Down
2 changes: 1 addition & 1 deletion src/templates/customers/files_lister.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<label for="id_test_data" class="margin-right-10">
<img src="{% static 'img/icons8-upload-to-ftp.png' %}" />
</label>
{{ form.test_data }} <span class="file-selected margin-right-10" id="file-selected">No file selected.</span>
{{ form.test_data }} <span class="file-selected margin-right-10" id="file-selected"> No file selected </span>

<button type="submit" id="load" name="load_button" disabled="disabled" class="btn btn-primary btn-sm">Load</button>

Expand Down

0 comments on commit dfc97dc

Please sign in to comment.