add user.name, list fixes

This commit is contained in:
j 2014-05-25 14:16:04 +02:00
parent ed2b7cebfc
commit 98d1725d19
15 changed files with 180 additions and 203 deletions

View File

@ -240,6 +240,7 @@ class Changelog(db.Model):
def action_editusername(self, user, timestamp, username):
user.info['username'] = username
user.update_name()
user.save()
return True

View File

@ -7,6 +7,7 @@ from user.models import List, User
def create_default_lists(user_id=None):
user_id = user_id or settings.USER_ID
user = User.get_or_create(user_id)
user.update_name()
for list in settings.config['lists']:
l = List.get(user_id, list['title'])
if not l:

View File

@ -58,6 +58,10 @@ def setPreferences(data):
}
'''
update_dict(settings.preferences, data)
if 'username' in data:
u = state.user()
u.update_name()
u.save()
return settings.preferences
actions.register(setPreferences)
@ -102,7 +106,7 @@ def getLists(data):
'items': Item.query.count(),
'name': 'Libraries',
'type': 'libraries',
'user': '',
'user': None,
})
for u in models.User.query.filter((models.User.peered==True)|(models.User.id==settings.USER_ID)):
lists += u.lists_json()
@ -245,7 +249,11 @@ def editUser(data):
'''
if 'nickname' in data:
p = models.User.get_or_create(data['id'])
p.set_nickname(data['nickname'])
if data['nickname']:
p.info['nickname'] = data['nickname']
elif 'nickname' in p.info:
del p.info['nickname']
p.update_name()
p.save()
return {}
actions.register(editUser, cache=False)

View File

@ -50,6 +50,11 @@ class User(db.Model):
db.session.add(self)
db.session.commit()
@property
def name(self):
name = self.nickname if self.id != settings.USER_ID else ''
return name
def json(self):
j = {}
if self.info:
@ -59,7 +64,9 @@ class User(db.Model):
j['pending'] = self.pending
j['peered'] = self.peered
j['online'] = self.is_online()
j['nickname'] = self.nickname
j['nickname'] = self.info.get('nickname')
j['username'] = self.info.get('username') if self.id != settings.USER_ID else settings.preferences['username']
j['name'] = self.name
return j
def is_online(self):
@ -71,7 +78,7 @@ class User(db.Model):
'name': 'Library',
'type': 'library',
'items': self.items.count(),
'user': self.nickname if self.id != settings.USER_ID else settings.preferences['username'],
'user': self.name
}] + [l.json() for l in self.lists.order_by('index_')]
def update_peering(self, peered, username=None):
@ -80,11 +87,7 @@ class User(db.Model):
self.pending = ''
if username:
self.info['username'] = username
else:
username = self.info.get('username')
if not username:
username = 'anonymous'
self.set_nickname(username)
self.update_name()
# FIXME: need to set peered to False to not trigger changelog event
# before other side receives acceptPeering request
self.peered = False
@ -97,7 +100,7 @@ class User(db.Model):
else:
self.pending = ''
self.peered = False
self.nickname = None
self.update_name()
self.save()
List.query.filter_by(user_id=self.id).delete()
for i in self.items:
@ -112,11 +115,15 @@ class User(db.Model):
Changelog.record(state.user(), 'removepeer', self.id)
self.save()
def set_nickname(self, nickname):
username = nickname
def update_name(self):
if self.id == settings.USER_ID:
name = settings.preferences.get('username', 'anonymous')
else:
name = self.info.get('nickname') or self.info.get('username') or 'anonymous'
nickname = name
n = 2
while self.query.filter_by(nickname=nickname).filter(User.id!=self.id).first():
nickname = '%s [%d]' % (username, n)
nickname = '%s [%d]' % (name, n)
n += 1
self.nickname = nickname
@ -261,7 +268,7 @@ class List(db.Model):
def json(self):
r = {
'id': self.public_id,
'user': self.user.nickname if self.user_id != settings.USER_ID else settings.preferences['username'],
'user': self.user.name,
'name': self.name,
'index': self.index_,
'items': self.items_count(),

View File

@ -59,6 +59,7 @@ oml.UI = (function() {
// and list may then change listSort and listView,
// which we don't want to trigger, since find triggers
// (values we put in add will be changed, but won't trigger)
// FIXME: ABOVE COMMENT DOES NOT APPLY
list = oml.getListState(args.find);
ui._list = list;
ui._filterState = oml.getFilterState(args.find);
@ -74,66 +75,65 @@ oml.UI = (function() {
// then for each setting that corresponds to a list setting
if (!ui.lists[list]) {
// either add the default setting
add[setting] = oml.config.user.ui[setting];
args[setting] = oml.config.user.ui[setting];
} else {
// or the existing list setting
add[setting] = ui.lists[list][listSetting]
args[setting] = ui.lists[list][listSetting];
}
});
} else {
list = previousUI._list;
}
// it is important to check for find first, so that
// if find changes list, list is correct here
item = args.item || ui.item;
listView = add.listView || args.listView;
} else {
list = previousUI._list;
}
// it is important to check for find first, so that
// if find changes list, list is correct here
item = args.item || ui.item;
listView = add.listView || args.listView;
if (!ui.lists[list]) {
add['lists.' + that.encode(list)] = {};
}
Ox.forEach(listSettings, function(listSetting, setting) {
// for each setting that corresponds to a list setting
// set that list setting to
var key = 'lists.' + that.encode(list) + '.' + listSetting;
if (setting in args) {
// the setting passed to UI.set
add[key] = args[setting];
} else if (setting in add) {
// or the setting changed via find
add[key] = add[setting];
} else if (!ui.lists[list]) {
// or the default setting
add[key] = oml.config.user.ui[setting];
}
});
if (args.item) {
// when switching to an item, update list selection
add['listSelection'] = [args.item];
add['lists.' + that.encode(list) + '.selection'] = [args.item];
if (
!args.itemView
&& ui.itemView == 'book'
&& !ui.mediaState[item]
&& !args['mediaState.' + item]
) {
// if the item view doesn't change, remains a media view,
// media state doesn't exist yet, and won't be set, add
// default media state
add['mediaState.' + item] = {position: 0, zoom: 1};
}
if (!ui.lists[list]) {
add['lists.' + that.encode(list)] = {};
}
Ox.forEach(listSettings, function(listSetting, setting) {
// for each setting that corresponds to a list setting
// set that list setting to
var key = 'lists.' + that.encode(list) + '.' + listSetting;
if (setting in args) {
// the setting passed to UI.set
args[key] = args[setting];
} else if (setting in add) {
// or the setting changed via find
args[key] = add[setting];
} else if (!ui.lists[list]) {
// or the default setting
args[key] = oml.config.user.ui[setting];
}
});
if (args.item) {
// when switching to an item, update list selection
add['listSelection'] = [args.item];
add['lists.' + that.encode(list) + '.selection'] = [args.item];
if (
args.itemView == 'book'
!args.itemView
&& ui.itemView == 'book'
&& !ui.mediaState[item]
&& !args['mediaState.' + item]
) {
// when switching to a media view, media state doesn't exist
// yet, and won't be set, add default media state
// if the item view doesn't change, remains a media view,
// media state doesn't exist yet, and won't be set, add
// default media state
add['mediaState.' + item] = {position: 0, zoom: 1};
}
}
if (
args.itemView == 'book'
&& !ui.mediaState[item]
&& !args['mediaState.' + item]
) {
// when switching to a media view, media state doesn't exist
// yet, and won't be set, add default media state
add['mediaState.' + item] = {position: 0, zoom: 1};
}
// items in args trigger events, items in add do not

View File

@ -3,7 +3,6 @@
oml.ui.folders = function() {
var ui = oml.user.ui,
username = oml.user.preferences.username,
userIndex,
users,
@ -13,7 +12,6 @@ oml.ui.folders = function() {
that = Ox.Element()
.css({
overflowX: 'hidden',
//overflowY: 'auto',
})
.bindEvent({
oml_find: selectList,
@ -35,7 +33,7 @@ oml.ui.folders = function() {
function getFolderList(list) {
var index = users.map(function(user) {
return user.nickname;
return user.name;
}).indexOf(list.user);
return list.id == '' ? oml.$ui.librariesList
: Ox.endsWith(list.id, ':') ? oml.$ui.libraryList[index]
@ -43,8 +41,10 @@ oml.ui.folders = function() {
}
function getUsersAndLists(callback) {
oml.getUsers(function() {
users = arguments[0];
oml.getUsers(function(users_) {
users = users_.filter(function(user) {
return user.id == oml.user.id || user.peered;
});
oml.getLists(function(lists) {
callback(users, lists);
});
@ -53,7 +53,7 @@ oml.ui.folders = function() {
function selectList() {
var split = ui._list.split(':'),
index = userIndex[split[0] || oml.user.preferences.username],
index = userIndex[split[0]],
list = split[1],
$selectedList = !ui._list ? oml.$ui.librariesList
: !list ? oml.$ui[!list ? 'libraryList' : 'folderList'][index]
@ -84,14 +84,7 @@ oml.ui.folders = function() {
$lists.push(
oml.$ui.librariesList = oml.ui.folderList({
items: [
{
id: '',
name: Ox._('All Libraries'),
type: 'libraries',
items: Ox.getObjectById(lists, '').items
}
]
items: [lists[0]]
})
.bindEvent({
select: function() {
@ -101,7 +94,7 @@ oml.ui.folders = function() {
selectnext: function() {
oml.UI.set(Ox.extend(
{find: getFind(':')},
'showFolder.' + username,
'showFolder.',
true
));
},
@ -115,15 +108,15 @@ oml.ui.folders = function() {
var $content,
items = lists.filter(function(list) {
return list.user == user.nickname
return list.user === user.name
&& list.type != 'library';
}),
libraryId = (!index ? '' : user.nickname) + ':'
libraryId = user.name + ':';
userIndex[user.nickname] = index;
userIndex[user.name] = index;
oml.$ui.folder[index] = Ox.CollapsePanel({
collapsed: !ui.showFolder[user.nickname],
collapsed: !ui.showFolder[user.name],
extras: [
oml.ui.statusIcon(user, index),
{},
@ -146,18 +139,22 @@ oml.ui.folders = function() {
}
})
],
title: Ox.encodeHTMLEntities(user.nickname)
title: Ox.encodeHTMLEntities(
!index
? oml.user.preferences.username || 'anonymous'
: user.name
)
})
.css({
width: ui.sidebarSize
})
.bindEvent({
toggle: function(data) {
oml.UI.set('showFolder.' + user.nickname, !data.collapsed);
oml.UI.set('showFolder.' + user.name, !data.collapsed);
}
})
.bindEvent(
'oml_showfolder.' + user.nickname.toLowerCase(),
'oml_showfolder.' + user.name.toLowerCase(),
function(data) {
oml.$ui.folder[index].options({collapsed: !data.value});
}
@ -171,14 +168,10 @@ oml.ui.folders = function() {
$lists.push(
oml.$ui.libraryList[index] = oml.ui.folderList({
items: [
{
id: libraryId,
name: Ox._('Library'),
type: 'library',
items: Ox.getObjectById(lists, libraryId).items
}
]
items: lists.filter(function(list) {
return list.user == user.name
&& list.type == 'library';
})
})
.bindEvent({
add: function() {
@ -196,12 +189,12 @@ oml.ui.folders = function() {
if (!index) {
set = {find: getFind('')};
} else {
user = users[index - 1].nickname;
user = users[index - 1].name;
userLists = lists.filter(function(list) {
return list.user == user;
});
set = {find: getFind(
!userLists.length ? (user == oml.user.preferences.username ? '' : user) + ':'
!userLists.length ? user + ':'
: Ox.last(userLists).id
)};
Ox.extend(set, 'showFolder.' + user, true);
@ -248,8 +241,8 @@ oml.ui.folders = function() {
selectnext: function() {
if (index < users.length - 1) {
oml.UI.set(Ox.extend(
{find: getFind(users[index + 1].nickname + ':')},
'showFolder.' + users[index + 1].nickname,
{find: getFind(users[index + 1].name + ':')},
'showFolder.' + users[index + 1].name,
true
));
}
@ -299,8 +292,7 @@ oml.ui.folders = function() {
that.updateOwnLists = function(callback) {
oml.getLists(function(lists) {
var items = lists.filter(function(list) {
return list.user == oml.user.preferences.username
&& list.type != 'library';
return list.user == '' && list.type != 'library';
});
oml.$ui.folder[0].$content
.css({height: 16 + items.length * 16 + 'px'});

View File

@ -8,6 +8,14 @@ oml.ui.infoView = function(identifyData) {
css = getCSS(iconSize, oml.config.iconRatio),
ids = [
{key: 'isbn', url: 'https://google.com/search?q=ISBN+{0}'},
{key: 'asin', url: 'http://www.amazon.com/dp/{0}'},
{key: 'lccn', url: 'http://lccn.loc.gov/{0}'},
{key: 'oclc', url: 'https://www.worldcat.org/oclc/{0}'},
{key: 'olid', url: 'https://openlibrary.org/books/{0}'}
],
that = Ox.Element()
.addClass('OxTextPage')
.css({overflowY: 'auto'})
@ -132,14 +140,12 @@ oml.ui.infoView = function(identifyData) {
}),
data.mediastate == 'available' && data.primaryid
? Ox.Select({
items: Ox.flatten([
'isbn', 'asin', 'lccn', 'oclc', 'olid'
].map(function(key) {
return (data[key] || []).map(function(value) {
items: Ox.flatten(ids.map(function(id) {
return (data[id.key] || []).map(function(value) {
return {
id: key + ':' + value,
id: id.key + ':' + value,
title: '<b>' + Ox.getObjectById(
oml.config.itemKeys, key
oml.config.itemKeys, id.key
).title + ':</b> ' + value
};
});
@ -632,12 +638,10 @@ oml.ui.infoView = function(identifyData) {
renderIdentifyButton(data).appendTo($data);
[
'isbn', 'asin', 'lccn', 'oclc', 'olid'
].forEach(function(id, index) {
ids.forEach(function(id, index) {
var title;
if (data[id] && !Ox.isEmpty(data[id])) {
title = Ox.getObjectById(oml.config.itemKeys, id).title;
if (data[id.key] && !Ox.isEmpty(data[id.key])) {
title = Ox.getObjectById(oml.config.itemKeys, id.key).title;
$('<div>')
.css({
marginTop: (index == 0 ? 10 : 6) + 'px',
@ -645,16 +649,17 @@ oml.ui.infoView = function(identifyData) {
})
.text(title)
.appendTo($data);
Ox.makeArray(data[id]/*FIXME!*/).forEach(function(value) {
var isPrimary = data.primaryid[0] == id
Ox.makeArray(data[id.key]/*FIXME!*/).forEach(function(value) {
var isPrimary = data.primaryid[0] == id.key
&& data.primaryid[1] == value;
value = Ox.encodeHTMLEntities(value);
Ox.Element({
tooltip: isPrimary ? 'Primary ID' : ''
})
.html(
Ox.encodeHTMLEntities(value) + (
isPrimary ? ' (*)' : ''
)
'<a href="' + Ox.formatString(id.url, [value])
+ '" target="_blank">' + value + '</a>'
+ (isPrimary ? ' (*)' : '')
)
.appendTo($data);
});

View File

@ -113,7 +113,10 @@ oml.ui.list = function() {
oml.UI.set({listSelection: data.ids});
},
oml_find: function() {
that.reloadList();
if (ui.listView == oml.UI.getPrevious().listView) {
Ox.print('NEW FIND, SAME LIST VIEW, RELOADING')
that.reloadList();
}
},
oml_item: function() {
if (!ui.item) {
@ -123,10 +126,16 @@ oml.ui.list = function() {
}
},
oml_listselection: function(data) {
that.options({selected: data.value});
if (ui._list == oml.UI.getPrevious()._list) {
that.options({selected: data.value});
}
},
oml_listsort: function(data) {
that.options({sort: data.value});
if (ui._list == oml.UI.getPrevious()._list) {
Ox.print('NEW LIST SORT, SAME LIST, SETTING SORT OPTION')
Ox.print('OLD:', that.options('sort'), 'NEW:', data.value);
that.options({sort: data.value});
}
},
oml_sidebarsize: function(data) {
that.size();

View File

@ -28,6 +28,7 @@ oml.ui.listInnerPanel = function() {
})
.bindEvent({
oml_listview: function() {
Ox.print('LIST VIEW HAS CHANGED, REPLACING LIST')
that.replaceElement(1, oml.$ui.list = oml.ui.list());
},
oml_showfilters: function(data) {

View File

@ -21,9 +21,16 @@ oml.ui.listViewButtons = function() {
.bindEvent({
change: function(data) {
oml.UI.set({listView: data.value});
},
oml_listview: function() {
that.updateElement();
}
});
that.updateElement = function() {
return that.options({value: ui.listView});
};
return that;
};

View File

@ -221,6 +221,15 @@ oml.ui.mainMenu = function() {
title: Ox._('Advanced Sort'),
keyboard: 'shift control s',
disabled: true
},
{},
{
id: 'sorttitles',
title: Ox._('Sort Titles...')
},
{
id: 'sortnames',
title: Ox._('Sort Names...')
}
]
},

View File

@ -11,7 +11,6 @@ oml.ui.preferencesDialog = function() {
id: 'username',
title: 'Username',
value: preferences.username,
placeholder: 'anonymous',
help: 'Your username doesn\'t have to be your real name, and you can change it at any time. You can also leave it blank, in which case you will appear as "anonymous". Any characters other than colons and leading, trailing or consecutive spaces are okay.'
},
{
@ -430,7 +429,7 @@ oml.ui.preferencesDialog = function() {
var key = data.id,
value = data.data.value[0];
if (key == 'username') {
value = getValidName(value, [], ':');
value = oml.getValidName(value, [], ':');
}
if (key in oml.config.user.preferences) {
oml.Preferences.set(key, value);

View File

@ -238,7 +238,7 @@ oml.ui.usersDialog = function() {
disabled: true,
label: Ox._('Download Link'),
labelWidth: 128,
value: 'https://openmedialibrary.com/#download',
value: 'http://openmedialibrary.com/#download',
width: 480
})
.css({
@ -260,27 +260,16 @@ oml.ui.usersDialog = function() {
Ox.Input({
label: Ox._('Nickname'),
labelWidth: 128,
placeholder: 'anonymous',
value: user.nickname || user.username || '',
value: user.nickname,
width: 480
})
.bindEvent({
change: function(data) {
var value = oml.getValidName(
data.value || 'anonymous',
// FIXME: WRONG
users.filter(function(user_) {
return user_.nickname != user.nickname;
}).map(function(u) {
return user_.nickname;
}),
':'
);
this.value(value);
oml.api.editUser({
id: user.id,
nickname: value
}, function() {
nickname: data.value
}, function(result) {
Ox.print('EDIT USER', result.data);
// ...
});
}
@ -331,8 +320,7 @@ oml.ui.usersDialog = function() {
disabled: true,
label: Ox._('Username'),
labelWidth: 128,
placeholder: 'anonymous',
value: user.username || '',
value: user.username,
width: 480
})
.css({
@ -474,11 +462,9 @@ oml.ui.usersDialog = function() {
},
{
format: function(value) {
return value
? Ox.encodeHTMLEntities(value)
: '<span class="OxLight">anonymous</span>';
return Ox.encodeHTMLEntities(value);
},
id: folder.id == 'peers' ? 'nickname' : 'username',
id: 'name',
visible: true,
width: 240
}
@ -533,14 +519,16 @@ oml.ui.usersDialog = function() {
}
function updateUsers(callback) {
Ox.Request.clearCache('getUsers');
oml.api.getUsers(function(result) {
users = result.data.users;
peerIds = users.filter(function(user) {
return user.peered;
}).map(function(user) {
return user.id
return user.id;
});
folders.forEach(function(folder) {
folder.items = [];
@ -595,14 +583,13 @@ oml.ui.usersDialog = function() {
});
}
that.updateElement = function() {
that.updateElement = function() {
that.options({
content: Ox.LoadingScreen().start()
});
updateUsers();
return that;
};
return that.updateElement();

View File

@ -800,7 +800,7 @@ oml.getLists = function(callback) {
: list.type == 'library' ? Ox._('Library') : list.name;
return Ox.extend(list, {
editable: list.user == username && list.type == 'static',
own: list.user == username,
own: list.user == '',
title: (list.user ? list.user + ': ' : '') + list.name
});
});
@ -836,71 +836,22 @@ oml.getSortOperator = function(key) {
};
oml.getUsers = function(callback) {
var users = [{
id: oml.user.id,
nickname: oml.user.preferences.username,
online: oml.user.online
}];
oml.api.getUsers(function(result) {
users = users.concat(
result.data.users.filter(function(user) {
return user.peered;
})
);
callback(users);
});
}
oml.getUsersAndLists = function(callback) {
var lists = [{
id: '',
name: Ox._('All Libraries'),
type: 'libraries'
}],
ui = oml.user.ui,
username = oml.user.preferences.username,
var ui = oml.user.ui,
users = [{
id: oml.user.id,
nickname: username,
name: '',
online: oml.user.online
}];
Ox.Request.clearCache('getUsers');
Ox.Request.clearCache('getLists');
oml.api.getUsers(function(result) {
users = users.concat(
result.data.users.filter(function(user) {
return user.peered;
})
);
oml.api.getLists(function(result) {
users.forEach(function(user) {
lists = lists.concat([{
id: (user.nickname == username ? '' : user.nickname) + ':',
name: Ox._('Library'),
type: 'library',
user: user.nickname
}].concat(
result.data.lists.filter(function(list) {
return list.user == user.nickname;
})
));
});
lists = lists.map(function(list) {
// FIXME: 'editable' is notoriously vague
return Ox.extend(list, {
editable: list.user == username && list.type == 'static',
own: list.user == username,
title: (list.user ? list.user + ': ' : '') + list.name
});
})
if (!ui.lists) {
oml.$ui.mainMenu.updateElement();
}
ui._lists = lists;
Ox.print('GOT LISTS ::::', lists);
callback(users, lists);
});
})
ui._users = users;
callback(users);
});
};
oml.getValidName = function(value, names, chars) {

View File

@ -6,25 +6,25 @@ oml.ui.viewer = function() {
that = Ox.Element()
.bindEvent({
oml_item: function(data) {
that.updateElement();
},
oml_itemview: function(data) {
that.updateElement();
if (ui.item != item && ui.itemView == 'book') {
that.updateElement(ui.item);
}
}
}),
$iframe;
$iframe, item;
that.updateElement = function() {
if (ui.item && ui.itemView == 'book') {
item = ui.item;
if (item) {
$iframe = $iframe || Ox.Element('<iframe>').css({
width: '100%',
height: '100%',
border: 0
}).appendTo(that);
$iframe.attr({
src: '/' + ui.item + '/reader/'
src: '/' + item + '/reader/'
});
}
return that;