Skip to content

Commit

Permalink
Stopped using ContentType with edit handlers, fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamBrenner committed Nov 7, 2016
1 parent 0c6d552 commit 8c10760
Showing 1 changed file with 4 additions and 25 deletions.
29 changes: 4 additions & 25 deletions wagtailpolls/edit_handlers.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,27 @@
from django.template.loader import render_to_string
from django.contrib.contenttypes.models import ContentType
from django.utils.safestring import mark_safe
from django.utils.encoding import force_text
from django.core.exceptions import ImproperlyConfigured

from django.utils.safestring import mark_safe
from wagtail.wagtailadmin.edit_handlers import BaseChooserPanel
from wagtail.wagtailcore.utils import resolve_model_string

from .widgets import AdminPollChooser


class BasePollChooserPanel(BaseChooserPanel):
object_type_name = 'item'

_target_model = None
_target_content_type = None

@classmethod
def widget_overrides(cls):
return {cls.field_name: AdminPollChooser(
content_type=cls.target_content_type(), snippet_type_name=cls.get_snippet_type_name())}
return {cls.field_name: AdminPollChooser(model=cls.target_model())}

@classmethod
def target_model(cls):
if cls._target_model is None:
if cls.snippet_type:
try:
cls._target_model = resolve_model_string(cls.snippet_type)
except LookupError:
raise ImproperlyConfigured("{0}.snippet_type must be of the form 'app_label.model_name', given {1!r}".format(
cls.__name__, cls.snippet_type))
except ValueError:
raise ImproperlyConfigured("{0}.snippet_type refers to model {1!r} that has not been installed".format(
cls.__name__, cls.snippet_type))
else:
cls._target_model = cls.model._meta.get_field(cls.field_name).rel.to
cls._target_model = cls.model._meta.get_field(cls.field_name).rel.model

return cls._target_model

@classmethod
def target_content_type(cls):
if cls._target_content_type is None:
cls._target_content_type = ContentType.objects.get_for_model(cls.target_model())
return cls._target_content_type

def render_as_field(self):
instance_obj = self.get_chosen_item()
return mark_safe(render_to_string(self.field_template, {
Expand Down

0 comments on commit 8c10760

Please sign in to comment.