Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ sudo: false


env:
- TOX_ENV=py27-django18
- TOX_ENV=py27-django19
- TOX_ENV=py27-django110
- TOX_ENV=py34-django18
- TOX_ENV=py34-django19
- TOX_ENV=py34-django110
- TOX_ENV=py27-django111
- TOX_ENV=py36-django111
- TOX_ENV=py36-django20
- TOX_ENV=py36-django21
- TOX_ENV=py36-django22


install:
- pip install tox

script:
- tox -e $TOX_ENV
- tox -e $TOX_ENV
8 changes: 5 additions & 3 deletions custom_field/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@


class CFAdmin(CustomFieldAdmin):
list_display = ('content_type', 'name')
list_filter = ('content_type',)
search_fields = ('content_type__name', 'name')
list_display = ("content_type", "name")
list_filter = ("content_type",)
search_fields = ("content_type__name", "name")


admin.site.register(CustomField, CFAdmin)
37 changes: 20 additions & 17 deletions custom_field/custom_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,39 @@ class CustomFieldModel(object):
"""
Abstract class adds some helper functions a Model
"""

@property
def get_custom_fields(self):
""" Return a list of custom fields for this model """
return CustomField.objects.filter(
content_type=ContentType.objects.get_for_model(self))
content_type=ContentType.objects.get_for_model(self)
)

def get_model_custom_fields(self):
""" Return a list of custom fields for this model, directly callable
without an instance. Use like Foo.get_model_custom_fields(Foo)
"""
return CustomField.objects.filter(
content_type=ContentType.objects.get_for_model(self))
content_type=ContentType.objects.get_for_model(self)
)

get_model_custom_fields = Callable(get_model_custom_fields)

def get_custom_field(self, field_name):
""" Get a custom field object for this model
field_name - Name of the custom field you want.
"""
content_type = ContentType.objects.get_for_model(self)
return CustomField.objects.get(
content_type=content_type, name=field_name)
return CustomField.objects.get(content_type=content_type, name=field_name)

def get_custom_value(self, field_name):
""" Get a value for a specified custom field
field_name - Name of the custom field you want.
"""
custom_field = self.get_custom_field(field_name)
return CustomFieldValue.objects.get_or_create(
field=custom_field, object_id=self.id)[0].value
field=custom_field, object_id=self.id
)[0].value

def set_custom_value(self, field_name, value):
""" Set a value for a specified custom field
Expand All @@ -54,7 +58,8 @@ def set_custom_value(self, field_name, value):
"""
custom_field = self.get_custom_field(field_name)
custom_value = CustomFieldValue.objects.get_or_create(
field=custom_field, object_id=self.id)[0]
field=custom_field, object_id=self.id
)[0]
custom_value.value = value
custom_value.save()

Expand All @@ -64,7 +69,7 @@ def __init__(self, *args, **kwargs):
super(CustomFieldValueForm, self).__init__(*args, **kwargs)
if self.instance:
try:
self.fields['value'] = self.instance.get_form_field()
self.fields["value"] = self.instance.get_form_field()
except ObjectDoesNotExist:
pass

Expand All @@ -73,8 +78,8 @@ class CustomInline(admin.GenericTabularInline):
model = CustomFieldValue
form = CustomFieldValueForm
can_delete = False
readonly_fields = ('field',)
fields = ('field', 'value')
readonly_fields = ("field",)
fields = ("field", "value")
extra = 0
max_num = 0

Expand All @@ -87,6 +92,7 @@ class CustomFieldAdmin(ModelAdmin):
""" Abstract class addes functionality to deal with custom fields in
Django admin.
"""

inlines = ()

def change_view(self, request, object_id, *args, **kwargs):
Expand All @@ -95,26 +101,23 @@ def change_view(self, request, object_id, *args, **kwargs):
inlines.append(CustomInline)
self.inlines = inlines
return super(CustomFieldAdmin, self).change_view(
request, object_id, *args, **kwargs)
request, object_id, *args, **kwargs
)

def get_form(self, request, obj=None, **kwargs):
if obj:
content_type = ContentType.objects.get_for_model(obj)
custom_fields = CustomField.objects.filter(
content_type=content_type)
custom_fields = CustomField.objects.filter(content_type=content_type)
for custom_field in custom_fields:
try:
field_value, created = CustomFieldValue.objects.get_or_create(
content_type=content_type,
object_id=obj.id,
field=custom_field,
content_type=content_type, object_id=obj.id, field=custom_field,
)
except IntegrityError:
# This can happen because content_type is really a
# cache field and didn't always exist
field_value, created = CustomFieldValue.objects.get_or_create(
object_id=obj.id,
field=custom_field,
object_id=obj.id, field=custom_field,
)
field_value.content_type = content_type
field_value.save()
Expand Down
94 changes: 72 additions & 22 deletions custom_field/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,98 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('contenttypes', '0001_initial'),
("contenttypes", "0001_initial"),
]

operations = [
migrations.CreateModel(
name='CustomField',
name="CustomField",
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=75)),
('field_type', models.CharField(default=b't', max_length=1, choices=[(b't', b'Text'), (b'i', b'Integer'), (b'b', b'Boolean (Yes/No)')])),
('default_value', models.CharField(help_text=b'You may leave blank. For Boolean use True or False', max_length=255, blank=True)),
('content_type', models.ForeignKey(to='contenttypes.ContentType')),
(
"id",
models.AutoField(
verbose_name="ID",
serialize=False,
auto_created=True,
primary_key=True,
),
),
("name", models.CharField(max_length=75)),
(
"field_type",
models.CharField(
default=b"t",
max_length=1,
choices=[
(b"t", b"Text"),
(b"i", b"Integer"),
(b"b", b"Boolean (Yes/No)"),
],
),
),
(
"default_value",
models.CharField(
help_text=b"You may leave blank. For Boolean use True or False",
max_length=255,
blank=True,
),
),
(
"content_type",
models.ForeignKey(
to="contenttypes.ContentType", on_delete=models.CASCADE
),
),
],
options={
},
options={},
bases=(models.Model,),
),
migrations.CreateModel(
name='CustomFieldValue',
name="CustomFieldValue",
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('value', models.CharField(max_length=255, null=True, blank=True)),
('object_id', models.PositiveIntegerField()),
('content_type', models.ForeignKey(blank=True, to='contenttypes.ContentType', null=True)),
('field', models.ForeignKey(related_name='instance', to='custom_field.CustomField')),
(
"id",
models.AutoField(
verbose_name="ID",
serialize=False,
auto_created=True,
primary_key=True,
),
),
("value", models.CharField(max_length=255, null=True, blank=True)),
("object_id", models.PositiveIntegerField()),
(
"content_type",
models.ForeignKey(
blank=True,
to="contenttypes.ContentType",
null=True,
on_delete=models.CASCADE,
),
),
(
"field",
models.ForeignKey(
related_name="instance",
to="custom_field.CustomField",
on_delete=models.CASCADE,
),
),
],
options={
},
options={},
bases=(models.Model,),
),
migrations.AlterUniqueTogether(
name='customfieldvalue',
unique_together=set([('field', 'object_id')]),
name="customfieldvalue", unique_together=set([("field", "object_id")]),
),
migrations.AlterUniqueTogether(
name='customfield',
unique_together=set([('name', 'content_type')]),
name="customfield", unique_together=set([("name", "content_type")]),
),
]
52 changes: 36 additions & 16 deletions custom_field/migrations/0002_auto_20150119_2032.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,63 @@
class Migration(migrations.Migration):

dependencies = [
('custom_field', '0001_initial'),
("custom_field", "0001_initial"),
]

operations = [
migrations.AddField(
model_name='customfield',
name='field_choices',
field=models.CharField(help_text=b'List the choices you want displayed, seperated by commas. This is only valid for Dropdown, Multiple, and Checkbox field types', max_length=2000, blank=True),
model_name="customfield",
name="field_choices",
field=models.CharField(
help_text=b"List the choices you want displayed, seperated by commas. This is only valid for Dropdown, Multiple, and Checkbox field types",
max_length=2000,
blank=True,
),
preserve_default=True,
),
migrations.AddField(
model_name='customfield',
name='is_required',
model_name="customfield",
name="is_required",
field=models.BooleanField(default=False),
preserve_default=True,
),
migrations.AlterField(
model_name='customfield',
name='default_value',
field=models.CharField(help_text=b'You may leave blank. For Boolean use True or False', max_length=5000, blank=True),
model_name="customfield",
name="default_value",
field=models.CharField(
help_text=b"You may leave blank. For Boolean use True or False",
max_length=5000,
blank=True,
),
preserve_default=True,
),
migrations.AlterField(
model_name='customfield',
name='field_type',
field=models.CharField(default=b't', max_length=1, choices=[(b't', b'Text'), (b'a', b'Large Text Field'), (b'i', b'Integer'), (b'f', b'Floating point decimal'), (b'b', b'Boolean (Yes/No)'), (b'm', b'Dropdown Choices'), (b'd', b'Date')]),
model_name="customfield",
name="field_type",
field=models.CharField(
default=b"t",
max_length=1,
choices=[
(b"t", b"Text"),
(b"a", b"Large Text Field"),
(b"i", b"Integer"),
(b"f", b"Floating point decimal"),
(b"b", b"Boolean (Yes/No)"),
(b"m", b"Dropdown Choices"),
(b"d", b"Date"),
],
),
preserve_default=True,
),
migrations.AlterField(
model_name='customfield',
name='name',
model_name="customfield",
name="name",
field=models.CharField(max_length=150),
preserve_default=True,
),
migrations.AlterField(
model_name='customfieldvalue',
name='value',
model_name="customfieldvalue",
name="value",
field=models.CharField(max_length=5000, null=True, blank=True),
preserve_default=True,
),
Expand Down
12 changes: 8 additions & 4 deletions custom_field/migrations/0003_customfield_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
class Migration(migrations.Migration):

dependencies = [
('custom_field', '0002_auto_20150119_2032'),
("custom_field", "0002_auto_20150119_2032"),
]

operations = [
migrations.AddField(
model_name='customfield',
name='mask',
field=models.CharField(help_text=b"You may leave blank. For user Jquery Mask, ex: '00/00/0000' for date.", max_length=5000, blank=True),
model_name="customfield",
name="mask",
field=models.CharField(
help_text=b"You may leave blank. For user Jquery Mask, ex: '00/00/0000' for date.",
max_length=5000,
blank=True,
),
),
]
Loading