Skip to content

Commit

Permalink
Implement project cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-nelson committed May 10, 2016
1 parent d3112e5 commit b8e6309
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
1 change: 1 addition & 0 deletions osmtm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def main(global_config, **settings):
config.add_route('project_new_grid', '/project/new/grid')
config.add_route('project_new_arbitrary', '/project/new/arbitrary')
config.add_route('project_grid_simulate', '/project/grid_simulate')
config.add_route('project_clone', '/project_clone')
config.add_route('project_json', '/project/{project:\d+}.json')
config.add_route('project', '/project/{project:\d+}')
config.add_route('project_edit', '/project/{project:\d+}/edit')
Expand Down
2 changes: 1 addition & 1 deletion osmtm/static/js/project.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ osmtm.project_new = (function() {
});
});

$('#gridform, #arbitraryform').submit(function() {
$('#gridform, #arbitraryform, #cloneform').submit(function() {
window.setTimeout(function() {
$('input[type=submit]').attr('disabled', 'disabled');
$('.loading').removeClass('hidden');
Expand Down
40 changes: 40 additions & 0 deletions osmtm/templates/project.new.mako
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,46 @@
</span>
</form>
</p>
<p>
${_('or')}
</p>
<p>
<%
link = '<a id="clone" class="btn btn-default" data-toggle="modal" data-target="#cloneModal">%s</a>' % (_('Clone'),)
text = _('${clone_link} an already existing project.', mapping={'clone_link': link})
%>
${text|n}
</p>
</div>
<div class="modal fade" id="cloneModal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<form id="cloneform" method="post" action="${request.route_url('project_clone')}">
<div class="modal-body">
<p>
${_('This will clone a project that currently exists. The original project area and tiling will be used.')}
</p>
<p class="form-group">
<input name="project" class="input form-control" placeholder="${_('Project #')}"/>
</p>
</div>
<div class="modal-footer">
<div class="form-actions pull-right">
<a class="btn btn-default" data-dismiss="modal">
${_('Cancel')}
</a>
<input type="submit" value=${_('Clone project')}"
class="btn btn-success"/>
</div>
<div class="clearfix"></div>
<div class="clearfix"></div>
<div class="pull-right loading help hidden">
${_('Cloning project, please wait...')}
</div>
</div>
</form>
</div>
</div>
</div>
</%block>
Expand Down
86 changes: 86 additions & 0 deletions osmtm/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
Feature,
)

from json import (
loads as _loads,
)

import datetime
import itertools

Expand Down Expand Up @@ -226,6 +230,88 @@ def project_grid_simulate(request):
return FeatureCollection([Feature(geometry=shape.to_shape(geometry))])


@view_config(route_name='project_clone', permission="project_edit")
def project_clone(request):
_ = request.translate
user_id = authenticated_userid(request)
user = DBSession.query(User).get(user_id)

try:
id = request.params['project']
old_project = DBSession.query(Project).get(id)

new_project = Project(old_project.name, user=user)
new_project.area = old_project.area

if old_project.zoom is not None:
new_project.auto_fill(old_project.zoom)
else:
tasks = []
for task in old_project.tasks:
tasks.append(Task(None, None, None, 'SRID=4326;%S' %
task.geometry, _loads(task.extra_properties)))
new_project.tasks = tasks
new_project.area = old_project.area

if old_project.license_id:
new_project.license_id = old_project.license_id

if old_project.imagery:
new_project.imagery = old_project.imagery

new_project.priority = old_project.priority

if old_project.entities_to_map:
new_project.entities_to_map = old_project.entities_to_map

if old_project.changeset_comment:
new_project.changeset_comment = old_project.changeset_comment

new_project.private = old_project.private

if old_project.allowed_users:
new_project.allowed_users = old_project.allowed_users

if old_project.josm_preset:
new_project.josm_preset = old_project.josm_preset

if old_project.due_date:
new_project.due_date = old_project.due_date

if old_project.priority_areas:
new_project.priority_areas = []
for feature in old_project.priority_areas:
new_project.priority_areas.append(
PriorityArea(feature.geometry))

for locale, translation in old_project.translations.iteritems():
with old_project.force_locale(locale) and \
new_project.force_locale(locale):
for field in ['name', 'short_description', 'description',
'instructions', 'per_task_instructions']:
print dir(translation)
if hasattr(old_project, field):
setattr(new_project, field,
getattr(translation, field))

DBSession.add(new_project)
DBSession.flush()

msg = _("""Project #${new_project_id} cloned successfully from Project
#${old_project_id}""", mapping={'new_project_id':
new_project.id,
'old_project_id': id})
request.session.flash(msg, 'alert')

return HTTPFound(location=route_path('project_edit', request,
project=new_project.id))

except Exception, e:
msg = _("Sorry, could not create the project. <br />%s") % e.message
request.session.flash(msg, 'alert')
return HTTPFound(location=route_path('project_new', request))


@view_config(route_name='project_edit', renderer='project.edit.mako',
permission="project_edit")
def project_edit(request):
Expand Down

0 comments on commit b8e6309

Please sign in to comment.