Skip to content

Commit 7fafb00

Browse files
authored
Merge c83ee67 into bb1d8e6
2 parents bb1d8e6 + c83ee67 commit 7fafb00

3 files changed

Lines changed: 54 additions & 10 deletions

File tree

contentcuration/contentcuration/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,6 @@ def check_space(self, size, checksum):
303303

304304
def check_channel_space(self, channel):
305305
active_files = self.get_user_active_files()
306-
active_size = float(active_files.aggregate(used=Sum('file_size'))['used'] or 0)
307-
308306
staging_tree_id = channel.staging_tree.tree_id
309307
channel_files = self.files\
310308
.filter(contentnode__tree_id=staging_tree_id)\
@@ -313,7 +311,7 @@ def check_channel_space(self, channel):
313311
.exclude(checksum__in=active_files.values_list('checksum', flat=True))
314312
staged_size = float(channel_files.aggregate(used=Sum('file_size'))['used'] or 0)
315313

316-
if self.get_available_space(active_files=active_files) < (active_size + staged_size):
314+
if self.get_available_space(active_files=active_files) < (staged_size):
317315
raise PermissionDenied(_('Out of storage! Request more space under Settings > Storage.'))
318316

319317
def check_staged_space(self, size, checksum):
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Tests for contentcuration.views.internal functions.
4+
"""
5+
from django.core.urlresolvers import reverse_lazy
6+
7+
from ..base import BaseAPITestCase
8+
from contentcuration.models import Channel
9+
from contentcuration.utils.db_tools import TreeBuilder
10+
11+
12+
class APIActivateChannelEndpointTestCase(BaseAPITestCase):
13+
def test_200_post(self):
14+
main_tree = TreeBuilder(user=self.user)
15+
staging_tree = TreeBuilder(user=self.user)
16+
self.channel.main_tree = main_tree.root
17+
self.channel.staging_tree = staging_tree.root
18+
self.channel.save()
19+
response = self.post(
20+
reverse_lazy("activate_channel"), {"channel_id": self.channel.id}
21+
)
22+
self.assertEqual(response.status_code, 200)
23+
24+
def test_404_no_permission(self):
25+
new_channel = Channel.objects.create()
26+
staging_tree = TreeBuilder(user=self.user, levels=1)
27+
new_channel.staging_tree = staging_tree.root
28+
new_channel.save()
29+
response = self.post(
30+
reverse_lazy("activate_channel"), {"channel_id": new_channel.id}
31+
)
32+
self.assertEqual(response.status_code, 404)
33+
34+
def test_200_no_change_in_space(self):
35+
main_tree = TreeBuilder(user=self.user)
36+
staging_tree = TreeBuilder(user=self.user)
37+
self.channel.main_tree = main_tree.root
38+
self.channel.staging_tree = staging_tree.root
39+
self.channel.save()
40+
self.user.disk_space = self.user.get_space_used(active_files=self.user.get_user_active_files())
41+
self.user.save()
42+
response = self.post(
43+
reverse_lazy("activate_channel"), {"channel_id": self.channel.id}
44+
)
45+
self.assertEqual(response.status_code, 200)

contentcuration/contentcuration/views/base.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from django.db.models import Q
1515
from django.db.models import Subquery
1616
from django.http import HttpResponse
17-
from django.http import HttpResponseBadRequest
1817
from django.http import HttpResponseForbidden
18+
from django.http import HttpResponseNotFound
1919
from django.shortcuts import redirect
2020
from django.shortcuts import render
2121
from django.urls import is_valid_path
@@ -324,14 +324,15 @@ def accessible_channels(request, channel_id):
324324
return Response(map(map_channel_data, channels_data))
325325

326326

327+
@api_view(['POST'])
328+
@authentication_classes((SessionAuthentication,))
329+
@permission_classes((IsAuthenticated,))
327330
def activate_channel_endpoint(request):
328-
if request.method != "POST":
329-
return HttpResponseBadRequest(
330-
"Only POST requests are allowed on this endpoint."
331-
)
332-
333331
data = json.loads(request.body)
334-
channel = Channel.objects.get(pk=data["channel_id"])
332+
try:
333+
channel = Channel.filter_edit_queryset(Channel.objects.all(), request.user).get(pk=data["channel_id"])
334+
except Channel.DoesNotExist:
335+
return HttpResponseNotFound("Channel not found")
335336
changes = []
336337
try:
337338
change = activate_channel(channel, request.user)

0 commit comments

Comments
 (0)