~vorlon/ubuntu/saucy/gourmet/trunk

« back to all changes in this revision

Viewing changes to src/lib/plugins/nutritional_information/export_plugin.py

  • Committer: Bazaar Package Importer
  • Author(s): Rolf Leggewie
  • Date: 2008-07-26 13:29:41 UTC
  • Revision ID: james.westby@ubuntu.com-20080726132941-6ldd73qmacrzz0bn
Tags: upstream-0.14.0
ImportĀ upstreamĀ versionĀ 0.14.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from gourmet.plugin import BaseExporterPlugin
 
2
from gourmet.recipeManager import default_rec_manager
 
3
from nutritionLabel import MAIN_NUT_LAYOUT, MAJOR, MINOR, TINY, SEP, SHOW_PERCENT, DONT_SHOW_PERCENT, SEP
 
4
from gettext import gettext as _
 
5
 
 
6
class NutritionBaseExporterPlugin (BaseExporterPlugin):
 
7
 
 
8
    def __init__ (self):
 
9
        BaseExporterPlugin.__init__(self)
 
10
        self.add_field('Nutritional Information',
 
11
                       self.get_nutritional_info_as_text_blob,
 
12
                       self.TEXT)
 
13
 
 
14
    def get_nutritional_info_as_text_blob (self, rec):
 
15
        txt = ''
 
16
        footnotes = ''
 
17
        rd = default_rec_manager()
 
18
        nd = rd.nd
 
19
        nutinfo = nd.get_nutinfo_for_inglist(rd.get_ings(rec),rd)
 
20
        ings = rd.get_ings(rec)
 
21
        vapor = nutinfo._get_vapor()
 
22
        if rec.servings:
 
23
            txt += '<i>%s</i>'%((rec.servings and _('Nutritional information reflects amount per serving.'))
 
24
                                or
 
25
                                _('Nutritional information reflects amounts for entire recipe'))
 
26
        if len(vapor)==len(ings): return None
 
27
        if vapor:
 
28
            txt = txt + '*'
 
29
            footnotes = '\n*' + _('Nutritional information is missing for %s ingredients: %s')%(
 
30
                len(vapor),
 
31
                ', '.join([nv.__ingobject__.item for nv in vapor])
 
32
                )
 
33
        for itm in MAIN_NUT_LAYOUT:
 
34
            if itm == SEP:
 
35
                # We don't have any nice way of outputting separator
 
36
                # lines in our export
 
37
                continue
 
38
            else:
 
39
                label,typ,name,properties,show_percent,unit = itm
 
40
                if typ==MAJOR:
 
41
                    itm_text = '<b>'+label+'</b>'
 
42
                else:
 
43
                    itm_text = label
 
44
                if unit:
 
45
                    itm_text += ' (%s)'%unit
 
46
                if type(properties) == list:
 
47
                    print 'Summing',properties
 
48
                    amts = [getattr(nutinfo,att) for att in properties]
 
49
                    print 'amts',amts
 
50
                    amt = sum(amts)
 
51
                else:
 
52
                    amt = getattr(nutinfo,properties)
 
53
                if rec.servings:
 
54
                    amt = amt/rec.servings
 
55
                itm_text += ' %d'%round(amt)
 
56
            txt += '\n'+itm_text
 
57
        return '\n'.join([txt,footnotes])