Skip to content

Commit

Permalink
Add route for updating map info
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimmons87 committed Sep 4, 2023
1 parent 403f807 commit 8d03788
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 8 deletions.
2 changes: 1 addition & 1 deletion openra/templates/displayMap.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h5>Description:</h5>
<div class="popup-content-wrapper">
<div class="closePopup"><label class="x"></label></div>
<h3>Edit map info</h3>
<form method="POST" action="/maps/{{map.id}}">{% csrf_token %}
<form method="POST" action="/maps/{{map.id}}/update-map-info">{% csrf_token %}
<div><textarea name="mapInfo" placeholder="Map info">{{map.info}}</textarea></div>
<div><button type="submit">Submit</button></div>
</form>
Expand Down
153 changes: 153 additions & 0 deletions openra/tests/routes/test_route_maps_update_map_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
from django.test import Client, override_settings
import factory
from registration.forms import User

from openra import content
from openra.models import Reports
from openra.tests.factories import MapsFactory, ReportsFactory, ScreenshotsFactory, UserFactory
from openra.tests.routes.test_route_base import TestRouteBase


class TestRouteMapsUpdateMapInfo(TestRouteBase):

_route = '/maps/{}/update-map-info'

def test_route_can_be_accessed_by_any_user(self):
map = MapsFactory()
response = self.get_authed(
{},
None,
self._route.format(map.id)
)

self.assertEquals(
302,
response.status_code
)

self.assertEquals(
'/maps/' + str(map.id),
response.url
)

@override_settings(SITE_MAINTENANCE=True)
def test_route_shows_maintenance_page(self):
map = MapsFactory()
self.assert_is_maintenance(
self.get(
{},
self._route.format(map.id)
),
)

def test_route_redirects_to_login_if_not_authed(self):
map = MapsFactory()

response = self.get(
{},
self._route.format(map.id)
)

self.assertEquals(
302,
response.status_code
)

self.assertEquals(
'/login/',
response.url
)

def test_map_info_can_be_updated_by_map_uploader(self):
user = UserFactory()

map = MapsFactory(
user=user
)

response = self.post_authed(
{
'mapInfo': 'new map info',
},
user,
self._route.format(map.id)
)

map.refresh_from_db()

self.assertEquals(
'new map info',
map.info
)

self.assertEquals(
302,
response.status_code
)

self.assertEquals(
'/maps/' + str(map.id),
response.url
)

def test_map_info_can_be_updated_by_a_superuser(self):
user = UserFactory(
is_superuser=True
)

map = MapsFactory()

response = self.post_authed(
{
'mapInfo': 'new map info',
},
user,
self._route.format(map.id)
)

map.refresh_from_db()

self.assertEquals(
'new map info',
map.info
)

self.assertEquals(
302,
response.status_code
)

self.assertEquals(
'/maps/' + str(map.id),
response.url
)

def test_map_info_cannot_be_updated_by_other_users(self):
user = UserFactory()

map = MapsFactory()

response = self.post_authed(
{
'mapInfo': 'new map info',
},
user,
self._route.format(map.id)
)

map.refresh_from_db()

self.assertNotEquals(
'new map info',
map.info
)

self.assertEquals(
302,
response.status_code
)

self.assertEquals(
'/maps/' + str(map.id),
response.url
)
1 change: 1 addition & 0 deletions openra/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
url(r'^maps/(?P<arg>\d+)/lua/(?P<name>[^/]+)/?$', views.serveLua, name='printLua'),

url(r'^maps/(?P<map_id>\d+)/report', views.map_report, name='map_report'),
url(r'^maps/(?P<map_id>\d+)/update-map-info', views.map_update_map_info, name='map_update_map_info'),

url(r'^upload/map/?$', views.uploadMap, name='uploadMap'),
url(r'^upload/map/(?P<previous_rev>\d+)/?$', views.uploadMap, name='uploadMap'),
Expand Down
22 changes: 15 additions & 7 deletions openra/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,23 @@ def map_report(request, map_id):
return HttpResponseRedirect('/maps/' + map_id)


def map_update_map_info(request, map_id):
if not request.user.is_authenticated():
return HttpResponseRedirect('/login/')

map_info = request.POST.get('mapInfo', False)
if map_info:
map_info = map_info.strip()
target_map = Maps.objects.get(id=map_id)
if request.user.is_superuser or request.user.id == target_map.user_id:
target_map.info = map_info
target_map.save()
return HttpResponseRedirect('/maps/' + map_id)


def displayMap(request, arg):
if request.method == 'POST':
if request.POST.get('mapInfo', False) is not False:
if request.user.is_superuser:
Maps.objects.filter(id=arg).update(info=request.POST['mapInfo'].strip())
else:
Maps.objects.filter(id=arg, user_id=request.user.id).update(info=request.POST['mapInfo'].strip())
return HttpResponseRedirect('/maps/' + arg)
elif request.FILES.get('screenshot', False) is not False:
if request.FILES.get('screenshot', False) is not False:

handlers.addScreenshot(request, arg, 'map')

Expand Down

0 comments on commit 8d03788

Please sign in to comment.