def create_node(self): """ Behavior varies depending on its source Node object's repeating status. If Node repeats, then create a new Node, change the rcvd_date and add to spawned_nodes. If Node does not repeat, then return the source Node and delete the Message object. """ source_node = self._msg.source_node if source_node.repeats and not source_node.repeats_from_completion: # Tuple of fields to be copied to new Node() FIELDS = ('owner', 'title', 'slug', 'deadline_date', 'deadline_time', 'priority', 'tag_string', 'energy', 'time_needed', ) M2M_FIELDS = ('focus_areas',) # Make a duplicate of the Node new_node = Node() # Set the appropriate fields for field in FIELDS: value = getattr(source_node, field, None) setattr(new_node, field, value) new_node.insert_at(source_node, position='last-child', save=True) # Set m2m fields for field in M2M_FIELDS: for obj in getattr(source_node, field).all(): getattr(new_node ,field).add(obj) self._msg.spawned_nodes.add(new_node) # Now update the scheduled dates on the original Node source_node.todo_state = TodoState.objects.get(abbreviation='DONE') source_node.auto_update = True source_node.save() # Now reschedule the message itself self._msg.rcvd_date = dt.datetime.combine( source_node.scheduled_date, dt.time(0, tzinfo=get_current_timezone()), ) self._msg.save() else: new_node = source_node return new_node