fix list info for nonexisting lists

This commit is contained in:
rolux 2011-10-22 16:07:05 +00:00
parent 4266c4578b
commit cca3b8b5b7
4 changed files with 81 additions and 48 deletions

View file

@ -10,7 +10,7 @@ import models
from ox.django.query import QuerySet from ox.django.query import QuerySet
def parseCondition(condition): def parseCondition(condition, user):
''' '''
condition: { condition: {
value: "war" value: "war"
@ -38,8 +38,8 @@ def parseCondition(condition):
exclude = False exclude = False
if isinstance(v, list): if isinstance(v, list):
q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}) \ q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \
& parseCondition({'key': k, 'value': v[1], 'operator': '<'}) & parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user)
if exclude: if exclude:
return ~q return ~q
else: else:
@ -109,14 +109,17 @@ def parseCondition(condition):
if len(l) >= 2: if len(l) >= 2:
l = (l[0], ":".join(l[1:])) l = (l[0], ":".join(l[1:]))
lqs = list(List.objects.filter(name=l[1], user__username=l[0])) lqs = list(List.objects.filter(name=l[1], user__username=l[0]))
if len(lqs) == 1: if len(lqs) == 1 and lqs[0].accessible(user):
l = lqs[0] l = lqs[0]
if l.query.get('static', False) == False: if l.query.get('static', False) == False:
data = l.query data = l.query
q = parseConditions(data.get('conditions', []), q = parseConditions(data.get('conditions', []),
data.get('operator', '&')) data.get('operator', '&'),
else: user)
q = Q(id__in=l.items.all()) else:
q = Q(id__in=l.items.all())
else:
q = Q(id=0)
return q return q
else: #number or date else: #number or date
@ -146,7 +149,7 @@ def parseCondition(condition):
return Q(**{'find__key': k, vk: v}) return Q(**{'find__key': k, vk: v})
def parseConditions(conditions, operator): def parseConditions(conditions, operator, user):
''' '''
conditions: [ conditions: [
{ {
@ -169,12 +172,12 @@ def parseConditions(conditions, operator):
for condition in conditions: for condition in conditions:
if 'conditions' in condition: if 'conditions' in condition:
q = parseConditions(condition['conditions'], q = parseConditions(condition['conditions'],
condition.get('operator', '&')) condition.get('operator', '&'), user)
if q: if q:
conn.append(q) conn.append(q)
pass pass
else: else:
conn.append(parseCondition(condition)) conn.append(parseCondition(condition, user))
if conn: if conn:
q = conn[0] q = conn[0]
for c in conn[1:]: for c in conn[1:]:
@ -209,7 +212,8 @@ class ItemManager(Manager):
if lqs[0].query: if lqs[0].query:
data = lqs[0].query data = lqs[0].query
conditions = parseConditions(data['query']['conditions'], conditions = parseConditions(data['query']['conditions'],
data['query'].get('operator', '&')) data['query'].get('operator', '&'),
user)
qs = qs.filter(conditions) qs = qs.filter(conditions)
else: else:
qs = qs.filter(id__in=lqs[0].items.all()) qs = qs.filter(id__in=lqs[0].items.all())
@ -241,7 +245,8 @@ class ItemManager(Manager):
qs = self.get_query_set() qs = self.get_query_set()
#only include items that have hard metadata #only include items that have hard metadata
conditions = parseConditions(data.get('query', {}).get('conditions', []), conditions = parseConditions(data.get('query', {}).get('conditions', []),
data.get('query', {}).get('operator', '&')) data.get('query', {}).get('operator', '&'),
user)
qs = qs.filter(conditions) qs = qs.filter(conditions)
qs = qs.distinct() qs = qs.distinct()

View file

@ -84,6 +84,9 @@ class List(models.Model):
def get_id(self): def get_id(self):
return u'%s:%s' % (self.user.username, self.name) return u'%s:%s' % (self.user.username, self.name)
def accessible(self, user):
return self.user == user or self.status in ('public', 'featured')
def editable(self, user): def editable(self, user):
#FIXME: make permissions work #FIXME: make permissions work
if self.user == user or user.is_staff: if self.user == user or user.is_staff:

View file

@ -1,14 +1,17 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from __future__ import division from __future__ import division
import os
from django.db.models import Max, Sum from django.db.models import Max, Sum
from django.http import HttpResponseForbidden, Http404 from django.http import HttpResponseForbidden, Http404
from django.conf import settings
from ox.utils import json from ox.utils import json
from ox.django.decorators import login_required_json from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from ox.django.http import HttpFileResponse from ox.django.http import HttpFileResponse
import models import models
from api.actions import actions from api.actions import actions
from item import utils from item import utils
@ -504,8 +507,14 @@ actions.register(sortLists, cache=False)
def icon(request, id, size=16): def icon(request, id, size=16):
if not size: if not size:
size = 16 size = 16
list = get_list_or_404_json(id)
icon = list.get_icon(int(size)) id = id.split(':')
if icon: username = id[0]
return HttpFileResponse(icon, content_type='image/jpeg') listname = ":".join(id[1:])
raise Http404 qs = models.List.objects.filter(user__username=username, name=listname)
if qs.count() == 1 and qs[0].accessible(request.user):
list = qs[0]
icon = list.get_icon(int(size))
else:
icon = os.path.join(settings.STATIC_ROOT, 'jpg/list.jpg')
return HttpFileResponse(icon, content_type='image/jpeg')

View file

@ -54,7 +54,7 @@ pandora.ui.info = function() {
previousView = view; previousView = view;
view = getView(); view = getView();
if (view == 'list') { if (view == 'list') {
that.empty().append(pandora.$ui.listInfo = pandora.ui.listInfo(ui._list)); that.empty().append(pandora.$ui.listInfo = pandora.ui.listInfo());
previousView == 'video' && resizeInfo(); previousView == 'video' && resizeInfo();
} else if (view == 'poster') { } else if (view == 'poster') {
pandora.api.get({id: id, keys: ['director', 'posterRatio', 'title']}, function(result) { pandora.api.get({id: id, keys: ['director', 'posterRatio', 'title']}, function(result) {
@ -121,39 +121,55 @@ pandora.ui.info = function() {
}; };
pandora.ui.listInfo = function(list) { pandora.ui.listInfo = function() {
var that = $('<div>').css({padding: '16px', textAlign: 'center'});
var $icon = $('<img>') var list = pandora.user.ui._list,
.attr({ that = $('<div>').css({padding: '16px', textAlign: 'center'}),
src: list $icon = $('<img>')
? '/list/' + list + '/icon256.jpg?' + Ox.uid() .attr({
: '/static/png/icon256.png' src: list
}) ? '/list/' + list + '/icon256.jpg?' + Ox.uid()
.css(getIconCSS()) : '/static/png/icon256.png'
.appendTo(that), })
title = list ? list.replace(':', ': ') : 'All ' + pandora.site.itemName.plural, .css(getIconCSS())
description = ''; .appendTo(that);
$('<div>').css({padding: '16px 0 16px 0', fontWeight: 'bold'}).html(title).appendTo(that);
//fixme: allow editing //fixme: allow editing
//pandora.api.editList({id: list, description: 'foobbar'}, callback) //pandora.api.editList({id: list, description: 'foobbar'}, callback)
//pandora.api.editPage({name: 'allItems', body: 'foobar'}, callback) //pandora.api.editPage({name: 'allItems', body: 'foobar'}, callback)
if(list) { if (list) {
pandora.api.findLists({ pandora.api.findLists({
query: { conditions: [{key: 'id', value: list, operator:'=='}] }, query: { conditions: [{key: 'id', value: list, operator: '=='}] },
keys:['description'] keys: ['description']
}, function(result) { }, function(result) {
$('<div>').css({textAlign: 'left'}) if (result.data.items.length) {
.html(result.data.items[0].description) that.append(
.appendTo(that); $('<div>')
}); .css({paddingTop: '16px', fontWeight: 'bold'})
} .html(Ox.encodeHTML(list))
else { ).append(
pandora.api.getPage({name: 'allItems'}, function(result) { $('<div>')
$('<div>').css({textAlign: 'left'}) .css({paddingTop: '16px', textAlign: 'left'})
.html(result.data.body) .html(
.appendTo(that); result.data.items.length
? result.data.items[0].description
: 'List not found'
)
);
} else {
that.append(
$('<div>')
.css({paddingTop: '16px',})
.html('List not found')
);
}
}); });
} else {
that.append(
$('<div>')
.css({paddingTop: '16px', fontWeight: 'bold'})
.html('All ' + pandora.site.itemName.plural)
);
} }
function getIconCSS() { function getIconCSS() {