Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement project cloning; fixes #689 #796

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
65 changes: 65 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,67 @@ 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)

for att in old_project.__dict__:
if (att[0] != '_' and att not in ['area_id', 'author_id',
'created', 'done', 'id',
'invalidated', 'last_update',
'priority_areas', 'status',
'tasks', 'validated']):
setattr(new_project, att, getattr(old_project, att))
Copy link
Collaborator Author

@ethan-nelson ethan-nelson May 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole for section feels a bit hacky, but the only other solution I could come up with is supply a list of attributes to choose as opposed to ignore.


if new_project.zoom is not None:
new_project.auto_fill(new_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

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']:
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