diff --git a/oml/changelog.py b/oml/changelog.py index 6786454..f79cd42 100644 --- a/oml/changelog.py +++ b/oml/changelog.py @@ -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 diff --git a/oml/setup.py b/oml/setup.py index 37528b5..6965293 100644 --- a/oml/setup.py +++ b/oml/setup.py @@ -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: diff --git a/oml/user/api.py b/oml/user/api.py index 49dba9e..23baecd 100644 --- a/oml/user/api.py +++ b/oml/user/api.py @@ -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) diff --git a/oml/user/models.py b/oml/user/models.py index 68cc576..f21e33c 100644 --- a/oml/user/models.py +++ b/oml/user/models.py @@ -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(), diff --git a/static/js/UI.js b/static/js/UI.js index a1be17b..e3c96ab 100644 --- a/static/js/UI.js +++ b/static/js/UI.js @@ -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 diff --git a/static/js/folders.js b/static/js/folders.js index 3629f26..2567111 100644 --- a/static/js/folders.js +++ b/static/js/folders.js @@ -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'}); diff --git a/static/js/infoView.js b/static/js/infoView.js index 4c1d5ac..9a035a4 100644 --- a/static/js/infoView.js +++ b/static/js/infoView.js @@ -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: '' + Ox.getObjectById( - oml.config.itemKeys, key + oml.config.itemKeys, id.key ).title + ': ' + 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; $('