sort lists/peers, rename peers

This commit is contained in:
j 2014-05-25 20:06:12 +02:00
parent eac91e7a3a
commit 8ddf228068
9 changed files with 141 additions and 26 deletions

View file

@ -210,12 +210,12 @@ class Changelog(db.Model):
def action_orderlists(self, user, timestamp, lists):
from user.models import List
position = 0
idx = 0
for name in lists:
l = List.get_or_create(user.id, name)
l.position = position
l.index_ = idx
l.save()
position += 1
idx += 1
return True
def action_removelist(self, user, timestamp, name):
@ -239,8 +239,12 @@ class Changelog(db.Model):
return True
def action_editusername(self, user, timestamp, username):
from user.models import List
old = user.nickname
user.info['username'] = username
user.update_name()
if old != user.nickname:
List.rename_user(old, user.nickname)
user.save()
return True

View file

@ -84,6 +84,20 @@ class PostUpdate(Command):
if old <= '20140521-65-e14c686' and new > '20140521-65-e14c686':
if not os.path.exists(settings.db_path):
r('./ctl', 'setup')
if old <= '20140525-92-eac91e7' and new > '20140525-92-eac91e7':
import user.models
for u in user.models.User.query:
u.update_name()
u.save()
import item.models
for f in item.models.File.query:
changed = False
for key in ('mediastate', 'coverRatio', 'previewRatio'):
if key in f.info:
del f.info[key]
changed = True
if changed:
f.save()
class Setup(Command):
"""

View file

@ -242,8 +242,9 @@ class Item(db.Model):
record = {}
for key in self.meta_keys:
if key in data:
if self.meta.get(key) != data[key]:
record[key] = data[key]
self.meta[key] = data[key]
record[key] = data[key]
update = True
for key in self.meta.keys():
if key not in self.meta_keys:
@ -254,7 +255,7 @@ class Item(db.Model):
self.modified = datetime.utcnow()
self.save()
user = state.user()
if user in self.users:
if record and user in self.users:
Changelog.record(user, 'edititem', self.id, record)
def update_primaryid(self, key=None, id=None):
@ -359,7 +360,11 @@ class Item(db.Model):
return False
if not f:
path = 'Downloads/%s.%s' % (self.id, self.info['extension'])
f = File.get_or_create(self.id, self.info, path=path)
info = self.info.copy()
for key in ('mediastate', 'coverRatio', 'previewRatio'):
if key in info:
del info[key]
f = File.get_or_create(self.id, info, path=path)
path = self.get_path()
if not os.path.exists(path):
ox.makedirs(os.path.dirname(path))
@ -371,7 +376,7 @@ class Item(db.Model):
t.progress = 1
t.save()
self.added = datetime.utcnow()
Changelog.record(u, 'additem', self.id, self.info)
Changelog.record(u, 'additem', self.id, f.info)
self.update()
f.move()
self.update_icons()

View file

@ -53,7 +53,7 @@ def add_file(id, f, prefix):
item.meta['primaryid'] = item.info.pop('primaryid')
db.session.add(item)
item.users.append(user)
Changelog.record(user, 'additem', item.id, item.info)
Changelog.record(user, 'additem', item.id, f.info)
if item.meta.get('primaryid'):
Changelog.record(user, 'edititem', item.id, dict([item.meta['primaryid']]))
item.added = datetime.utcnow()

View file

@ -7,7 +7,7 @@ from copy import deepcopy
import json
from oxtornado import actions
import ox
import models
@ -87,6 +87,7 @@ def getUsers(data):
users = []
for u in models.User.query.filter(models.User.id!=settings.USER_ID).all():
users.append(u.json())
users.sort(key=lambda u: ox.sort_string(str(u.get('index', '')) + 'Z' + (u.get('name') or '')))
return {
"users": users
}
@ -229,13 +230,17 @@ def sortLists(data):
'''
n = 0
logger.debug('sortLists %s', data)
lists = []
for id in data['ids']:
l = models.List.get(id)
l.position = n
l.index_ = n
n += 1
if l.type == 'static':
lists.append(l.name)
models.db.session.add(l)
models.db.session.commit()
logger.debug('FIXME: broadcast orderlist event here')
if lists:
Changelog.record(state.user(), 'orderlists', lists)
return {}
actions.register(sortLists, cache=False)
@ -253,12 +258,31 @@ def editUser(data):
p.info['nickname'] = data['nickname']
elif 'nickname' in p.info:
del p.info['nickname']
old = p.nickname
p.update_name()
if old != p.nickname:
models.List.rename_user(old, p.nickname)
p.save()
return p.json()
return {}
actions.register(editUser, cache=False)
def sortUsers(data):
'''
takes {
ids
}
'''
n = 0
logger.debug('sortUsers %s', data)
for id in data['ids']:
u = models.User.get(id)
u.info['index'] = n
n += 1
models.db.session.add(u)
models.db.session.commit()
return {}
actions.register(sortUsers, cache=False)
def requestPeering(data):
'''

View file

@ -183,13 +183,30 @@ class List(db.Model):
l.type = 'smart' if l._query else 'static'
l.index_ = cls.query.filter_by(user_id=user_id).count()
if user_id == settings.USER_ID:
p = User.get(settings.USER_ID)
if not l._query:
Changelog.record(p, 'addlist', l.name)
Changelog.record(state.user(), 'addlist', l.name)
db.session.add(l)
db.session.commit()
return l
@classmethod
def rename_user(cls, old, new):
for l in cls.query.filter(cls._query!=None):
def update_conditions(conditions):
changed = False
for c in conditions:
if 'conditions' in c:
changed = update_conditions(conditions)
else:
if c.get('key') == 'list' and c.get('value', '').startswith('%s:' % old):
c['value'] = '%s:%s' % new, c['value'].split(':', 1)[1]
changed = True
return changed
if update_conditions(l._query.get('conditions', [])):
l.save()
def add_items(self, items):
from item.models import Item
for item_id in items:

View file

@ -306,8 +306,23 @@ oml.ui.folders = function() {
});
};
that.updateUser = function(index) {
oml.$ui.folder[index].options({title: ui._users[index].name})
that.updateUser = function(index, callback) {
var name = ui._users[index].name;
oml.$ui.folder[index].options({title: Ox.encodeHTMLEntities(name)});
oml.getLists(function(lists) {
var items = lists.filter(function(list) {
return list.user == name && list.type != 'library';
});
oml.$ui.folder[index].$content
.css({height: 16 + items.length * 16 + 'px'});
oml.$ui.folderList[index].options({
items: items
})
.css({height: items.length * 16 + 'px'})
.size();
oml.resizeListFolders();
callback && callback();
});
return that;
};

View file

@ -493,6 +493,7 @@ oml.ui.usersDialog = function() {
ids: data.ids
}, function(result) {
Ox.print('USER ORDER CHANGED', result.data);
oml.$ui.folders.updateElement();
});
},
select: function(data) {

View file

@ -835,18 +835,17 @@ oml.getSortOperator = function(key) {
};
oml.getUsers = function(callback) {
var ui = oml.user.ui,
var ui = oml.user.ui;
Ox.Request.clearCache('getUsers');
oml.api.getUsers(function(result) {
users = [{
id: oml.user.id,
name: '',
online: oml.user.online
}];
Ox.Request.clearCache('getUsers');
oml.api.getUsers(function(result) {
users = users.concat(
result.data.users.filter(function(user) {
}].concat(
Ox.sortBy(result.data.users.filter(function(user) {
return user.peered;
})
}), 'index')
);
ui._users = users;
callback(users);
@ -890,26 +889,62 @@ oml.reloadList = function() {
};
oml.renameUser = function(data) {
var ui = oml.user.ui,
index = Ox.getIndexById(ui._users, data.id),
name = ui._users[index].name,
set = {};
set = {},
oldFind = Ox.clone(ui.find, true),
newFind = Ox.clone(ui.find, true);
ui._users[index].name = data.name;
ui._users[index].nickname = data.nickname;
oml.$ui.folders.updateUser(index);
set['showFolder.' + data.name] = ui.showFolder[name];
set['showFolder.' + name] = null;
set['showFolder.' + data.name] = ui.showFolder[name];
Ox.forEach(ui.lists, function(value, key) {
var split = key.split(':'),
username = split[0],
listname = split.slice(1).join(':');
if (username == name) {
set['lists.' + data.name + ':' + listname] = value;
set['lists.' + key] = null;
set['lists.' + data.name + ':' + listname] = value;
}
});
ui._lists.filter(function(list) {
return list.user === '' && list.type == 'smart';
}).forEach(function(list) {
updateConditions(list.query);
});
Ox.print('$$$ SET', set);
oml.UI.set(set, false);
updateConditions(newFind);
if (!Ox.isEqual(oldFind, newFind)) {
oml.replaceURL = true;
oml.UI.set({find: newFind}, false);
}
oml.$ui.folders.updateUser(index);
function updateCondition(condition) {
if (condition.key == 'list') {
condition.value = condition.value.replace(
new RegExp('^' + Ox.escapeRegExp(name) + ':'),
data.name + ':'
);
}
}
function updateConditions(query) {
query.conditions.forEach(function(condition) {
if (!condition.conditions) {
updateCondition(condition);
} else {
condition.conditions.forEach(updateCondition);
}
});
}
};
oml.resizeFilters = function() {