This commit is contained in:
j 2014-11-19 19:21:42 +00:00
parent 8b4a2cdfc3
commit 60a7beeb23
3 changed files with 119 additions and 24 deletions

View 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']

View file

@ -22,6 +22,8 @@ class Entity(models.Model):
class Meta:
unique_together = ("type", "name")
user = models.ForeignKey(User, related_name='entities', null=True, default=None)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
@ -81,7 +83,7 @@ class Entity(models.Model):
if key == 'name':
data['name'] = re.sub(' \[\d+\]$', '', data['name']).strip()
if not data['name']:
data['name'] = "Untitled"
data['name'] = "Unnamed"
name = data['name']
num = 1
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):
if not keys:
keys=[
'alternativeNames',
'editable',
'id',
'type',
'name',
'alternativeNames',
'type',
'user',
] + self.data.keys()
response = {}
for key in keys:
@ -114,6 +117,8 @@ class Entity(models.Model):
response[key] = self.get_id()
elif key == 'editable':
response[key] = self.editable(user)
elif key == 'user':
response[key] = self.user and self.user.username
elif key in ('name', 'alternativeNames', 'type'):
response[key] = getattr(self, key)
elif key in self.data:

View file

@ -39,6 +39,7 @@ def addEntity(request, data):
'''
existing_names = []
exists = False
if 'name' in data:
names = [data['name']] + data.get('alternativeNames', [])
for name in names:
name = ox.decode_html(name)
@ -49,6 +50,7 @@ def addEntity(request, data):
if not exists:
data['name'] = ox.escape_html(data['name'])
entity = models.Entity(name=data['name'])
entity.user = request.user
for key in ('type', 'alternativeNames'):
if key in data and data[key]:
value = data[key]
@ -64,6 +66,19 @@ def addEntity(request, data):
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.save()
response = json_response(status=200, text='created')
response['data'] = entity.json()
return render_to_json_response(response)
actions.register(addEntity, cache=False)