Skip to content

Commit

Permalink
Merge pull request #131 from EsupPortail/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ptitloup authored May 3, 2019
2 parents 010f971 + 7a053dd commit 2912368
Show file tree
Hide file tree
Showing 25 changed files with 901 additions and 153 deletions.
15 changes: 15 additions & 0 deletions pod/authentication/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from pod.authentication.forms import OwnerAdminForm
from django.utils.html import format_html

from django.contrib.auth.models import Group
from pod.authentication.forms import GroupAdminForm

# Define an inline admin descriptor for Owner model
# which acts a bit like a singleton

Expand Down Expand Up @@ -69,6 +72,18 @@ def owner_hashkey(self, obj):
inlines = (OwnerInline, )


# Create a new Group admin.
class GroupAdmin(admin.ModelAdmin):
# Use our custom form.
form = GroupAdminForm
# Filter permissions horizontal as well.
filter_horizontal = ['permissions']


# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

# Register the new Group ModelAdmin instead of the original one.
admin.site.unregister(Group)
admin.site.register(Group, GroupAdmin)
43 changes: 43 additions & 0 deletions pod/authentication/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
from pod.authentication.models import Owner
from django.conf import settings

from django.contrib.auth import get_user_model
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.contrib.auth.models import Group
from django.utils.translation import ugettext_lazy as _

FILEPICKER = False
if getattr(settings, 'USE_PODFILE', False):
from pod.podfile.widgets import CustomFileWidget
Expand All @@ -26,3 +31,41 @@ class FrontOwnerForm(OwnerAdminForm):
class Meta(object):
model = Owner
fields = ('userpicture',)


User = get_user_model()


# Create ModelForm based on the Group model.
class GroupAdminForm(forms.ModelForm):
class Meta:
model = Group
exclude = []

# Add the users field.
users = forms.ModelMultipleChoiceField(
queryset=User.objects.all(),
required=False,
# Use the pretty 'filter_horizontal widget'.
widget=FilteredSelectMultiple(_('Users'), False),
label=_('Users')
)

def __init__(self, *args, **kwargs):
# Do the normal form initialisation.
super(GroupAdminForm, self).__init__(*args, **kwargs)
# If it is an existing group (saved objects have a pk).
if self.instance.pk:
# Populate the users field with the current Group users.
self.fields['users'].initial = self.instance.user_set.all()

def save_m2m(self):
# Add the users to the Group.
self.instance.user_set.set(self.cleaned_data['users'])

def save(self, *args, **kwargs):
# Default save
instance = super(GroupAdminForm, self).save()
# Save many-to-many data
self.save_m2m()
return instance
106 changes: 106 additions & 0 deletions pod/authentication/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
Unit tests for authentication views
"""
import os

from django.conf import settings
from django.test import override_settings
from django.test import TestCase
from django.test import Client
from django.contrib.auth.models import User


@override_settings(
MEDIA_ROOT=os.path.join(settings.BASE_DIR, 'media'),
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite',
}
},
LANGUAGE_CODE='en'
)
class authenticationViewsTestCase(TestCase):
fixtures = ['initial_data.json', ]

def setUp(self):
User.objects.create(username='pod', password='podv2')
print(" ---> SetUp of authenticationViewsTestCase : OK !")

def test_authentication_login_gateway(self):
self.client = Client()
# CAS_GATEWAY is valued to False
response = self.client.get('/authentication_login_gateway/')
self.assertEqual(response.content,
b'You must set CAS_GATEWAY to True to use this view')
self.assertEqual(response.status_code, 200)

print(
" ---> test_authentication_login_gateway \
of authenticationViewsTestCase : OK !")

def test_authentication_login(self):
self.client = Client()
self.user = User.objects.get(username="pod")

# User already authenticated
self.client.force_login(self.user)
response = self.client.get('/authentication_login/')
self.assertRedirects(response, '/')

# User not authenticated and CAS are valued to False
self.client.logout()
response = self.client.get('/authentication_login/')
self.assertRedirects(response, '/accounts/login/?next=/')

print(
" ---> test_authentication_login \
of authenticationViewsTestCase : OK !")

def test_authentication_logout(self):
self.client = Client()
# USE_CAS is valued to False
response = self.client.get('/authentication_logout/')
self.assertRedirects(response, '/accounts/logout/?next=/',
target_status_code=302)

print(
" ---> test_authentication_logout \
of authenticationViewsTestCase : OK !")

def test_userpicture(self):
self.client = Client()
self.user = User.objects.get(username="pod")
# User is not loged in
response = self.client.get('/accounts/userpicture/')
self.assertEqual(response.status_code, 302) # Redirect to login page

# User is loged in
self.client.force_login(self.user)
# GET method
response = self.client.get('/accounts/userpicture/')
messages = list(response.wsgi_request._messages)
self.assertEqual(len(messages), 0)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'userpicture/userpicture.html')
# POST method
# Form is valid
response = self.client.post('/accounts/userpicture/',
{'userpicture': ''})
messages = list(response.wsgi_request._messages)
self.assertEqual(len(messages), 1)
self.assertEqual(str(messages[0]), 'Your picture has been saved.')
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'userpicture/userpicture.html')
# Form is not valid
response = self.client.post('/accounts/userpicture/',
{'userpicture': 12})
messages = list(response.wsgi_request._messages)
self.assertEqual(len(messages), 1)
self.assertEqual(str(messages[0]),
'One or more errors have been found in the form.')
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'userpicture/userpicture.html')

print(
" ---> test_userpicture of authenticationViewsTestCase : OK !")
89 changes: 86 additions & 3 deletions pod/chapter/static/js/chapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var ajaxfail = function(data) {
$(document).on('click', '#cancel_chapter', function() {
$('form.get_form').show();
show_form('');
$('#fileModal_id_file').remove();
});

$(document).on('submit', 'form.get_form', function(e) {
Expand Down Expand Up @@ -91,7 +92,13 @@ var sendandgetform = function(elt, action) {
if (data.indexOf('list_chapter') == -1) {
showalert(gettext('You are no longer authenticated. Please log in again.'), 'alert-danger');
} else {
location.reload();
data = JSON.parse(data);
//location.reload();
updateDom(data);
manageDelete();
$(list_chapter).html(data['list_chapter']);
show_form('');
$('form.get_form').show();
}
});
jqxhr.fail(function($xhr) {
Expand Down Expand Up @@ -122,7 +129,12 @@ var sendform = function(elt, action) {
show_form(data.form);
$('form#form_chapter').show();
} else {
location.reload();
//location.reload();
updateDom(data);
manageSave();
$(list_chapter).html(data['list_chapter']);
show_form('');
$('form.get_form').show();
}
}
});
Expand All @@ -146,7 +158,13 @@ var sendform = function(elt, action) {
if (data.indexOf('list_chapter') == -1) {
showalert(gettext('You are no longer authenticated. Please log in again.'), 'alert-danger');
} else {
location.reload();
//location.reload();
data = JSON.parse(data);
updateDom(data);
manageImport();
$(list_chapter).html(data['list_chapter']);
show_form('');
$('form.get_form').show();
}
});
jqxhr.fail(function($xhr) {
Expand Down Expand Up @@ -216,3 +234,68 @@ $(document).on('click','#info_video span.getfromvideo a',function(e) {
}
}
});



var updateDom = function(data) {
let player = window.videojs.players.podvideoplayer;
let n1 = document.querySelector('ul#chapters');
let n2 = document.querySelector('div.chapters-list');
let tmp_node = document.createElement('div');
$(tmp_node).html(data['video-elem']);
let chaplist = tmp_node.querySelector('div.chapters-list');
if (n1 != null) {
n1.parentNode.removeChild(n1);
}
if (n2 != null) {
n2.parentNode.removeChild(n2);
}
if (chaplist != null && n2 != null) {
chaplist.className = n2.className;
}
$("#" + window.videojs.players.podvideoplayer.id_).append(chaplist);
$("#" + window.videojs.players.podvideoplayer.id_).append(tmp_node.querySelector('ul#chapters'));
}

var manageSave = function() {
let player = window.videojs.players.podvideoplayer;
if(player.usingPlugin('videoJsChapters')) {
player.main();
}
else {
player.videoJsChapters();
}
}

var manageDelete = function() {
let player = window.videojs.players.podvideoplayer;
let n = document.querySelector('div.chapters-list');
if(n != null) {
player.main();
}
else {
player.controlBar.chapters.dispose();
player.videoJsChapters().dispose();
}
}

var manageImport = function() {
let player = window.videojs.players.podvideoplayer;
let n = document.querySelector('div.chapters-list');
if(n != null) {
if(player.usingPlugin('videoJsChapters')) {
player.main();
}
else {
player.videoJsChapters();
}
}
else {
if(typeof(player.controlBar.chapters) != 'undefined') {
player.controlBar.chapters.dispose();
}
if(player.usingPlugin('videoJsChapters')) {
player.videoJsChapters().dispose();
}
}
}
Loading

0 comments on commit 2912368

Please sign in to comment.