django_lsd
This commit is contained in:
commit
dceba28d9f
6 changed files with 198 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
*.pyc
|
||||||
|
*.swp
|
||||||
|
MANIFEST
|
||||||
|
build
|
30
README
Normal file
30
README
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
# django-lsd
|
||||||
|
integrate LSD into your django app
|
||||||
|
|
||||||
|
# settings.py
|
||||||
|
|
||||||
|
in settings set LSD_ITEM to your item model i.e.:
|
||||||
|
|
||||||
|
LDS_ITEM="item.models.Item"
|
||||||
|
|
||||||
|
and add "django_lsd" to INSTALLED_APPS
|
||||||
|
|
||||||
|
|
||||||
|
# add this to urls.py
|
||||||
|
|
||||||
|
import ox.django.api.urls
|
||||||
|
|
||||||
|
urlpatterns += patterns('',
|
||||||
|
(r'^api/?$', include(ox.django.api.urls)),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# custom models
|
||||||
|
|
||||||
|
If you set a custom model via LSD_ITEMS,
|
||||||
|
the referenced model needs the following rows:
|
||||||
|
|
||||||
|
sha1 = models.CharField(max_length=40, unique=True)
|
||||||
|
info_hash = models.CharField(max_length=40, unique=True, null=True)
|
||||||
|
data = ox.django.fields.DictField(default={}, editable=False)
|
||||||
|
|
0
django_lsd/__init__.py
Normal file
0
django_lsd/__init__.py
Normal file
57
django_lsd/models.py
Normal file
57
django_lsd/models.py
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
from __future__ import division
|
||||||
|
import sys
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import models
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from ox.django import fields
|
||||||
|
|
||||||
|
def str_to_class(str):
|
||||||
|
if '.' in str:
|
||||||
|
module_name = '.'.join(str.split('.')[:-1])
|
||||||
|
class_name = str.split('.')[-1]
|
||||||
|
m = importlib.import_module(module_name)
|
||||||
|
return getattr(m, class_name)
|
||||||
|
else:
|
||||||
|
return reduce(getattr, str.split("."), sys.modules[__name__])
|
||||||
|
|
||||||
|
class LSDItem(models.Model):
|
||||||
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
|
modified = models.DateTimeField(auto_now=True)
|
||||||
|
accessed = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
sha1 = models.CharField(max_length=40, unique=True)
|
||||||
|
info_hash = models.CharField(max_length=40, unique=True, null=True)
|
||||||
|
|
||||||
|
data = fields.DictField(default={}, editable=False)
|
||||||
|
|
||||||
|
Item = str_to_class(getattr(settings, 'LSD_ITEM', 'LSDItem'))
|
||||||
|
|
||||||
|
class Request(models.Model):
|
||||||
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
|
modified = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
item = models.ForeignKey(Item, related_name='lsd_requests', db_index=True)
|
||||||
|
user = models.ForeignKey(User, related_name='lsd_requests', db_index=True)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(cls, user):
|
||||||
|
return [f['sha1'] for f in Item.objects.filter(
|
||||||
|
lsd_files__in=File.objects.filter(user=user),
|
||||||
|
lsd_requests__in=cls.objects.exclude(user=user)
|
||||||
|
).values('sha1')]
|
||||||
|
|
||||||
|
class File(models.Model):
|
||||||
|
|
||||||
|
item = models.ForeignKey(Item, related_name='lsd_files', db_index=True)
|
||||||
|
user = models.ForeignKey(User, related_name='lsd_files', db_index=True)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get(cls, user):
|
||||||
|
return [f['sha1'] for f in Item.objects.filter(
|
||||||
|
lsd_files__in=cls.objects.filter(user=user)
|
||||||
|
).values('sha1')]
|
||||||
|
|
86
django_lsd/views.py
Normal file
86
django_lsd/views.py
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
from __future__ import division
|
||||||
|
import json
|
||||||
|
|
||||||
|
from ox.django.decorators import login_required_json
|
||||||
|
from ox.django.shortcuts import render_to_json_response, json_response
|
||||||
|
|
||||||
|
import models
|
||||||
|
|
||||||
|
from ox.django.api import actions
|
||||||
|
|
||||||
|
@login_required_json
|
||||||
|
def sync(request):
|
||||||
|
'''
|
||||||
|
sync({}) -> {ids: [id]}
|
||||||
|
sync({add: [id], remove: [id]}) -> {update: [id]}
|
||||||
|
sync({update: [metadata]}) -> {}
|
||||||
|
'''
|
||||||
|
response = json_response()
|
||||||
|
if 'data' in request.POST:
|
||||||
|
data = json.loads(request.POST['data'])
|
||||||
|
else:
|
||||||
|
data = {}
|
||||||
|
if not data:
|
||||||
|
response['data']['ids'] = models.File.get(request.user)
|
||||||
|
update = []
|
||||||
|
if 'add' in data:
|
||||||
|
for sha1 in data['add']:
|
||||||
|
i, created = models.Item.objects.get_or_create(sha1=sha1)
|
||||||
|
if created:
|
||||||
|
update.append(sha1)
|
||||||
|
f, created = models.File.objects.get_or_create(item=i, user=request.user)
|
||||||
|
models.Request.objects.filter(user=request.user, item=i).delete()
|
||||||
|
if 'remove' in data:
|
||||||
|
models.File.objects.filter(item__sha1__in=data['remove'], user=request.user).delete()
|
||||||
|
if 'update' in data:
|
||||||
|
for f in data['update']:
|
||||||
|
i, created = models.Item.objects.get_or_create(sha1=f['sha1'])
|
||||||
|
i.info_hash = f['info_hash']
|
||||||
|
for key in f.keys():
|
||||||
|
if key not in ('sha1', 'info_hash'):
|
||||||
|
i.data[key] = f[key]
|
||||||
|
#i.update_external_data()
|
||||||
|
i.save()
|
||||||
|
response['data']['update'] = update
|
||||||
|
return render_to_json_response(response)
|
||||||
|
actions.register(sync)
|
||||||
|
|
||||||
|
@login_required_json
|
||||||
|
def addRequests(request):
|
||||||
|
'''
|
||||||
|
addRequests({ids: [id]}) -> {metadata: [metadata]}
|
||||||
|
'''
|
||||||
|
response = json_response()
|
||||||
|
data = json.loads(request.POST['data'])
|
||||||
|
metadata = []
|
||||||
|
for item in models.Item.objects.filter(sha1__in=data['ids']):
|
||||||
|
info_hash = item.info_hash
|
||||||
|
if info_hash and not request.user.lsd_files.filter(item=item).exists():
|
||||||
|
r, created = models.Request.objects.get_or_create(user=request.user, item=item)
|
||||||
|
metadata.append({'sha1': item.sha1, 'info_hash': info_hash})
|
||||||
|
response['data']['metadata'] = metadata
|
||||||
|
return render_to_json_response(response)
|
||||||
|
actions.register(addRequests)
|
||||||
|
|
||||||
|
@login_required_json
|
||||||
|
def removeRequests(request):
|
||||||
|
'''
|
||||||
|
removeRequests({ids: [id]}) -> {}
|
||||||
|
'''
|
||||||
|
response = json_response()
|
||||||
|
data = json.loads(request.POST['data'])
|
||||||
|
models.Request.objects.filter(user=request.user, item__sha1__in=data['ids']).delete()
|
||||||
|
return render_to_json_response(response)
|
||||||
|
actions.register(removeRequests)
|
||||||
|
|
||||||
|
@login_required_json
|
||||||
|
def getRequests(request):
|
||||||
|
'''
|
||||||
|
getRequests({}) -> {ids: [id]}
|
||||||
|
'''
|
||||||
|
response = json_response()
|
||||||
|
response['data']['ids'] = models.Request.get(request.user)
|
||||||
|
return render_to_json_response(response)
|
||||||
|
actions.register(getRequests)
|
21
setup.py
Normal file
21
setup.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# setup.py
|
||||||
|
# -*- coding: UTF-8 -*-
|
||||||
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
from distutils.core import setup
|
||||||
|
|
||||||
|
setup(name="django_lsd",
|
||||||
|
version="0.1",
|
||||||
|
packages=[
|
||||||
|
'django_lsd',
|
||||||
|
],
|
||||||
|
install_requires=['ox'],
|
||||||
|
author="j",
|
||||||
|
author_email="j@mailb.org",
|
||||||
|
description=u"django_lsd integrates LSD into django",
|
||||||
|
classifiers = [
|
||||||
|
'Development Status :: 4 - Beta',
|
||||||
|
'Operating System :: OS Independent',
|
||||||
|
'Programming Language :: Python',
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue