forked from 0x2620/pandora
home backend
This commit is contained in:
parent
63e8756703
commit
c5d961a946
13 changed files with 229 additions and 0 deletions
|
@ -56,6 +56,7 @@
|
|||
"canImportItems": {},
|
||||
"canManageDocuments": {"staff": true, "admin": true},
|
||||
"canManageEntities": {"staff": true, "admin": true},
|
||||
"canManageHome": {"staff": true, "admin": true},
|
||||
"canManagePlacesAndEvents": {"staff": true, "admin": true},
|
||||
"canManageTitlesAndNames": {"staff": true, "admin": true},
|
||||
"canManageUsers": {"staff": true, "admin": true},
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
"canImportItems": {},
|
||||
"canManageDocuments": {"member": true, "researcher": true, "staff": true, "admin": true},
|
||||
"canManageEntities": {"member": true, "researcher": true, "staff": true, "admin": true},
|
||||
"canManageHome": {"staff": true, "admin": true},
|
||||
"canManagePlacesAndEvents": {"member": true, "researcher": true, "staff": true, "admin": true},
|
||||
"canManageTitlesAndNames": {"member": true, "researcher": true, "staff": true, "admin": true},
|
||||
"canManageUsers": {"staff": true, "admin": true},
|
||||
|
|
|
@ -56,6 +56,7 @@
|
|||
"canImportItems": {"member": true, "staff": true, "admin": true},
|
||||
"canManageDocuments": {"member": true, "staff": true, "admin": true},
|
||||
"canManageEntities": {"member": true, "staff": true, "admin": true},
|
||||
"canManageHome": {"staff": true, "admin": true},
|
||||
"canManagePlacesAndEvents": {"member": true, "staff": true, "admin": true},
|
||||
"canManageTitlesAndNames": {"member": true, "staff": true, "admin": true},
|
||||
"canManageUsers": {"staff": true, "admin": true},
|
||||
|
|
|
@ -60,6 +60,7 @@ examples (config.SITENAME.jsonc) that are part of this pan.do/ra distribution.
|
|||
"canImportItems": {"member": true, "staff": true, "admin": true},
|
||||
"canManageDocuments": {"member": true, "staff": true, "admin": true},
|
||||
"canManageEntities": {"member": true, "staff": true, "admin": true},
|
||||
"canManageHome": {"staff": true, "admin": true},
|
||||
"canManagePlacesAndEvents": {"member": true, "staff": true, "admin": true},
|
||||
"canManageTitlesAndNames": {"member": true, "staff": true, "admin": true},
|
||||
"canManageUsers": {"staff": true, "admin": true},
|
||||
|
|
0
pandora/home/__init__.py
Normal file
0
pandora/home/__init__.py
Normal file
3
pandora/home/admin.py
Normal file
3
pandora/home/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
7
pandora/home/apps.py
Normal file
7
pandora/home/apps.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class HomeConfig(AppConfig):
|
||||
name = 'home'
|
28
pandora/home/migrations/0001_initial.py
Normal file
28
pandora/home/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.4 on 2017-01-24 17:22
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import oxdjango.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Item',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('modified', models.DateTimeField(auto_now=True)),
|
||||
('active', models.BooleanField(default=True)),
|
||||
('index', models.IntegerField(default=0)),
|
||||
('data', oxdjango.fields.DictField(default={}, editable=False)),
|
||||
],
|
||||
),
|
||||
]
|
0
pandora/home/migrations/__init__.py
Normal file
0
pandora/home/migrations/__init__.py
Normal file
77
pandora/home/models.py
Normal file
77
pandora/home/models.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, print_function, absolute_import
|
||||
|
||||
from six import string_types
|
||||
from django.db import models
|
||||
from django.db.models import Max
|
||||
import ox
|
||||
|
||||
from oxdjango import fields
|
||||
|
||||
|
||||
class Item(models.Model):
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
modified = models.DateTimeField(auto_now=True)
|
||||
|
||||
active = models.BooleanField(default=True)
|
||||
index = models.IntegerField(default=-1)
|
||||
data = fields.DictField(default={}, editable=False)
|
||||
|
||||
def editable(self, user):
|
||||
return user.is_authenticated() and user.profile.capability("canManageHome")
|
||||
|
||||
def edit(self, data):
|
||||
changed = False
|
||||
for key in (
|
||||
'contentid',
|
||||
'crop',
|
||||
'image',
|
||||
'text',
|
||||
'title',
|
||||
'type',
|
||||
):
|
||||
if key in data and self.data.get(key) != data[key]:
|
||||
if key == 'crop':
|
||||
if not (isinstance(data[key], list)
|
||||
and len([d for d in data[key] if isinstance(d, int)]) == 4):
|
||||
return False
|
||||
else:
|
||||
if not isinstance(data[key], string_types):
|
||||
return False
|
||||
self.data[key] = data[key]
|
||||
changed = True
|
||||
if 'active' in data:
|
||||
self.active = data['active'] is True
|
||||
if changed:
|
||||
self.save()
|
||||
return True
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.index == -1:
|
||||
idx = Item.objects.all().aggregate(Max('index'))['index__max']
|
||||
idx = 0 if idx is None else idx + 1
|
||||
self.index = idx
|
||||
super(Item, self).save(*args, **kwargs)
|
||||
|
||||
def get(self, id):
|
||||
return self.objects.get(id=ox.fromAZ(id))
|
||||
|
||||
def get_id(self):
|
||||
return ox.toAZ(self.id)
|
||||
|
||||
def json(self, keys=None):
|
||||
j = {
|
||||
'id': self.get_id(),
|
||||
'active': self.active,
|
||||
'index': self.index,
|
||||
}
|
||||
j.update(self.data)
|
||||
if keys:
|
||||
for key in list(j):
|
||||
if key not in keys:
|
||||
del j[key]
|
||||
return j
|
||||
|
||||
def __unicode__(self):
|
||||
return u"%s" %(self.get_id())
|
3
pandora/home/tests.py
Normal file
3
pandora/home/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
106
pandora/home/views.py
Normal file
106
pandora/home/views.py
Normal file
|
@ -0,0 +1,106 @@
|
|||
from django.shortcuts import render
|
||||
import ox
|
||||
|
||||
from oxdjango.shortcuts import render_to_json_response, get_object_or_404_json, json_response
|
||||
from user.decorators import capability_required_json
|
||||
from oxdjango.api import actions
|
||||
|
||||
from . import models
|
||||
|
||||
|
||||
@capability_required_json('canManageHome')
|
||||
def addHomeItem(request, data):
|
||||
'''
|
||||
add new home item
|
||||
takes {
|
||||
type:
|
||||
...
|
||||
}
|
||||
returns {
|
||||
id:
|
||||
...
|
||||
}
|
||||
'''
|
||||
item = models.Item()
|
||||
if not item.edit(data):
|
||||
response = json_response(status=500, text='invalid data')
|
||||
else:
|
||||
response = json_response()
|
||||
return render_to_json_response(response)
|
||||
actions.register(addHomeItem, cache=False)
|
||||
|
||||
@capability_required_json('canManageHome')
|
||||
def editHomeItem(request, data):
|
||||
'''
|
||||
edit home item
|
||||
takes {
|
||||
id:
|
||||
...
|
||||
}
|
||||
returns {
|
||||
id:
|
||||
...
|
||||
}
|
||||
'''
|
||||
item = get_object_or_404_json(models.Item, id=ox.fromAZ(data['id']))
|
||||
if not item.edit(data):
|
||||
response = json_response(status=500, text='failed to edit item')
|
||||
else:
|
||||
response = json_response()
|
||||
return render_to_json_response(response)
|
||||
actions.register(editHomeItem, cache=False)
|
||||
|
||||
@capability_required_json('canManageHome')
|
||||
def removeHomeItem(request, data):
|
||||
'''
|
||||
remove home item
|
||||
takes {
|
||||
id:
|
||||
}
|
||||
returns {
|
||||
}
|
||||
'''
|
||||
item = get_object_or_404_json(models.Item, id=ox.fromAZ(data['id']))
|
||||
item.delete()
|
||||
response = json_response()
|
||||
return render_to_json_response(response)
|
||||
actions.register(removeHomeItem, cache=False)
|
||||
|
||||
@capability_required_json('canManageHome')
|
||||
def sortHomeItems(request, data):
|
||||
'''
|
||||
sort home times
|
||||
takes {
|
||||
ids: []
|
||||
}
|
||||
returns {
|
||||
}
|
||||
'''
|
||||
response = json_response()
|
||||
ids = data['ids']
|
||||
index = 0
|
||||
for id in ids:
|
||||
item = get_object_or_404_json(models.Item, id=ox.fromAZ(id))
|
||||
if item.index != index:
|
||||
item.index = index
|
||||
item.save()
|
||||
index += 1
|
||||
return render_to_json_response(response)
|
||||
actions.register(sortHomeItems, cache=False)
|
||||
|
||||
def getHomeItems(request, data):
|
||||
'''
|
||||
takes {
|
||||
active: true // if active is set only return active
|
||||
}
|
||||
returns {
|
||||
items: []
|
||||
}
|
||||
'''
|
||||
response = json_response()
|
||||
qs = models.Item.objects.all().order_by('index', 'created')
|
||||
if 'active' in data:
|
||||
qs = qs.filter(active=data['active'] is True)
|
||||
response['data']['items'] = [i.json() for i in qs]
|
||||
return render_to_json_response(response)
|
||||
actions.register(getHomeItems)
|
|
@ -144,6 +144,7 @@ INSTALLED_APPS = (
|
|||
'entity',
|
||||
'websocket',
|
||||
'taskqueue',
|
||||
'home',
|
||||
)
|
||||
|
||||
# Log errors into db
|
||||
|
|
Loading…
Reference in a new issue