Test reverting

This commit is contained in:
Oleg Lavrovsky 2023-07-17 20:16:33 +02:00
parent c32d390a89
commit c7b93b4863
No known key found for this signature in database
GPG Key ID: 31E523030632FF4B
6 changed files with 70 additions and 14 deletions

View File

@ -221,6 +221,18 @@ def post_delete(project_id, activity_id):
return redirect(purl)
def revert_project_by_activity(project, activity):
"""Revert Project to a previous version based on an Activity."""
if not activity.project_version:
return None, 'Could not revert: data not available.'
elif activity.project_version == 0:
return None, 'Could not revert: this is the earliest version.'
# Apply revert
revert_to = activity.project_version
project.versions[revert_to - 1].revert()
return revert_to, 'Project data reverted to version %d.' % revert_to
@blueprint.route('/<int:project_id>/undo/<int:activity_id>', methods=['GET'])
@login_required
def post_revert(project_id, activity_id):
@ -232,15 +244,12 @@ def post_revert(project_id, activity_id):
flash('Could not revert: user not allowed.', 'warning')
return redirect(purl)
activity = Activity.query.filter_by(id=activity_id).first_or_404()
if not activity.project_version:
flash('Could not revert: data not available.', 'warning')
elif activity.project_version == 0:
flash('Could not revert: this is the earliest version.', 'warning')
result, status = revert_project_by_activity(project, activity)
if not result:
flash(status, 'warning')
else:
revert_to = activity.project_version
project.versions[revert_to - 1].revert()
flash('Project data reverted to version %d.' % revert_to, 'success')
return project_view(project.id)
flash(status, 'success')
return project_action(project_id, 'revert', then_redirect=True)
return redirect(purl)

View File

@ -126,8 +126,7 @@
{% endmacro %}
{% macro render_form(url, form, horizontal=False, legend=None, confirm_msg=None, formid=None) %}
{% set idattr = "id=" + formid if formid else "" %}
<form {{idattr}} class="{% if horizontal %}form-horizontal{% endif %}" method="POST" action="{{ url }}"
<form id="{{formid}}" class="{% if horizontal %}form-horizontal{% endif %}" method="POST" action="{{ url }}"
{% if confirm_msg %}onsubmit='return confirm("{{ confirm_msg }}");' {% endif %}
{% if form.multipart %}enctype="multipart/form-data"{% endif %} >
{{ form.hidden_tag() }}

View File

@ -71,9 +71,9 @@
{% endif %}
{% if detail_view %}
{{ render_form(url_for('project.project_details', project_id=project.id), form) }}
{{ render_form(url_for('project.project_details', project_id=project.id), form, formid='projectDetails') }}
{% else %}
{{ render_form(url_for('project.project_edit', project_id=project.id), form) }}
{{ render_form(url_for('project.project_edit', project_id=project.id), form, formid='projectEdit') }}
{% endif %}
<div class="alert-warning text-center mt-4 p-2">

View File

@ -200,6 +200,11 @@ def getActivityByType(a, only_active=True): # noqa: C901
text = a.content
author = None
icon = 'random'
elif a.name == 'revert':
text = "Reverted to"
if a.project_version:
text += " version %d" % a.project_version
icon = 'paperclip'
elif a.name == 'update':
text = "Edited content"
if a.project_version:

View File

@ -6,7 +6,10 @@ secure_scheme_headers = {
'X-FORWARDED-PROTO': 'https',
'X-FORWARDED-SSL': 'on'
}
bind = '0.0.0.0:%s' % str(os_env.get('PORT', 5000))
port = str(os_env.get('PORT', 5000))
bind = ['0.0.0.0:%s' % port, '[::1]:%s' % port]
worker_class = 'gevent'
workers = os_env.get('WORKERS', 2)
errorlog = '-'

View File

@ -7,7 +7,9 @@ from flask import url_for
from dribdat.user.models import User, Project
from .factories import UserFactory
from dribdat.public.project import revert_project_by_activity
from .factories import UserFactory, ProjectFactory, EventFactory
class TestEditing:
@ -67,3 +69,41 @@ class TestEditing:
res = form.submit()
# sees error
assert 'A user with this name already exists' in res
def test_edit_and_revert(self, db, testapp):
"""Test reverting projects."""
user = UserFactory(active=True, is_admin=True)
user.set_password('myprecious')
user.save()
# Login with the user
res = testapp.get('/login/')
form = res.forms['loginForm']
form['username'] = user.username
form['password'] = 'myprecious'
res = form.submit().follow()
assert res.status_code == 200
event = EventFactory()
event.save()
project = ProjectFactory()
project.event = event
project.save()
# A new project was created: edit it
res1 = testapp.get('/project/%d/edit' % project.id)
form1 = res1.forms['projectEdit']
form1['longtext'] = "Hello"
res1 = form1.submit().follow()
assert res1.status_code == 200
# Change the content now
res2 = testapp.get('/project/%d/edit' % project.id)
form2 = res2.forms['projectEdit']
form2['longtext'] = "Fixme"
res2 = form2.submit().follow()
assert res2.status_code == 200
# Get previous activity and revert to it
activity = project.activities[-2]
assert activity.project_version is not None
result, status = revert_project_by_activity(project, activity)
assert result is not None
# Check that we have indeed reverted to original text
project = Project.query.first()
assert 'Hello' == project.longtext