0

I am trying to create a report content type that contains a point-in-time snapshot of the contents of a custom container object over a time interval. I will eventually be storing additional time-variable data with the list so generating the list just-in-time in a view will not quite work.

I have a content type for the report that includes the time interval and a field to hold the list of references to the container contents:

class IIssuesReport(form.Schema):
    report_begin_date = schema.Date(
        title=_(u"Report begin date"),
        )

    report_end_date = schema.Date(
        title=_(u"Report end date"),
        )

    issues = RelationList(
        title=_(u"Report Issues"),
        description=_(u'Select Issues'),
        default = [],
        value_type=RelationChoice(
            title=_(u'Issue'),
            default=[],
            source=ObjPathSourceBinder()
            ),
        required=False,
        )

I want to programatically populate the "issues" field when the form is submitted. I believe I should be able to do this by writing an adapter that override the issues() property setter to generate and assign the data to the list. I created a "populated" class with a factory to override the issues property setter:

class IPopulatedIssuesReport(interface.Interface):
    """A list of issues.
    """


class PopulateIssuesReport(object):
    """ Generate the IssuesReport issues from existing inventory
    """
    implements(IPopulatedIssuesReport)
    adapts(IIssuesReport)

    def __init__(self, context):
        self.context = context

    @property
    def issues(self):
        import pdb; pdb.set_trace()

And registered the adapter factory:

<adapter factory=".issuesReport.PopulateIssuesReport" />

I patterned much of this after other posts about adapting INameFromTitle to use other separate fields, specifically DavidJB's post:

When I created my IIssueReport content I expected to be dropped into the debugger in the adapter but it did not happen, as if the adapter never got executed. What am I missing? Is this the correct approach to populate this field with existing data from my site?

dayne
  • 1
  • 1
  • Does the IIssueReport interface need to be a behavior for this to work? I would not think so. – dayne Jan 08 '14 at 16:58

1 Answers1

1

you should provide provides definition in adapter.

Try this !!

<adapter for="yourType.IIssuesReport"
         factory=".issuesReport.PopulateIssuesReport"
         provides=".issuesReport.IPopulateIssuesReport"
       />        
webbyfox
  • 1,049
  • 10
  • 22
  • Something is still amiss as this did not seem to work. I currently have IIssuesReport, IPopulatedIssuesReport, and PopulateIssuesReport in the same issuesReport.py file. Perhaps this is causing a conflict preventing this from working. I will try splitting out the adapter into a separate file. – dayne Jan 08 '14 at 17:12
  • creat behaviors.py and move those code in it. Let see if that works. adapter defination for factor and provides will start with like `.behaviors.PopulateIssuesReport` and `.behaviors.IPopulateIssuesReport` – webbyfox Jan 08 '14 at 17:15
  • Moved the adapter code (interface and factory) to the a behaviors.py file and still see the same behavior: When I create the IIssuesReport content it completes the creation but I expected the adapter to run and stop in the debugger. Other ideas what I may be missing? Is there a way I can see that the adapter got registered and is available? – dayne Jan 08 '14 at 17:37
  • Thanks so much for helping here. If I can get this figured out I need to turn it into a short tutorial. To simplify I created a new project and stripped everything I think is non-essential out of the code. I pasted all the code [here](http://pastebin.com/JcVF6Fyp). Not sure this was the best way to paste it (with multiple files in one paste). The past contains issuesReport.py, profiles/default/types/issuesReport.xml, interfaces.py, behaviors.py, and configure.zcml. I am using Plone 4.1 (running on an Ubuntu system). – dayne Jan 11 '14 at 15:38
  • About the only thing I have not tried yet was to register a behavior and extend the behavior so this the same as the adapting INameFromTitle examples. I would not expect this to be a requirement but perhaps there is an issue that makes this necessary? – dayne Jan 14 '14 at 18:18
  • Sorry, I will have a look very soon. Meanwhile, can you have a look https://github.com/webbyfox/GeorgeDrexler.grant Same sort of interface I've implemented – webbyfox Jan 15 '14 at 10:06