示例#1
0
    def edit_idea(self, draft):
        # read the fields
        submission_date = self.idea.i.submission_date if self.idea else datetime.now()
        title = self.title.value
        description = self.description.value
        impact = self.impact.value
        domain = DomainRepository().get_by_id(self.domain_id.value)

        challenge_id = self.challenge_id.value
        challenge = ChallengeRepository().get_by_id(
            challenge_id) if challenge_id else None
        # make sure the challenge was active at the submission_date
        if challenge and not challenge.is_active(submission_date):
            challenge = None

        tags = [self.get_or_create_tag(tag.lower()) for tag in self.tags.value]

        # create or update the idea
        # mandatory fields
        mandatory_fields = dict(
            title=title,
            description=description,
            impact=impact,
            domain=domain,
            challenge=challenge,
            tags=tags,
        )

        if not self.idea:
            idea = IdeaRepository().create(submitted_by=get_current_user(),
                                           submission_date=submission_date,
                                           **mandatory_fields)
        else:
            idea = IdeaRepository().get_by_id(self.idea.id)
            idea.set(**mandatory_fields)

        # Optional fields
        idea.benefit_department = self.benefit_department.value
        idea.origin = self.origin.value
        idea.implementation = self.implementation.value

        self.update_idea_authors(idea)

        for attr in ('attachment_1', 'attachment_2', 'attachment_3'):
            property_value = getattr(self, attr).value
            if property_value is not None:
                idea_attachment = getattr(idea, attr)
                if idea_attachment:
                    setattr(idea, attr, None)
                    # idea_attachment.delete()
                setattr(idea, attr, self.create_attachement(property_value))

        idea.show_creator = not self.anonymous.value
        idea.already_done = self.already_done.value

        # workflow
        workflow = get_workflow()
        draft_event = workflow.WFEvents.DRAFT_EVENT
        submit_event = workflow.WFEvents.SUBMIT_EVENT

        def process_event(idea, event):
            wf_process_event(from_user=get_current_user(),
                             idea=idea,
                             event=event,
                             notify=flashmessage.set_flash)

        if draft:  # save as a draft
            process_event(idea, draft_event)
        else:  # submit the idea
            process_event(idea, submit_event)

        return idea