forked from 0x2620/pandora
add user
This commit is contained in:
parent
8b4a2cdfc3
commit
60a7beeb23
3 changed files with 119 additions and 24 deletions
75
pandora/entity/migrations/0002_add_user.py
Normal file
75
pandora/entity/migrations/0002_add_user.py
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from south.utils import datetime_utils as datetime
|
||||||
|
from south.db import db
|
||||||
|
from south.v2 import SchemaMigration
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(SchemaMigration):
|
||||||
|
|
||||||
|
def forwards(self, orm):
|
||||||
|
# Adding field 'Entity.user'
|
||||||
|
db.add_column('entity_entity', 'user',
|
||||||
|
self.gf('django.db.models.fields.related.ForeignKey')(default=None, related_name='entities', null=True, to=orm['auth.User']),
|
||||||
|
keep_default=False)
|
||||||
|
|
||||||
|
|
||||||
|
def backwards(self, orm):
|
||||||
|
# Deleting field 'Entity.user'
|
||||||
|
db.delete_column('entity_entity', 'user_id')
|
||||||
|
|
||||||
|
|
||||||
|
models = {
|
||||||
|
'auth.group': {
|
||||||
|
'Meta': {'object_name': 'Group'},
|
||||||
|
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||||
|
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
|
||||||
|
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
|
||||||
|
},
|
||||||
|
'auth.permission': {
|
||||||
|
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
|
||||||
|
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||||
|
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
|
||||||
|
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||||
|
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
|
||||||
|
},
|
||||||
|
'auth.user': {
|
||||||
|
'Meta': {'object_name': 'User'},
|
||||||
|
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||||
|
'email': ('django.db.models.fields.EmailField', [], {'max_length': '255', 'blank': 'True'}),
|
||||||
|
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||||
|
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||||
|
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||||
|
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
|
||||||
|
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||||
|
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||||
|
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||||
|
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||||
|
'password': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||||
|
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||||
|
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
|
||||||
|
},
|
||||||
|
'contenttypes.contenttype': {
|
||||||
|
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
|
||||||
|
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||||
|
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||||
|
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||||
|
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
|
||||||
|
},
|
||||||
|
'entity.entity': {
|
||||||
|
'Meta': {'unique_together': "(('type', 'name'),)", 'object_name': 'Entity'},
|
||||||
|
'alternativeNames': ('ox.django.fields.TupleField', [], {'default': '[]'}),
|
||||||
|
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
|
||||||
|
'data': ('ox.django.fields.DictField', [], {'default': '{}'}),
|
||||||
|
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||||
|
'matches': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||||
|
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
|
||||||
|
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||||
|
'name_find': ('django.db.models.fields.TextField', [], {'default': "''"}),
|
||||||
|
'name_sort': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True'}),
|
||||||
|
'type': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||||
|
'user': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'related_name': "'entities'", 'null': 'True', 'to': "orm['auth.User']"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
complete_apps = ['entity']
|
|
@ -22,6 +22,8 @@ class Entity(models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = ("type", "name")
|
unique_together = ("type", "name")
|
||||||
|
|
||||||
|
user = models.ForeignKey(User, related_name='entities', null=True, default=None)
|
||||||
|
|
||||||
created = models.DateTimeField(auto_now_add=True)
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
modified = models.DateTimeField(auto_now=True)
|
modified = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
@ -81,7 +83,7 @@ class Entity(models.Model):
|
||||||
if key == 'name':
|
if key == 'name':
|
||||||
data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip()
|
data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip()
|
||||||
if not data['name']:
|
if not data['name']:
|
||||||
data['name'] = "Untitled"
|
data['name'] = "Unnamed"
|
||||||
name = data['name']
|
name = data['name']
|
||||||
num = 1
|
num = 1
|
||||||
while Entity.objects.filter(name=name, type=self.type).exclude(id=self.id).count()>0:
|
while Entity.objects.filter(name=name, type=self.type).exclude(id=self.id).count()>0:
|
||||||
|
@ -102,11 +104,12 @@ class Entity(models.Model):
|
||||||
def json(self, keys=None, user=None):
|
def json(self, keys=None, user=None):
|
||||||
if not keys:
|
if not keys:
|
||||||
keys=[
|
keys=[
|
||||||
|
'alternativeNames',
|
||||||
'editable',
|
'editable',
|
||||||
'id',
|
'id',
|
||||||
'type',
|
|
||||||
'name',
|
'name',
|
||||||
'alternativeNames',
|
'type',
|
||||||
|
'user',
|
||||||
] + self.data.keys()
|
] + self.data.keys()
|
||||||
response = {}
|
response = {}
|
||||||
for key in keys:
|
for key in keys:
|
||||||
|
@ -114,6 +117,8 @@ class Entity(models.Model):
|
||||||
response[key] = self.get_id()
|
response[key] = self.get_id()
|
||||||
elif key == 'editable':
|
elif key == 'editable':
|
||||||
response[key] = self.editable(user)
|
response[key] = self.editable(user)
|
||||||
|
elif key == 'user':
|
||||||
|
response[key] = self.user and self.user.username
|
||||||
elif key in ('name', 'alternativeNames', 'type'):
|
elif key in ('name', 'alternativeNames', 'type'):
|
||||||
response[key] = getattr(self, key)
|
response[key] = getattr(self, key)
|
||||||
elif key in self.data:
|
elif key in self.data:
|
||||||
|
|
|
@ -39,31 +39,46 @@ def addEntity(request, data):
|
||||||
'''
|
'''
|
||||||
existing_names = []
|
existing_names = []
|
||||||
exists = False
|
exists = False
|
||||||
names = [data['name']] + data.get('alternativeNames', [])
|
if 'name' in data:
|
||||||
for name in names:
|
names = [data['name']] + data.get('alternativeNames', [])
|
||||||
name = ox.decode_html(name)
|
for name in names:
|
||||||
if models.Entity.objects.filter(type=data['type'],
|
name = ox.decode_html(name)
|
||||||
name_find__icontains=u'|%s|'%name).count() != 0:
|
if models.Entity.objects.filter(type=data['type'],
|
||||||
exists = True
|
name_find__icontains=u'|%s|'%name).count() != 0:
|
||||||
existing_names.append(name)
|
exists = True
|
||||||
if not exists:
|
existing_names.append(name)
|
||||||
data['name'] = ox.escape_html(data['name'])
|
if not exists:
|
||||||
entity = models.Entity(name=data['name'])
|
data['name'] = ox.escape_html(data['name'])
|
||||||
for key in ('type', 'alternativeNames'):
|
entity = models.Entity(name=data['name'])
|
||||||
if key in data and data[key]:
|
entity.user = request.user
|
||||||
value = data[key]
|
for key in ('type', 'alternativeNames'):
|
||||||
if isinstance(value, basestring):
|
if key in data and data[key]:
|
||||||
value = ox.escape_html(value)
|
value = data[key]
|
||||||
if key == 'alternativeNames':
|
if isinstance(value, basestring):
|
||||||
value = tuple([ox.escape_html(v) for v in value])
|
value = ox.escape_html(value)
|
||||||
setattr(entity, key, value)
|
if key == 'alternativeNames':
|
||||||
|
value = tuple([ox.escape_html(v) for v in value])
|
||||||
|
setattr(entity, key, value)
|
||||||
|
entity.matches = 0
|
||||||
|
entity.save()
|
||||||
|
response = json_response(status=200, text='created')
|
||||||
|
response['data'] = entity.json()
|
||||||
|
else:
|
||||||
|
response = json_response(status=409, text='name exists')
|
||||||
|
response['data']['names'] = existing_names
|
||||||
|
else:
|
||||||
|
type = data['type']
|
||||||
|
name = 'Unnamed'
|
||||||
|
num = 1
|
||||||
|
while models.Entity.objects.filter(name=name, type=type).count()>0:
|
||||||
|
num += 1
|
||||||
|
name = 'Unnamed [%d]' % num
|
||||||
|
entity = models.Entity(name=name, type=type)
|
||||||
|
entity.user = request.user
|
||||||
entity.matches = 0
|
entity.matches = 0
|
||||||
entity.save()
|
entity.save()
|
||||||
response = json_response(status=200, text='created')
|
response = json_response(status=200, text='created')
|
||||||
response['data'] = entity.json()
|
response['data'] = entity.json()
|
||||||
else:
|
|
||||||
response = json_response(status=409, text='name exists')
|
|
||||||
response['data']['names'] = existing_names
|
|
||||||
return render_to_json_response(response)
|
return render_to_json_response(response)
|
||||||
actions.register(addEntity, cache=False)
|
actions.register(addEntity, cache=False)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue