Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

values in the version--jinja2 can't get it. #51

Closed
tsengs opened this issue Sep 25, 2014 · 6 comments
Closed

values in the version--jinja2 can't get it. #51

tsengs opened this issue Sep 25, 2014 · 6 comments
Assignees
Labels

Comments

@tsengs
Copy link

tsengs commented Sep 25, 2014

Hi Max,

Here I come again. This time I am trying to get 'description' of the version, and the custom fields I made in the version. Can you tell me how to retrieve them?

Thanks,
Shu

@maxtepkeev
Copy link
Owner

Hi Shu!

What have you tried ?
Did you read the documentation for the version resource ?
Did you have any problems ?

Basically it's the same as with all resources:

version = redmine.version.get(12345)  # 12345 is your version id
version.description
'I am description of the version 12345'
version.custom_fields
<redmine.resultsets.ResourceSet object with CustomField resources>

OR

versions = redmine.version.filter(project_id=12345)  # 12345 is your project id with versions
for version in versions:
    print(version.description)
    for field in version.custom_fields:
        print(field)

You can also get versions from the project resource via project.versions relation.

@maxtepkeev maxtepkeev self-assigned this Sep 25, 2014
@tsengs
Copy link
Author

tsengs commented Oct 8, 2014

HI Max,

I am using jinja2 template with your project. I've tried:

{% for i in data %}
{{ i.id }}
{{ i.description }}
{% if i.version %}{{ i.version.name }}{% endif %}
{% for v in i.versions %}
{{ v|showattrs }}
{{ v.description }}{% endfor %}
{% for f in i.version.custom_fields %}
{{ f|showattrs }}
{% if loop.index == 1 %} {{ f.value }} {{ f.name }} {% endif %} {% endfor %}
{% endfor %}

I am able to print these three
{{ i.id }}
{{ i.description }}
{{ i.version.name }}
but I got nothing for
{{ v.description }}
{{ f.value }}
{{ f.name }}

Shu

@tsengs
Copy link
Author

tsengs commented Oct 13, 2014

Hi Max,

I did read the documentation and I tried a couple of other possibilities to get the fields, but in vain. Can you maybe tell what I've missed?

{% for i in data %}
{% for version in i.fixed_versions %}
{% for version in i.versions %}
{% for version in i.project_versions %}
{% for version in versions %}
{% for version in version_ids. %}
{% for version in fixed_versions %}
{% for version in fixed_version_ids %}

print
{{ version.description }}
{{ version.name }}
{{ version.value }}
{{ version.id }}

Thanks!
Shu

@maxtepkeev
Copy link
Owner

Hi, Shu!

Python-Redmine works absolutely fine with jinja2, django templates or any other template engine.

When you're trying to print something and get nothing, means that you're trying to get an attribute that doesn't exist but you get nothing because template engines usually suppress some common exceptions. Try to run the same cycle in Python and you'll get an exception.

Also I don't understand what is data in your cycle. You're using the variable i, so I believe that data is a resource set filled with issue resources. Issue resources only have a version attribute if a project contains versions in your Redmine and this version attribute represents a single resource, so I don't understand what you're trying to achieve when you're trying to iterate over them in a cycle.

As I said there are no problems with python-redmine and any template engine. What I can recommend you to do is to recreate this same cycle in pure Python and examine existing attributes via dir function to see what attributes are available.

@tsengs tsengs changed the title values in the version values in the version--jinja2 can't get it. Oct 15, 2014
@tsengs
Copy link
Author

tsengs commented Oct 15, 2014

Hi Max,

The short question would be:
Within a Jinja2 template where you have an issue, how do you get that issue's version.description and it's custom fields?

A long explanation would be:
I understand the python can get it like below:

from redminereports import make_redmine
redmine = make_redmine()
print redmine.version.get(id).description
for cf in redmine.version.get(id).custom_fields:
print cf

But is there a way to access the Fixed_Version object associated with an Issue directory from a Redmine() object? The only method I know for accessing the fixed_version object associated with an Issue is via a call to Redmine().version.get(id).custom_fields().

I need to access to each Issue's fixed_version in a Jinja2 template and I can't call python code within Jinja2. I don't have access to the database and I don't control the data source, so I can't have Redmine().version.get() called externally.

Is there an alternative?

Thanks!
Shu

@maxtepkeev
Copy link
Owner

Hi Shu,

Your python code is invalid, here's the right one:

from redmine.exceptions import ResourceAttrError
from redminereports import make_redmine

redmine = make_redmine()

# let's get all issues from a project where we have some versions defined
issues = redmine.issue.filter(project_id='foobar')

for issue in issues:
    try:
        # we need to load all the available info for this version resource,
        # because by default Redmine gives us only id and name
        version = issue.version.refresh()
    except ResourceAttrError:
        version = None

    if version is not None:
        for custom_field in version.custom_fields:
            print(
                custom_field.id, # id
                custom_field.name, # name
                dir(custom_field) # all the available attributes
            )

This code will do exactly what you asked for, i.e. you'll have an access to each issues version custom fields.

I can't help you with porting this to jinja2, because I don't use it, you'll have to figure out this yourself, or ask someone who knows jinja2. This has nothing to do with python-redmine, this is about how you can call python object methods within a template engine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants