Worked on startup process.

- fix group view on reload
This commit is contained in:
FinnStutzenstein 2017-01-14 13:02:26 +01:00 committed by Emanuel Schütze
parent 481a36501f
commit a6d1eeb9c3
14 changed files with 157 additions and 182 deletions

View File

@ -95,8 +95,7 @@ Other:
- Added new caching system with support for Redis.
- Support https as websocket protocol (wss).
- Added migration path from 2.0.
- Add an api for apps to send data when the websocket connection is established.
- Used this api for all models expect of users to send all data to the client.
- Accelerated startup process (send all data to the client after login).
Version 2.0 (2016-04-18)

View File

@ -129,6 +129,25 @@ img {
margin: 0 auto 0 auto;
}
#startup-overlay {
display: table;
background-color: #fff;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1000;
}
#startup-overlay h1 {
font-size: 56px;
text-align: center;
}
#startup-overlay > div {
display: table-cell;
vertical-align: middle;
}
/** Header **/
#header {
float: left;
@ -138,12 +157,6 @@ img {
color: #999;
}
#header div.unconnected {
border: 1px solid red;
position: fixed;
width: 100%;
z-index: 1000;
}
#header a.headerlink {
text-decoration: none;
}

View File

@ -53,13 +53,12 @@ angular.module('OpenSlidesApp.core', [
.factory('autoupdate', [
'DS',
'$rootScope',
'REALM',
'ProjectorID',
function (DS, $rootScope, REALM, ProjectorID) {
'$q',
function (DS, REALM, ProjectorID, $q) {
var socket = null;
var recInterval = null;
$rootScope.connected = false;
var websocketProtocol;
if (location.protocol == 'https:') {
@ -79,6 +78,8 @@ angular.module('OpenSlidesApp.core', [
var Autoupdate = {};
Autoupdate.messageReceivers = [];
// We use later a promise to defer the first message of the established ws connection.
Autoupdate.firstMessageDeferred = $q.defer();
Autoupdate.onMessage = function (receiver) {
Autoupdate.messageReceivers.push(receiver);
};
@ -90,11 +91,7 @@ angular.module('OpenSlidesApp.core', [
Autoupdate.newConnect = function () {
socket = new WebSocket(websocketProtocol + '//' + location.host + websocketPath);
clearInterval(recInterval);
socket.onopen = function () {
$rootScope.connected = true;
};
socket.onclose = function () {
$rootScope.connected = false;
socket = null;
recInterval = setInterval(function () {
Autoupdate.newConnect();
@ -104,12 +101,50 @@ angular.module('OpenSlidesApp.core', [
_.forEach(Autoupdate.messageReceivers, function (receiver) {
receiver(event.data);
});
// The first message is done: resolve the promise.
// TODO: check whether the promise is already resolved.
Autoupdate.firstMessageDeferred.resolve();
};
};
return Autoupdate;
}
])
.factory('operator', [
'User',
'Group',
function (User, Group) {
var operator = {
user: null,
perms: [],
isAuthenticated: function () {
return !!this.user;
},
setUser: function(user_id, user_data) {
if (user_id && user_data) {
operator.user = User.inject(user_data);
operator.perms = operator.user.getPerms();
} else {
operator.user = null;
operator.perms = Group.get(1).permissions;
}
},
// Returns true if the operator has at least one perm of the perms-list.
hasPerms: function(perms) {
if (typeof perms === 'string') {
perms = perms.split(' ');
}
return _.intersection(perms, operator.perms).length > 0;
},
// Returns true if the operator is a member of group.
isInGroup: function(group) {
return _.indexOf(operator.user.groups_id, group.id) > -1;
},
};
return operator;
}
])
// gets all in OpenSlides available languages
.factory('Languages', [
'gettext',
@ -227,7 +262,7 @@ angular.module('OpenSlidesApp.core', [
var deletedElements = [];
var collectionString = key;
_.forEach(list, function(data) {
// uncomment this line for debugging to log all autoupdates:
// Uncomment this line for debugging to log all autoupdates:
// console.log("Received object: " + data.collection + ", " + data.id);
// remove (=eject) object from local DS store
@ -299,7 +334,6 @@ angular.module('OpenSlidesApp.core', [
])
// Template hooks
.factory('templateHooks', [
function () {
var hooks = {};

View File

@ -8,6 +8,13 @@ angular.module('OpenSlidesApp.core.projector', ['OpenSlidesApp.core'])
// Can be used to find out if the projector or the side is used
.constant('REALM', 'projector')
.run([
'autoupdate',
function (autoupdate) {
autoupdate.newConnect();
}
])
// Provider to register slides in a .config() statement.
.provider('slides', [
function() {
@ -84,10 +91,8 @@ angular.module('OpenSlidesApp.core.projector', ['OpenSlidesApp.core'])
'$scope',
'$location',
'gettext',
'loadGlobalData',
'Projector',
function($scope, $location, gettext, loadGlobalData, Projector) {
loadGlobalData();
function($scope, $location, gettext, Projector) {
$scope.error = '';
// watch for changes in Projector

View File

@ -5,6 +5,7 @@
// The core module for the OpenSlides site
angular.module('OpenSlidesApp.core.site', [
'OpenSlidesApp.core',
'OpenSlidesApp.core.start',
'OpenSlidesApp.poll.majority',
'ui.router',
'angular-loading-bar',
@ -77,10 +78,7 @@ angular.module('OpenSlidesApp.core.site', [
this.$get = ['operator', function(operator) {
return {
registerScope: function (scope) {
var that = this;
this.scope = scope;
this.updateMainMenu();
// TODO: operator.onOperatorChange(function () {that.updateMainMenu();});
},
updateMainMenu: function () {
this.scope.elements = this.getElements();
@ -660,28 +658,6 @@ angular.module('OpenSlidesApp.core.site', [
}
])
.directive('routeLoadingIndicator', [
'$rootScope',
'$state',
'gettext',
function($rootScope, $state, gettext) {
gettext('Loading ...');
return {
restrict: 'E',
template: "<div class='header spacer-bottom' ng-if='isRouteLoading'><div class='title'><h1><translate>Loading ...</translate> <i class='fa fa-spinner fa-pulse'></i></h1></div></div>",
link: function(scope, elem, attrs) {
scope.isRouteLoading = false;
$rootScope.$on('$stateChangeStart', function() {
scope.isRouteLoading = true;
});
$rootScope.$on('$stateChangeSuccess', function() {
scope.isRouteLoading = false;
});
}
};
}
])
/* This directive provides a csv import template.
* Papa Parse is used to parse the csv file. Accepted attributes:
* * change:

View File

@ -10,8 +10,10 @@ angular.module('OpenSlidesApp.core.start', [])
'$state',
'autoupdate',
'operator',
function($http, $rootScope, $state, autoupdate, operator) {
// Put the operator into the root scope
'Group',
'mainMenu',
function($http, $rootScope, $state, autoupdate, operator, Group, mainMenu) {
$rootScope.startupWaitingEnabled = true;
$http.get('/users/whoami/').success(function(data) {
$rootScope.guest_enabled = data.guest_enabled;
if (data.user_id === null && !data.guest_enabled) {
@ -19,46 +21,15 @@ angular.module('OpenSlidesApp.core.start', [])
$state.go('login', {guest_enabled: data.guest_enabled});
} else {
autoupdate.newConnect();
// TODO: Connect websocket
// Then operator.setUser(data.user_id, data.user); $rootScope.operator = operator;
autoupdate.firstMessageDeferred.promise.then(function () {
operator.setUser(data.user_id, data.user);
$rootScope.operator = operator;
mainMenu.updateMainMenu();
$rootScope.startupWaitingEnabled = false;
});
}
});
}
])
.factory('operator', [
'Group',
'User',
function (User, Group) {
var operator = {
user: null,
perms: [],
isAuthenticated: function () {
return !!this.user;
},
setUser: function(user_id, user_data) {
if (user_id && user_data) {
operator.user = User.inject(user_data);
operator.perms = operator.user.getPerms();
} else {
operator.user = null;
operator.perms = Group.get(1).permissions;
}
},
// Returns true if the operator has at least one perm of the perms-list.
hasPerms: function(perms) {
if (typeof perms === 'string') {
perms = perms.split(' ');
}
return _.intersection(perms, operator.perms).length > 0;
},
// Returns true if the operator is a member of group.
isInGroup: function(group) {
return _.indexOf(operator.user.groups_id, group.id) > -1;
},
};
return operator;
}
])
]);
}());

View File

@ -20,9 +20,15 @@
<div id="wrapper" ng-cloak>
<!-- please wait -->
<div id="startup-overlay" ng-if="startupWaitingEnabled">
<div>
<h1><i class="fa fa-spinner fa-pulse"></i></h1>
</div>
</div>
<!-- Header -->
<div id="header">
<div ng-hide="connected" class="unconnected"></div>
<div class="containerOS">
<!-- Logo -->
<div class="title">
@ -174,8 +180,7 @@
<div class="containerOS">
<div class="col1" ng-class="isProjectorSidebar ? 'min' : 'max'">
<!-- dynamic views -->
<route-loading-indicator></route-loading-indicator>
<div ui-view ng-if="!isRouteLoading"></div>
<div ui-view></div>
<!-- footer -->
<div id="footer">
&copy; Copyright by <a href="http://www.openslides.org" target="_blank">OpenSlides</a> |

View File

@ -198,14 +198,14 @@ angular.module('OpenSlidesApp.motions', [
'jsDataModel',
'gettext',
'gettextCatalog',
'operator',
'Config',
'lineNumberingService',
'diffService',
'OpenSlidesSettings',
'Projector',
'operator',
function(DS, $http, MotionPoll, MotionChangeRecommendation, MotionComment, jsDataModel, gettext, gettextCatalog,
operator, Config, lineNumberingService, diffService, OpenSlidesSettings, Projector) {
Config, lineNumberingService, diffService, OpenSlidesSettings, Projector, operator) {
var name = 'motions/motion';
return DS.defineResource({
name: name,

View File

@ -285,14 +285,6 @@ angular.module('OpenSlidesApp.motions.site', [
}
])
// Load all MotionWorkflows at startup
.run([
'Workflow',
function (Workflow) {
Workflow.findAll();
}
])
.factory('ChangeRecommendationForm', [
'gettextCatalog',
'Editor',

View File

@ -30,4 +30,9 @@ class UsersAppConfig(AppConfig):
# Register viewsets.
router.register(self.get_model('User').get_collection_string(), UserViewSet)
router.register('users/group', GroupViewSet)
router.register(self.get_model('Group').get_collection_string(), GroupViewSet)
def get_startup_elements(self):
from ..utils.collection import Collection
for model in ('User', 'Group'):
yield Collection(self.get_model(model).get_collection_string())

View File

@ -40,31 +40,13 @@ angular.module('OpenSlidesApp.users.site', [
abstract: true,
template: "<ui-view/>",
})
.state('users.user.list', {
resolve: {
users: function(User) {
return User.findAll();
},
groups: function(Group) {
return Group.findAll();
}
}
})
.state('users.user.create', {
resolve: {
groups: function(Group) {
return Group.findAll();
}
}
})
.state('users.user.list', {})
.state('users.user.create', {})
.state('users.user.detail', {
resolve: {
user: function(User, $stateParams) {
return User.find($stateParams.id);
},
groups: function(Group) {
return Group.findAll();
}
userId: ['$stateParams', function($stateParams) {
return $stateParams.id;
}]
}
})
// Redirects to user detail view and opens user edit form dialog, uses edit url.
@ -72,8 +54,8 @@ angular.module('OpenSlidesApp.users.site', [
// (from users list controller use UserForm factory instead to open dialog in front of
// current view without redirect)
.state('users.user.detail.update', {
onEnter: ['$stateParams', '$state', 'ngDialog', 'User',
function($stateParams, $state, ngDialog, User) {
onEnter: ['$stateParams', '$state', 'ngDialog',
function($stateParams, $state, ngDialog) {
ngDialog.open({
template: 'static/templates/users/user-form.html',
controller: 'UserUpdateCtrl',
@ -81,7 +63,7 @@ angular.module('OpenSlidesApp.users.site', [
closeByEscape: false,
closeByDocument: false,
resolve: {
user: function() {return User.find($stateParams.id);}
userId: function() {return $stateParams.id;}
}
});
}
@ -106,22 +88,14 @@ angular.module('OpenSlidesApp.users.site', [
controller: 'UserChangePasswordCtrl',
templateUrl: 'static/templates/users/user-change-password.html',
resolve: {
user: function(User, $stateParams) {
return User.find($stateParams.id);
}
userId: ['$stateParams', function($stateParams) {
return $stateParams.id;
}]
}
})
.state('users.user.import', {
url: '/import',
controller: 'UserImportCtrl',
resolve: {
groups: function(Group) {
return Group.findAll();
},
users: function(User) {
return User.findAll();
}
}
})
// groups
.state('users.group', {
@ -134,9 +108,6 @@ angular.module('OpenSlidesApp.users.site', [
})
.state('users.group.list', {
resolve: {
groups: function(Group) {
return Group.findAll();
},
permissions: function(Group) {
return Group.getPermissions();
}
@ -188,7 +159,7 @@ angular.module('OpenSlidesApp.users.site', [
}
$scope.$watch(
function (scope) {
return scope.operator.hasPerms(perms);
return scope.operator && scope.operator.hasPerms(perms);
},
function (value) {
if ($attr.osPerms[0] === '!') {
@ -262,19 +233,15 @@ angular.module('OpenSlidesApp.users.site', [
return {
// ngDialog for user form
getDialog: function (user) {
var resolve;
if (user) {
resolve = {
user: function(User) {return User.find(user.id);}
};
}
return {
template: 'static/templates/users/user-form.html',
controller: (user) ? 'UserUpdateCtrl' : 'UserCreateCtrl',
className: 'ngdialog-theme-default wide-form',
closeByEscape: false,
closeByDocument: false,
resolve: (resolve) ? resolve : null
resolve: {
userId: function () {return user ? user.id : void 0;},
}
};
},
// angular-formly fields for user form
@ -675,12 +642,12 @@ angular.module('OpenSlidesApp.users.site', [
'ngDialog',
'UserForm',
'User',
'user',
'userId',
'Group',
'Projector',
'ProjectionDefault',
function($scope, ngDialog, UserForm, User, user, Group, Projector, ProjectionDefault) {
User.bindOne(user.id, $scope, 'user');
function($scope, ngDialog, UserForm, User, userId, Group, Projector, ProjectionDefault) {
User.bindOne(userId, $scope, 'user');
Group.bindAll({where: {id: {'>': 1}}}, $scope, 'groups');
$scope.$watch(function () {
return Projector.lastModified();
@ -738,13 +705,13 @@ angular.module('OpenSlidesApp.users.site', [
'User',
'UserForm',
'Group',
'user',
function($scope, $state, $http, User, UserForm, Group, user) {
'userId',
function($scope, $state, $http, User, UserForm, Group, userId) {
Group.bindAll({where: {id: {'>': 2}}}, $scope, 'groups');
$scope.alert = {};
// set initial values for form model by create deep copy of user object
// so list/detail view is not updated while editing
$scope.model = angular.copy(user);
$scope.model = angular.copy(User.get(userId));
// get all form fields
$scope.formFields = UserForm.getFormFields();
@ -803,11 +770,11 @@ angular.module('OpenSlidesApp.users.site', [
'$state',
'$http',
'User',
'user',
'userId',
'gettextCatalog',
'PasswordGenerator',
function($scope, $state, $http, User, user, gettextCatalog, PasswordGenerator) {
User.bindOne(user.id, $scope, 'user');
function($scope, $state, $http, User, userId, gettextCatalog, PasswordGenerator) {
User.bindOne(userId, $scope, 'user');
$scope.alert={};
$scope.generatePassword = function () {
$scope.new_password = PasswordGenerator.generate();
@ -1210,7 +1177,7 @@ angular.module('OpenSlidesApp.users.site', [
$scope.groups.forEach(function (group) {
if ((_.indexOf(group.permissions, 'users.can_see_name') > -1) &&
(_.indexOf(group.permissions, 'users.can_manage') > -1)){
if (operator.isInGroup(group)){
if (!operator.user || operator.isInGroup(group)){
groups_danger.push(group);
}
}
@ -1342,19 +1309,15 @@ angular.module('OpenSlidesApp.users.site', [
};
$scope.openDialog = function (group) {
var resolve;
if (group) {
resolve = {
group: function() {return Group.find(group.id);}
};
}
ngDialog.open({
template: 'static/templates/users/group-edit.html',
controller: group ? 'GroupRenameCtrl' : 'GroupCreateCtrl',
className: 'ngdialog-theme-default wide-form',
closeByEscape: false,
closeByDocument: false,
resolve: (resolve) ? resolve : null
resolve: {
group: function () {return group;},
}
});
};
}
@ -1439,10 +1402,15 @@ angular.module('OpenSlidesApp.users.site', [
'$rootScope',
'$scope',
'$http',
'$state',
'$stateParams',
'$q',
'operator',
'gettext',
function ($rootScope, $scope, $http, $stateParams, operator, gettext) {
'autoupdate',
'mainMenu',
'DS',
function ($rootScope, $scope, $http, $state, $stateParams, $q, operator, gettext, autoupdate, mainMenu, DS) {
$scope.alerts = [];
// get login info-text from server
@ -1478,11 +1446,19 @@ angular.module('OpenSlidesApp.users.site', [
$http.post('/users/login/', data).then(
function (response) {
// Success: User logged in.
operator.setUser(response.data.user_id);
$scope.closeThisDialog();
setTimeout(function(){
window.location.replace('/');
}, 1000);
// Clear store and reset deferred first message, if guests was enabled before.
DS.clear();
autoupdate.firstMessageDeferred = $q.defer();
// The next lines are partly the same lines as in core/start.js
autoupdate.newConnect();
autoupdate.firstMessageDeferred.promise.then(function () {
operator.setUser(response.data.user_id, response.data.user);
$rootScope.operator = operator;
mainMenu.updateMainMenu();
$scope.closeThisDialog();
$state.go('home');
$rootScope.startupWaitingEnabled = false;
});
},
function (response) {
// Error: Username or password is not correct.

View File

@ -5,6 +5,7 @@ from django.utils.encoding import force_text
from django.utils.translation import ugettext as _
from ..core.config import config
from ..utils.collection import CollectionElement
from ..utils.rest_api import (
ModelViewSet,
Response,
@ -13,7 +14,6 @@ from ..utils.rest_api import (
detail_route,
status,
)
from ..utils.collection import CollectionElement
from ..utils.views import APIView
from .access_permissions import GroupAccessPermissions, UserAccessPermissions
from .models import Group, User
@ -209,6 +209,8 @@ class UserLoginView(APIView):
else:
# self.request.method == 'POST'
context['user_id'] = self.user.pk
user_collection = CollectionElement.from_instance(self.user)
context['user'] = user_collection.as_dict_for_user(self.user)
return super().get_context_data(**context)
@ -238,7 +240,7 @@ class WhoAmIView(APIView):
Appends also the serialized user if available.
"""
user_id = self.request.user.pk
if self.request.user.pk is not None:
if user_id is not None:
user_collection = CollectionElement.from_instance(self.request.user)
user_data = user_collection.as_dict_for_user(self.request.user)
else:

View File

@ -21,7 +21,7 @@ def ws_add_site(message):
The group with the name 'user-None' stands for all anonymous users.
Send all "starup-data" through the connection.
Send all "startup-data" through the connection.
"""
Group('site').add(message.reply_channel)
message.channel_session['user_id'] = message.user.id
@ -42,11 +42,11 @@ def ws_add_site(message):
for collection in get_startup_elements():
output.extend(collection.as_autoupdate_for_user(message.user))
# Send all data. If there is no data, then onyl accept the connection
# Send all data. If there is no data, then only accept the connection
if output:
message.reply_channel.send({'text': json.dumps(output)})
else:
message.reply_channel.send({"accept": True})
message.reply_channel.send({'accept': True})
@channel_session_user

View File

@ -14,7 +14,7 @@ class TestWhoAmIView(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(
json.loads(response.content.decode()),
{'user_id': None, 'guest_enabled': False})
{'user_id': None, 'user': None, 'guest_enabled': False})
def test_get_authenticated_user(self):
self.client.login(username='admin', password='admin')
@ -22,9 +22,8 @@ class TestWhoAmIView(TestCase):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
json.loads(response.content.decode()),
{'user_id': 1, 'guest_enabled': False})
self.assertEqual(json.loads(response.content.decode()).get('user_id'), 1)
self.assertEqual(json.loads(response.content.decode()).get('guest_enabled'), False)
def test_post(self):
response = self.client.post(self.url)
@ -79,9 +78,7 @@ class TestUserLoginView(TestCase):
{'username': 'admin', 'password': 'admin'})
self.assertEqual(response.status_code, 200)
self.assertEqual(
json.loads(response.content.decode()),
{'user_id': 1})
self.assertEqual(json.loads(response.content.decode()).get('user_id'), 1)
def test_post_incorrect_data(self):
response = self.client.post(