Skip to content

Commit

Permalink
New: A basic GUI to add/remove Curators
Browse files Browse the repository at this point in the history
Refs #213
  • Loading branch information
AFFogarty committed Mar 10, 2016
1 parent d4de543 commit b88dc80
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 4 deletions.
18 changes: 18 additions & 0 deletions elvis/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ def __add_movement(self, movement):
else:
movement.collections.add(self)

def add_curator(self, user):
"""
Add a curator to the collection. A curator cannot be the owner.
:param user:
:return:
"""
if user != self.creator:
self.curators.add(user)

def remove_curator(self, user):
"""
Remove a curator from the collection.
:param user:
:return:
"""
self.curators.remove(user)

def solr_dict(self):
collection = self
if collection.creator:
Expand Down
71 changes: 70 additions & 1 deletion elvis/static/js/template-scripts/collection-detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,73 @@ $(document).ready(function ($)
$base_modal.modal('show');
}
});
});

$('form[name="add-curator"]').submit(function(event) {
event.preventDefault();
// Grab the username
var userName = event.target[0].value;
var errorMessage = event.target.children[3];
console.log(event);
// Hit the API
var url = window.location.href + "curators/";
$.ajax({
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
type: "post",
data: JSON.stringify({
username: userName
}),
dataType: 'json',
success: function(data) {
console.log("success:", data);
// Refresh the page
document.location.href = window.location.href;
},
error: function (data) {
console.log("error:", data);
console.log(errorMessage);
errorMessage.innerHTML = data.responseText;
}
});
});

$('form[name="remove-curator"]').submit(function(event) {
event.preventDefault();

var usernames = [];
// Get the usernames
$('input[name="remove-curator"]:checked').map(function() {
usernames.push($(this).val());
});

if (usernames.length > 0) {
var url = window.location.href + "curators/";
// Some things were checked!
$.ajax({
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
type: "delete",
data: JSON.stringify({
usernames: usernames
}),
dataType: 'json',
success: function(data) {
console.log("success:", data);
//// Refresh the page
//document.location.href = window.location.href;
},
error: function (data) {
console.log("error:", data);
//console.log(errorMessage);
//errorMessage.innerHTML = data.responseText;
}
});
}
});
});
22 changes: 21 additions & 1 deletion elvis/templates/collection/collection_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,27 @@ <h4>Movements</h4>
</form>
</div>
<div role="tabpanel" class="tab-pane" id="options-manage-curators">
Feature Coming Soon...
<h3>Add Curators</h3>
<form name="add-curator">
<label>Username:</label>
<input name="user-name"/>
<button type="submit" class="btn btn-default">Add</button>
<p class="text-danger"></p>
</form>
<h3>Remove Curators</h3>
<form name="remove-curator">
<ul class="list-group">
{% for curator in content.curators %}
<li class="list-group-item">
<input name="remove-curator" value="{{ curator.username }}" type="checkbox">
{{ curator.last_name }}, {{ curator.first_name }} <small>{{ curator.username }}</small>
</li>
{% empty %}
<p>None</p>
{% endfor %}
</ul>
<button type="submit" class="btn btn-danger">Remove</button>
</form>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion elvis/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from elvis.views.movement import MovementList, MovementDetail
from elvis.views.composer import ComposerList, ComposerDetail
from elvis.views.collection import CollectionList, CollectionDetail, \
CollectionCreate, CollectionUpdate, CollectionElements, MyCollections
CollectionCreate, CollectionUpdate, CollectionElements, CollectionCurators, MyCollections
from elvis.views.media import MediaServeView
from django.contrib.auth import views as auth_views

Expand Down Expand Up @@ -61,6 +61,7 @@
url(r'^collection/create/', CollectionCreate.as_view(), name="collection-create", kwargs={'model': "Collection"}),
url(r'^collection/(?P<pk>[0-9]+)/update/$', CollectionUpdate.as_view(), name="collection-update", kwargs={'model': "Collection"}),
url(r'^collection/(?P<pk>[0-9]+)/elements/$', CollectionElements.as_view(), name="collection-elements", kwargs={'model': "Collection"}),
url(r'^collection/(?P<pk>[0-9]+)/curators/$', CollectionCurators.as_view(), name="collection-curators", kwargs={'model': "Collection"}),
url(r'^collections/mine/$', MyCollections.as_view(), name="my-collections", kwargs={'model': "Collection"}),

url(r'^composers/$', ComposerList.as_view(), name="composer-list", kwargs={'model': "Composer"}),
Expand Down
66 changes: 65 additions & 1 deletion elvis/views/collection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ujson as json
import datetime
import pytz

from django.contrib.auth.models import User
from rest_framework import generics
from rest_framework import status
from rest_framework.response import Response
Expand Down Expand Up @@ -130,6 +130,70 @@ def determine_perms(self, request, *args, **kwargs):
return super().determine_perms(request, *args, **kwargs)


class CollectionCurators(CollectionDetail):
def post(self, request, *args, **kwargs):
"""
If the user has permission to edit the collection, add the specified pieces
to the specified collection.
:param request:
:param args:
:param kwargs:
:return:
"""
if self.determine_perms(request, *args, **kwargs)['can_edit']:
username = request.data.get("username")
try:
user = User.objects.get(username=username)
except ObjectDoesNotExist:
return HttpResponse(
content="User {0} does not exist.".format(username),
status=status.HTTP_400_BAD_REQUEST)

collection = Collection.objects.get(id=int(kwargs['pk']))
collection.add_curator(user)
return HttpResponse(
content="Curator added to collection.",
content_type="application/json",
status=status.HTTP_200_OK)
else:
raise PermissionDenied

def delete(self, request, *args, **kwargs):
"""
If the user has permission to edit the collection, remove the specified
pieces from the collection.
:param request:
:param args:
:param kwargs:
:return:
"""
if self.determine_perms(request, *args, **kwargs)["can_edit"]:
usernames = request.data.get("usernames")
if not usernames:
return HttpResponse(
content="Please provide some usernames.",
status=status.HTTP_400_BAD_REQUEST
)
collection = Collection.objects.get(id=int(kwargs['pk']))
for username in usernames:
try:
user = User.objects.get(username=username)
except ObjectDoesNotExist:
# User doesn't exist, so keep going.
continue
collection.remove_curator(user)

return HttpResponse(
content="{0} removed from collection {1}.".format(usernames, collection.title),
content_type="application/json",
status=status.HTTP_200_OK
)
else:
raise PermissionDenied


class CollectionElements(CollectionDetail):
def patch(self, request, *args, **kwargs):
"""
Expand Down

0 comments on commit b88dc80

Please sign in to comment.