inventree / InvenTree

Open Source Inventory Management System
https://docs.inventree.org
MIT License
4.4k stars 795 forks source link

[FR] Custom templatetags in labels #5052

Closed DS1705 closed 1 year ago

DS1705 commented 1 year ago

Please verify that this feature request has NOT been suggested before.

Problem statement

i wanna use a custom templatetag inside of my label. to display the voltage 0.1V as 100mV the code is below. Is this possible and if it is how do i do it?

 `
from django import template
import sys
register = template.Library()

@register.filter(name="displayunits")
def displayunits(value):
if value>=10**6:
        return value/10**6+'M'
    if value>=10**3:
        strvalue=str(value/10**3)
        if '.' in strvalue:
            return strvalue.replace('.','k')    
        return value/10**3+'k'
    if value<10**3 and value>10**-3:
        return value
    if value>=10**-3:
        return value/10**-3+'m'
    if value>=10**-6:
        return value/10**-6+'µ'
    if value>=10**-9:
        return value/10**-9+'n'
    if value>=10**-12:
        return value/10**-12+'p'

`

Suggested solution

i just wan't to know if it is possible

Describe alternatives you've considered

i tried adding the template tag in map templatetags i created in the map of the label app but it didn't work

Examples of other systems

No response

Do you want to develop this?

matmair commented 1 year ago

@DS1705 I do not really understand how you are trying to achieve your goal. In what kind of plugin (AppMixin, ReportMixin) did you insert this code? How is it loaded? What guide did you follow / where did you copy this code from? It seems logical at first but I do not think it will work this way. In general, we should be able to get this running pretty fast.

DS1705 commented 1 year ago

https://docs.djangoproject.com/en/4.2/howto/custom-template-tags/ that is what i try to do i updated the code there was something not right

matmair commented 1 year ago

@DS1705 the tag code itself is fine. I just do not see how InvenTree would discover the tag this way. Where did you place this code? In a plugin or in /plugins?

DS1705 commented 1 year ago

i found the location i put my tag in report.py in templatetags under report

wolflu05 commented 1 year ago

That is not a very good solution because it makes upgrading much harder because you have to deal with conflicts. You could use an appmixin plugin to define your tags or @matmair what about adding a template tag Mixin?

SchrodingersGat commented 1 year ago

We have a ReportMixin which is useful for adding custom context data to a report (or label).

Perhaps this mixin could be extended to also add custom methods (tags) which can be called from the template?

matmair commented 1 year ago

A mixin is one possible solution, you could also just use a simple plugin, import the library register of *any templatetag and register your tags against that. in either the init() or the plugin class.

github-actions[bot] commented 1 year ago

This issue seems stale. Please react to show this is still important.

SchrodingersGat commented 1 year ago

not stale

github-actions[bot] commented 1 year ago

This issue seems stale. Please react to show this is still important.

wolflu05 commented 1 year ago

Not stale