Skip to content

Commit

Permalink
Merge branch 'save_geom_only_if_updated'
Browse files Browse the repository at this point in the history
  • Loading branch information
Anaethelion committed Jul 31, 2015
2 parents 9296612 + cbdc69b commit 550744f
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 61 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,13 @@ after_success:
- if [[ $ACTION == test ]]; then ./bin/coverage report -m; fi
- if [[ $ACTION == test ]]; then sudo pip install coveralls; fi
- if [[ $ACTION == test ]]; then coveralls; fi

after_failure:
- cat install.log
- cat var/log/geotrek.log
- cat var/log/nginx-access.log
- cat var/log/nginx-error.log
- cat var/log/supervisord.log

notifications:
irc: "irc.freenode.org#geotrek"
78 changes: 41 additions & 37 deletions docs/logo.ans

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions geotrek/common/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def parse_row(self, row):
if self.progress_cb:
self.progress_cb(float(self.line) / self.nb, self.line, self.eid_val)

def report(self):
def report(self, output_format='txt'):
context = {
'nb_success': self.nb_success,
'nb_lines': self.line,
Expand All @@ -292,7 +292,7 @@ def report(self):
'nb_unmodified': self.nb_unmodified,
'warnings': self.warnings,
}
return render_to_string('common/parser_report.html', context)
return render_to_string('common/parser_report.{output_format}'.format(output_format=output_format), context)

def get_mapping(self, src, val, mapping, partial):
if partial:
Expand Down
6 changes: 2 additions & 4 deletions geotrek/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def progress_cb(progress, line, eid):
'total': 100,
'filename': filename.split('/').pop(-1),
'parser': class_name,
'report': parser.report().replace('$celery_id', current_task.request.id)
'report': parser.report(output_format='html').replace('$celery_id', current_task.request.id)
}


Expand Down Expand Up @@ -71,12 +71,10 @@ def progress_cb(progress, line, eid):
except Exception as e:
raise e

print(parser.report())

return {
'current': 100,
'total': 100,
'filename': _("Import from web."),
'parser': class_name,
'report': parser.report().replace('$celery_id', current_task.request.id)
'report': parser.report(output_format='html').replace('$celery_id', current_task.request.id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% load i18n %}

<li><a href="{% url 'common:import_dataset' %}">{% trans "Imports" %}</a></li>
17 changes: 0 additions & 17 deletions geotrek/common/templates/common/common_usermenu_fragment.html

This file was deleted.

9 changes: 9 additions & 0 deletions geotrek/common/templates/common/parser_report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% load i18n %}{% blocktrans count n=nb_success %}{{ n }}/{{ nb_lines }} line imported.{% plural %}{{ n }}/{{ nb_lines }} lines imported.{% endblocktrans %}
{% if nb_created %}{% blocktrans count n=nb_created %}{{ n }} record created.{% plural %}{{ n }} records created.{% endblocktrans %}
{% endif %}{% if nb_updated %}{% blocktrans count n=nb_updated %}{{ n }} record updated.{% plural %}{{ n }} records updated.{% endblocktrans %}
{% endif %}{% if nb_deleted %}{% blocktrans count n=nb_deleted %}{{ n }} record deleted.{% plural %}{{ n }} records deleted.{% endblocktrans %}
{% endif %}{% if nb_unmodified %}{% blocktrans count n=nb_unmodified %}{{ n }} record unmodified.{% plural %}{{ n }} records unmodified.{% endblocktrans %}
{% endif %}{% if warnings %}{% blocktrans count n=warnings|length %}{{ n }} warning:{% plural %}{{ n }} warnings:{% endblocktrans %}
{% for id, msgs in warnings.iteritems %}# {{ id }}:
{% for msg in msgs %}- {{ msg|safe }},
{% endfor %}{% endfor %}{% endif %}
15 changes: 15 additions & 0 deletions geotrek/common/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test.utils import override_settings
from django.template.base import TemplateDoesNotExist

from paperclip.models import Attachment

Expand Down Expand Up @@ -72,6 +73,20 @@ def test_updated_with_eid(self):
self.assertEqual(organisms[0].organism, u"Comité Théodule")
self.assertEqual(organisms[1].organism, u"Comité Hippolyte")

def test_report_format_text(self):
parser = OrganismParser()
self.assertRegexpMatches(parser.report(), '0/0 lines imported.')
self.assertNotRegexpMatches(parser.report(), '<div id=\"collapse-\$celery_id\" class=\"collapse\">')

def test_report_format_html(self):
parser = OrganismParser()
self.assertRegexpMatches(parser.report(output_format='html'), '<div id=\"collapse-\$celery_id\" class=\"collapse\">')

def test_report_format_bad(self):
parser = OrganismParser()
with self.assertRaises(TemplateDoesNotExist):
parser.report(output_format='toto')


@override_settings(MEDIA_ROOT=mkdtemp('geotrek_test'))
class AttachmentParserTests(TestCase):
Expand Down
1 change: 0 additions & 1 deletion geotrek/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ def import_view(request):
choices, request.POST, request.FILES, prefix="with-file")

if form.is_valid():
print(request.FILES)
uploaded = request.FILES['with-file-zipfile']

destination_dir, destination_file = create_tmp_destination(
Expand Down
14 changes: 14 additions & 0 deletions geotrek/trekking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ def distance(self, to_cls):
def is_public(self):
return self.any_published or (self.parent and self.parent.any_published)

def save(self, *args, **kwargs):
if self.pk is not None and kwargs.get('update_fields', None) is None:
field_names = set()
for field in self._meta.concrete_fields:
if not field.primary_key and not hasattr(field, 'through'):
field_names.add(field.attname)
old_trek = Trek.objects.get(pk=self.pk)
if self.geom is not None and old_trek.geom.equals_exact(self.geom, tolerance=0.00001):
field_names.remove('geom')
if self.geom_3d is not None and old_trek.geom_3d.equals_exact(self.geom_3d, tolerance=0.00001):
field_names.remove('geom_3d')
return super(Trek, self).save(update_fields=field_names, *args, **kwargs)
super(Trek, self).save(*args, **kwargs)

Path.add_property('treks', Trek.path_treks, _(u"Treks"))
Topology.add_property('treks', Trek.topology_treks, _(u"Treks"))
if settings.HIDE_PUBLISHED_TREKS_IN_TOPOLOGIES:
Expand Down
44 changes: 44 additions & 0 deletions geotrek/trekking/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,47 @@ def test_city_departure(self):
(3, 9), (3, 3)))))
self.assertEqual(trek.cities, [city1, city2])
self.assertEqual(trek.city_departure, unicode(city1))


class TrekUpdateGeomTest(TestCase):
def setUp(self):
self.trek = TrekFactory.create(published=True, geom=LineString(((700000, 6600000), (700100, 6600100)), srid=2154))

def tearDown(self):
del (self.trek)

def test_save_with_same_geom(self):
geom = LineString(((700000, 6600000), (700100, 6600100)), srid=2154)
self.trek.geom = geom
self.trek.save()
retrieve_trek = Trek.objects.get(pk=self.trek.pk)
self.assertTrue(retrieve_trek.geom.equals_exact(geom, tolerance=0.00001))

def test_save_with_another_geom(self):
geom = LineString(((-7, -7), (5, -7), (5, 5), (-7, 5), (-7, -7)), srid=2154)
self.trek.geom = geom
self.trek.save()
retrieve_trek = Trek.objects.get(pk=self.trek.pk)
self.assertFalse(retrieve_trek.geom.equals_exact(geom, tolerance=0.00001))

def test_save_with_provided_one_field_exclusion(self):
self.trek.save(update_fields=['geom'])
self.assertTrue(self.trek.pk)

def test_save_with_multiple_fields_exclusion(self):
new_trek = TrekFactory.create()

new_trek.description_en = 'Description Test update'
new_trek.ambiance = 'Very special ambiance, for test purposes.'

new_trek.save(update_fields=['description_en'])
db_trek = Trek.objects.get(pk=new_trek.pk)

self.assertTrue(db_trek.pk)
self.assertEqual(db_trek.description_en, 'Description Test update')
self.assertNotEqual(db_trek.ambiance, 'Very special ambiance, for test purposes.')

new_trek.save(update_fields=['ambiance_en'])
db_trek = Trek.objects.get(pk=new_trek.pk)

self.assertEqual(db_trek.ambiance_en, 'Very special ambiance, for test purposes.')

0 comments on commit 550744f

Please sign in to comment.