Adding the id as a prefix of the title of a created elements

Each time I create an epic, user story, task or an issue I start the sequence, press the create button, then

  1. Wait for it to be created
  2. Copy the id from the url
  3. Edit the title
  4. Paste the id at the front of the title
  5. Save the element.

I do this to make it easy to link the element across different apps for my time keeping - toggl and team task management.

What would I have to change to automatically add the id as a prefix to the title when an elment is created?

Hi there!

There is no setting to do so. You could probably tinker something with the API and webhooks so that it autoupdates the name after creation.

On the other hand, if you are talking about the numerical id that appears on every item (as in #53), they are sequential so you should be able to guess them beforehand, depending on the number of items and how many people are working on the given project.

Best!

G’day Charlie

I know there is no setting to do this.

I have no problem with coding on the backend with Django.

I’m looking for where/what I’d have to change in the Taiga/Django backend to do this.

Cheers and thanks!

Keith

Hello there, Keith!

You could, for example, add a check in the save method (here it is for user stories). That executes every time an object is created or updated, so it’s best to have something like if not self.ref in self.subject or something like that to check before.

Of course, there are other ways to do it, like creating a service for example, and then calling it somewhere else in the code.

Hope this helps!

Best!

1 Like

G’day @Charlie

I tried

def save(self, *args, **kwargs):
    if not self.ref in self.subject:
        self.subject = self.ref + self.subject
    
    if not self.\_importing or not self.modified_date:
        self.modified_date = timezone.now()

    if not self.status_id:
        self.status = self.project.default_issue_status

    if not self.type_id:
        self.type = self.project.default_issue_type

    if not self.severity_id:
        self.severity = self.project.default_severity

    if not self.priority_id:
        self.priority = self.project.default_priority

    return super().save(\*args, \*\*kwargs)

and
def save(self, *args, **kwargs):
    if not self.ref in self.subject:
        self.subject = f’{self.ref} {self.subject}’
    
    if not self.\_importing or not self.modified_date:
        self.modified_date = timezone.now()

    if not self.status_id:
        self.status = self.project.default_issue_status

    if not self.type_id:
        self.type = self.project.default_issue_type

    if not self.severity_id:
        self.severity = self.project.default_severity

    if not self.priority_id:
        self.priority = self.project.default_priority

    return super().save(\*args, \*\*kwargs)

which caused updating and creation to fail.

The changes I made looks like standard python/Django to me.

Any ideas as to what I’m doing wrong?

Hi there, Keith!

I checked the code again just in case, and the ref value is auto generated in a post save signal (so it is not available before the save). You can either do it after the save, or globally on this method, something like this:

// ...existing code...
def attach_sequence(sender, instance, created, **kwargs):
    if not instance._importing:
        if created or instance.prev_project != instance.project:
            # Create a reference object. This operation should be
            # used in transaction context, otherwise it can
            # create a lot of phantom reference objects.
            refval, _ = make_reference(instance, instance.project)

            # Additionally, attach sequence number to instance as ref
            instance.ref = refval
            
            # Add ref prefix to subject if not already present
            if hasattr(instance, 'subject') and not instance.subject.startswith(f"{refval} "):
                instance.subject = f"{refval} {instance.subject}"
                instance.save(update_fields=['ref', 'subject'])
            else:
                instance.save(update_fields=['ref'])

That would affect all instance types, though: Issue, Task, Epic and User Story. I did a quick try and it seems to work, let me know if that works for you.

Best!

1 Like

I’ve implemented this change in

/taiga-back/taiga/projects/references/models.py

it works well.

thank you @Charlie cheers!

1 Like