2010-12-06 22:33:00 +00:00
|
|
|
from django.db import models
|
2011-01-04 06:27:10 +00:00
|
|
|
import operator
|
|
|
|
from django.db.models import Q
|
|
|
|
from ox.text import smartSplit
|
2011-03-06 12:35:55 +00:00
|
|
|
from ox.django.fields import DictField
|
2011-01-04 06:27:10 +00:00
|
|
|
from django.core.paginator import Paginator, InvalidPage, EmptyPage
|
2011-03-06 12:35:55 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2011-01-04 06:27:10 +00:00
|
|
|
|
|
|
|
def splitSearch(string):
|
|
|
|
ret = []
|
|
|
|
for s in smartSplit(string):
|
|
|
|
word = s.replace("'", "").replace('"', '')
|
|
|
|
ret.append(word)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
class ItfModel(models.Model):
|
|
|
|
fts_fields = []
|
|
|
|
fk_filters = []
|
|
|
|
sort_fields = []
|
2011-02-01 15:13:30 +00:00
|
|
|
hasComments = True
|
2011-01-04 06:27:10 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def list_dict(self):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def info_dict(self):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def fts(kls, qset, search):
|
|
|
|
terms = splitSearch(search)
|
|
|
|
qobjects = []
|
|
|
|
for t in terms:
|
|
|
|
for f in kls.fts_fields:
|
|
|
|
qstring = f + '__icontains'
|
|
|
|
qobject = Q(**{qstring:t})
|
|
|
|
qobjects.append(qobject)
|
|
|
|
return qset.filter(reduce(operator.or_, qobjects))
|
|
|
|
|
|
|
|
'''
|
|
|
|
eg. fks = {
|
|
|
|
'somefield': [1, 5, 7],
|
|
|
|
'someotherfield': [3]
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
@classmethod
|
|
|
|
def filter_by_fks(kls, qset, fks):
|
|
|
|
qobjects = []
|
|
|
|
for key in fks.keys():
|
|
|
|
field = getField(kls._meta.fields, key)
|
|
|
|
if field:
|
|
|
|
# rel_class = field.related.parent_model
|
|
|
|
for i in fks[key]:
|
|
|
|
qobject = Q(**{field.name: i})
|
|
|
|
qobjects.append(qobject)
|
|
|
|
return qset.filter(reduce(operator.or_, qobjects))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_fk_objects(kls):
|
|
|
|
ret = {}
|
|
|
|
for f in kls.fk_filters:
|
|
|
|
ret[f] = []
|
|
|
|
field = getField(kls._meta.fields, f)
|
|
|
|
rel_class = field.related.parent_model
|
|
|
|
for o in rel_class.objects.all():
|
|
|
|
ret[f].append({
|
|
|
|
'id': o.id,
|
|
|
|
'title': unicode(o)
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_list(kls, data):
|
|
|
|
options = {
|
|
|
|
'page_no': 1,
|
|
|
|
'list_size': 8,
|
|
|
|
'search': '',
|
2011-01-06 05:59:08 +00:00
|
|
|
'sort': [],
|
|
|
|
'range': [0,50]
|
2011-01-04 06:27:10 +00:00
|
|
|
}
|
|
|
|
options.update(data)
|
|
|
|
ret = []
|
|
|
|
page_no = options['page_no']
|
|
|
|
list_size = options['list_size']
|
|
|
|
qset = kls.objects.all()
|
|
|
|
|
|
|
|
search = options['search']
|
|
|
|
if search != '':
|
|
|
|
qset = kls.fts(qset, search)
|
2011-01-11 20:32:18 +00:00
|
|
|
|
2011-01-04 06:27:10 +00:00
|
|
|
sort = options['sort']
|
|
|
|
if sort != []:
|
2011-01-11 20:32:18 +00:00
|
|
|
for s in sort:
|
2011-01-13 10:08:10 +00:00
|
|
|
if s['operator'] == '-':
|
|
|
|
operator = '-'
|
|
|
|
else:
|
|
|
|
operator = ''
|
|
|
|
sort = operator + s['key']
|
2011-01-11 20:32:18 +00:00
|
|
|
qset = qset.order_by(sort)
|
2011-01-06 05:59:08 +00:00
|
|
|
r0 = options['range'][0]
|
|
|
|
r1 = options['range'][1]
|
|
|
|
results = qset[r0:r1]
|
|
|
|
for r in results:
|
2011-01-04 06:27:10 +00:00
|
|
|
ret.append(r.list_dict())
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def getField(fields, name):
|
|
|
|
for f in fields:
|
|
|
|
if f.name == name:
|
|
|
|
return f
|
|
|
|
return False
|
2010-12-06 22:33:00 +00:00
|
|
|
|
2011-02-01 15:13:30 +00:00
|
|
|
|
2011-03-06 12:35:55 +00:00
|
|
|
class ModelsModel(models.Model):
|
|
|
|
model = models.ForeignKey(ContentType)
|
|
|
|
friendly_name = models.CharField(max_length=256)
|
|
|
|
friendly_name_singular = models.CharField(max_length=256, blank=True, null=True)
|
|
|
|
info = models.TextField(blank=True)
|
|
|
|
sort_options = models.ManyToManyField("ModelSort", blank=True, null=True)
|
|
|
|
# filter_fields = DictField(blank=True)
|
|
|
|
is_visible = models.BooleanField(default=True)
|
2011-02-01 15:13:30 +00:00
|
|
|
|
2011-03-06 12:35:55 +00:00
|
|
|
@property
|
|
|
|
def module(self):
|
|
|
|
return self.model.app_label
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.friendly_name
|
|
|
|
|
|
|
|
def get_dict(self):
|
|
|
|
# sort_options = map(lambda x: {'key': x.key, 'text': x.text}, self.sort_options.all())
|
|
|
|
return {
|
|
|
|
'module': self.module,
|
|
|
|
'model': self.model.model,
|
|
|
|
'info': self.info,
|
|
|
|
'friendly_name': self.friendly_name,
|
|
|
|
'friendly_name_singular': self.friendly_name_singular,
|
|
|
|
'fk_filters': self.model.model_class().fk_filters,
|
|
|
|
'fts_fields': self.model.model_class().fts_fields,
|
|
|
|
'sort_options': map(lambda x: {'key': x.key, 'text': x.text}, self.sort_options.all())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ModelSort(models.Model):
|
|
|
|
key = models.CharField(max_length=64)
|
|
|
|
text = models.CharField(max_length=512)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return "%s: %s" % (self.key, self.text,)
|
|
|
|
|
|
|
|
class ModelExtraButton(models.Model):
|
|
|
|
icon = models.CharField(max_length=64)
|
|
|
|
mouseover = models.CharField(max_length=512, blank=True, null=True)
|
|
|
|
dialog_text = models.TextField()
|
|
|
|
model = models.ForeignKey(ModelsModel)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.mouseover
|
2011-02-01 15:13:30 +00:00
|
|
|
|
|
|
|
def site_config():
|
|
|
|
with open(settings.SITE_CONFIG) as f:
|
|
|
|
site_config = json.load(f)
|
|
|
|
site_config['keys'] = {}
|
|
|
|
for key in site_config['itemKeys']:
|
|
|
|
site_config['keys'][key['id']] = key
|
|
|
|
site_config['_findKeys'] = {}
|
|
|
|
for key in site_config['findKeys']:
|
|
|
|
site_config['_findKeys'][key['id']] = key
|
|
|
|
return site_config
|