def get_initial(self, *args, **kwargs): """Pre-populate the value for the initial order. This can't be done at the class level because we want to query the value each time.""" initial = super(OpenEndedQuestionCreateView, self).get_initial(*args, **kwargs) if 'order' not in initial: initial['order'] = get_max_order(OpenEndedQuestion) return initial
def save(self, *args, **kwargs): """Always slugify the name prior to saving the model and set created_by or updated_by fields if specified.""" self.title_slug = slugify(self.title) self.color = self._format_color(self.color) self.secondary_color = self._generate_secondary_color() if not self.order: self.order = get_max_order(self.__class__) kwargs = self._check_updated_or_created_by(**kwargs) super(Category, self).save(*args, **kwargs)
def _create_category(self, row): title = row[1] desc = row[2] try: # Update the description if this exists. category = Category.objects.get(title_slug=slugify(title)) category.description = desc category.save() except Category.DoesNotExist: order = get_max_order(Category) Category.objects.create(order=order, title=title, description=desc)
def duplicate_content(self, prefix="Copy of"): """This method will duplicate all of the content stored within this Category. That is, we'll create copies of all Goal and Action objects that are children for this category. Every newly created object will be a clone, except for the title, which will be prefixed with the given text. XXX: This is a potentially slow & inefficient method! Handle with care. Returns a copy of the new category. """ new_category = Category.objects.create( order=get_max_order(Category), title="{} {}".format(prefix, self.title), description=self.description, icon=self.icon, image=self.image, notes=self.notes, color=self.color, secondary_color=self.secondary_color, packaged_content=self.packaged_content, consent_summary=self.consent_summary, consent_more=self.consent_more, prevent_custom_triggers_default=self.prevent_custom_triggers_default, display_prevent_custom_triggers_option=self.display_prevent_custom_triggers_option, ) for goal in self.goals: new_goal, _ = Goal.objects.update_or_create( title="{} {}".format(prefix, goal.title), description=goal.description, subtitle=goal.subtitle, notes=goal.notes, more_info=goal.more_info, icon=goal.icon, keywords=goal.keywords, ) new_goal.categories.add(new_category) new_goal.save() for action in goal.action_set.all(): default_trigger = None if action.default_trigger: default_trigger = action.default_trigger default_trigger.pk = None # HACK to get a new object. default_trigger.name = "{} {}".format( prefix, action.default_trigger.name ) default_trigger.save() new_action = Action.objects.create( title=action.title, action_type=action.action_type, sequence_order=action.sequence_order, source_link=action.source_link, source_notes=action.source_notes, notes=action.notes, more_info=action.more_info, description=action.description, external_resource=action.external_resource, external_resource_name=action.external_resource_name, notification_text=action.notification_text, icon=action.icon, default_trigger=default_trigger, ) new_action.goals.add(goal) return new_category