forked from 0x2620/pandora
add itemlist
This commit is contained in:
parent
43faea6028
commit
03832454fe
4 changed files with 223 additions and 0 deletions
0
pandora/itemlist/__init__.py
Normal file
0
pandora/itemlist/__init__.py
Normal file
66
pandora/itemlist/models.py
Normal file
66
pandora/itemlist/models.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
|
||||
from datetime import datetime
|
||||
import os.path
|
||||
import math
|
||||
import random
|
||||
import re
|
||||
import subprocess
|
||||
import unicodedata
|
||||
from glob import glob
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.files.base import ContentFile
|
||||
from django.utils import simplejson as json
|
||||
from django.conf import settings
|
||||
|
||||
from ox.django import fields
|
||||
import ox
|
||||
from ox import stripTags
|
||||
from ox.normalize import canonicalTitle, canonicalName
|
||||
|
||||
|
||||
class List(models.Model):
|
||||
class Meta:
|
||||
unique_together = ("user", "name")
|
||||
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
modified = models.DateTimeField(auto_now=True)
|
||||
user = models.ForeignKey(User)
|
||||
name = models.CharField(max_length=255)
|
||||
public = models.BooleanField(default=False)
|
||||
items = models.ManyToManyField('item.Item', related_name='lists', through='ListItem')
|
||||
|
||||
def add(self, item):
|
||||
q = self.items.filter(id=item.id)
|
||||
if q.count() == 0:
|
||||
l = ListItem()
|
||||
l.list = self
|
||||
l.item = item
|
||||
l.save()
|
||||
|
||||
def remove(self, item):
|
||||
self.ListItem.objects.all().filter(item=item, list=self).delete()
|
||||
|
||||
def __unicode__(self):
|
||||
return u'%s (%s)' % (self.title, unicode(self.user))
|
||||
|
||||
def editable(self, user):
|
||||
#FIXME: make permissions work
|
||||
if self.user == user or user.has_perm('Ox.admin'):
|
||||
return True
|
||||
return False
|
||||
|
||||
class ListItem(models.Model):
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
modified = models.DateTimeField(auto_now=True)
|
||||
list = models.ForeignKey(List)
|
||||
item = models.ForeignKey('item.Item')
|
||||
|
||||
def __unicode__(self):
|
||||
return u'%s in %s' % (unicode(self.item), unicode(self.list))
|
||||
|
23
pandora/itemlist/tests.py
Normal file
23
pandora/itemlist/tests.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
"""
|
||||
This file demonstrates two different styles of tests (one doctest and one
|
||||
unittest). These will both pass when you run "manage.py test".
|
||||
|
||||
Replace these with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.failUnlessEqual(1 + 1, 2)
|
||||
|
||||
__test__ = {"doctest": """
|
||||
Another way to test that 1 + 1 is equal to 2.
|
||||
|
||||
>>> 1 + 1 == 2
|
||||
True
|
||||
"""}
|
||||
|
134
pandora/itemlist/views.py
Normal file
134
pandora/itemlist/views.py
Normal file
|
@ -0,0 +1,134 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division
|
||||
import os.path
|
||||
import re
|
||||
from datetime import datetime
|
||||
from urllib2 import unquote
|
||||
import mimetypes
|
||||
|
||||
from django import forms
|
||||
from django.core.paginator import Paginator
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import Q, Avg, Count, Sum
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404, redirect
|
||||
from django.template import RequestContext
|
||||
from django.conf import settings
|
||||
|
||||
from ox.utils import 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.http import HttpFileResponse
|
||||
import ox
|
||||
|
||||
|
||||
'''
|
||||
List API
|
||||
'''
|
||||
@login_required_json
|
||||
def api_addListItem(request):
|
||||
'''
|
||||
param data
|
||||
{list: listId,
|
||||
item: itemId,
|
||||
quert: ...
|
||||
}
|
||||
return {'status': {'code': int, 'text': string},
|
||||
'data': {}}
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
list = get_object_or_404_json(models.List, pk=data['list'])
|
||||
if 'item' in data:
|
||||
item = get_object_or_404_json(models.Item, pk=data['item'])
|
||||
if list.editable(request.user):
|
||||
list.add(item)
|
||||
response = json_response(status=200, text='item removed')
|
||||
else:
|
||||
response = json_response(status=403, text='not allowed')
|
||||
elif 'query' in data:
|
||||
response = json_response(status=501, text='not implemented')
|
||||
|
||||
else:
|
||||
response = json_response(status=501, text='not implemented')
|
||||
return render_to_json_response(response)
|
||||
|
||||
@login_required_json
|
||||
def api_removeListItem(request):
|
||||
'''
|
||||
param data
|
||||
{list: listId,
|
||||
item: itemId,
|
||||
quert: ...
|
||||
}
|
||||
return {'status': {'code': int, 'text': string},
|
||||
'data': {}}
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
list = get_object_or_404_json(models.List, pk=data['list'])
|
||||
if 'item' in data:
|
||||
item = get_object_or_404_json(models.Item, pk=data['item'])
|
||||
if list.editable(request.user):
|
||||
list.remove(item)
|
||||
response = json_response(status=200, text='item removed')
|
||||
else:
|
||||
response = json_response(status=403, text='not allowed')
|
||||
elif 'query' in data:
|
||||
response = json_response(status=501, text='not implemented')
|
||||
|
||||
else:
|
||||
response = json_response(status=501, text='not implemented')
|
||||
return render_to_json_response(response)
|
||||
|
||||
@login_required_json
|
||||
def api_addList(request):
|
||||
'''
|
||||
param data
|
||||
{name: value}
|
||||
return {'status': {'code': int, 'text': string},
|
||||
'data': {}}
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
if models.List.filter(name=data['name'], user=request.user).count() == 0:
|
||||
list = models.List(name = data['name'], user=request.user)
|
||||
list.save()
|
||||
response = json_response(status=200, text='created')
|
||||
else:
|
||||
response = json_response(status=403, text='list name exists')
|
||||
return render_to_json_response(response)
|
||||
|
||||
@login_required_json
|
||||
def api_editList(request):
|
||||
'''
|
||||
param data
|
||||
{key: value}
|
||||
keys: name, public
|
||||
return {'status': {'code': int, 'text': string},
|
||||
'data': {}}
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
list = get_object_or_404_json(models.List, pk=data['list'])
|
||||
if list.editable(request.user):
|
||||
for key in data:
|
||||
if key in ('name', 'public'):
|
||||
setattr(list, key, data['key'])
|
||||
else:
|
||||
response = json_response(status=403, text='not allowed')
|
||||
return render_to_json_response(response)
|
||||
|
||||
def api_removeList(request):
|
||||
'''
|
||||
param data
|
||||
{key: value}
|
||||
return {'status': {'code': int, 'text': string},
|
||||
'data': {}}
|
||||
'''
|
||||
data = json.loads(request.POST['data'])
|
||||
list = get_object_or_404_json(models.List, pk=data['list'])
|
||||
if list.editable(request.user):
|
||||
list.delete()
|
||||
else:
|
||||
response = json_response(status=403, text='not allowed')
|
||||
return render_to_json_response(response)
|
||||
|
Loading…
Reference in a new issue