def apply_ingkey (self,*args): key = self.ingKeyEntry.get_text() if key==self.ingkey: self.changeIngKeyAction.dehighlight_action() return #ings = self.rd.fetch_all(self.rd.ingredients_table,ingkey=self.ingkey) #self.rd.modify_ings(ings,{'ingkey':key}) if self.rec: try: user_says_yes = de.getBoolean( label=_('Change ingredient key'), sublabel=_( 'Change ingredient key from %(old_key)s to %(new_key)s everywhere or just in the recipe %(title)s?' )%{'old_key':self.ingkey, 'new_key':key, 'title':self.rec.title }, custom_no=_('Change _everywhere'), custom_yes=_('_Just in recipe %s')%self.rec.title ) except de.UserCancelledError: self.changeIngKeyAction.dehighlight_action() return else: if not de.getBoolean(label=_('Change ingredient key'), sublabel=_('Change ingredient key from %(old_key)s to %(new_key)s everywhere?' )%{'old_key':self.ingkey, 'new_key':key, }, cancel=False, ): self.changeIngKeyAction.dehighlight_action() return if self.rec and user_says_yes: self.rd.update_by_criteria(self.rd.ingredients_table, {'ingkey':self.ingkey, 'recipe_id':self.rec.id}, {'ingkey':key} ) else: self.rd.update_by_criteria(self.rd.ingredients_table, {'ingkey':self.ingkey}, {'ingkey':key} ) old_key = self.ingkey self.set_ingkey(key) # Update amounts dictionary... self.amounts[key] = self.amounts[old_key] del self.amounts[old_key] self.autosearch_ingkey() self.changeIngKeyAction.dehighlight_action() if self.nd.get_nutinfo(key): self.setup_to_units() self.check_next_amount() self.emit('key-changed',(old_key,key))
def dialog_response_cb (self, dialog, response_id): if response_id == gtk.RESPONSE_CLOSE: self.valueDialog.hide() if response_id == gtk.RESPONSE_APPLY: criteria,table = self.get_criteria_and_table() count = self.rd.fetch_len(table,**criteria) count_text = ngettext('Change will affect %s recipe', 'Change will affect %s recipes', count)%count if self.deleteValueButton.get_active(): label = _('Delete %s where it is %s?')%(self.field,self.val_string) yes = gtk.STOCK_DELETE else: label = _('Change %s from %s to "%s"?')%(self.field,self.val_string, self.newValueEntry.get_text()) yes = '_Change' if de.getBoolean(label=label, sublabel='\n\n'.join([ count_text, _('<i>This change is not reversable.</i>') ]), custom_yes=yes, custom_no=gtk.STOCK_CANCEL, cancel=False): self.apply_changes(criteria,table) self.populate_treeview()
def save_unit_cb (self,*args): from_unit = self.fromUnitComboBoxEntry.get_children()[0].get_text() old_from_unit = self.fromUnit #ings = self.rd.fetch_all(self.rd.ingredients_table,ingkey=self.ingkey,unit=old_from_unit) #self.rd.modify_ings(ings,{'unit':from_unit}) if self.rec and de.getBoolean( label=_('Change unit'), sublabel=_( 'Change unit from %(old_unit)s to %(new_unit)s for all ingredients %(ingkey)s or just in the recipe %(title)s?' )%{'old_unit':old_from_unit, 'new_unit':from_unit, 'ingkey':self.ingkey, 'title':self.rec.title }, custom_no=_('Change _everywhere'), custom_yes=_('_Just in recipe %s')%self.rec.title ): self.rd.update_by_criteria(self.rd.ingredients_table, {'ingkey':self.ingkey, 'unit':old_from_unit, 'recipe_id':self.rec.id}, {'unit':from_unit} ) else: self.rd.update_by_criteria(self.rd.ingredients_table, {'ingkey':self.ingkey, 'unit':old_from_unit}, {'unit':from_unit} ) self.set_from_unit(self.fromUnitComboBoxEntry.get_children()[0].get_text()) self.changeUnitAction.dehighlight_action() self.emit('unit-changed',((old_from_unit,self.ingkey),(from_unit,self.ingkey)))
def applyEntriesCB (self, *args): newdic = {} for k,e in self.entries.items(): txt = e.get_text() if txt: if k=='amount': try: newdic[k]=convert.frac_to_float(txt) except: print 'Problem with amount:',txt import traceback; traceback.print_exc() de.show_amount_error(txt) return else: newdic[k]=txt if not newdic: print 'We called applyEntriesCB with no text -- that shouldn\'t be possible' return mod,rows = self.treeview.get_selection().get_selected_rows() if not de.getBoolean( label=_("Change all selected rows?"), sublabel=(_('This action will not be undoable. Are you that for all %s selected rows, you want to set the following values:')%len(rows) + (newdic.has_key('ingkey') and _('\nKey to %s')%newdic['ingkey'] or '') + (newdic.has_key('item') and _('\nItem to %s')%newdic['item'] or '') + (newdic.has_key('unit') and _('\nUnit to %s')%newdic['unit'] or '') + (newdic.has_key('amount') and _('\nAmount to %s')%newdic['amount'] or ''))): return # Now actually apply our lovely new logic... changed_iters = True updated_iters = [] for path in rows: itr=self.treeModel.get_iter(path) # We check to see if we've updated the parent of our iter, # in which case the changes would already be inherited by # the current row (i.e. if the tree has been expanded and # all rows have been selected). parent = mod.iter_parent(itr); already_updated = False while parent: if parent in updated_iters: already_updated = True else: parent = mod.iter_parent(parent) if already_updated: continue # Now that we're sure we really need to update... curdic,field = self.get_dic_describing_iter(itr) curkey = self.treeModel.get_value(itr,self.VALUE_COL) if not already_updated: self.rd.update_by_criteria( self.rd.ingredients_table, curdic, newdic, ) if curdic.has_key('ingkey') and newdic.has_key('ingkey'): self.rd.delete_by_criteria( self.rd.keylookup_table, {'ingkey':curdic['ingkey']} ) self.resetTree()
def email_selected (self, *args): recs = self.get_selected_recs() l = len(recs) if l > 20: if not de.getBoolean( title=_('Email recipes'), # only called for l>20, so fancy gettext methods # shouldn't be necessary if my knowledge of # linguistics serves me sublabel=_('Do you really want to email all %s selected recipes?')%l, custom_yes=_('Yes, e_mail them'), cancel=False, ): return re = RecipeEmailer(recs) re.send_email_with_attachments()
def tree_edited (self, renderer, path_string, text, n, head): indices = path_string.split(':') path = tuple( map(int, indices)) itr = self.treeModel.get_iter(path) curdic,field = self.get_dic_describing_iter(itr) value = curdic[field] if value == text: return if field=='ingkey': key = curdic['ingkey'] if de.getBoolean(label=_('Change all keys "%s" to "%s"?')%(key,text), sublabel=_("You won't be able to undo this action. If there are already ingredients with the key \"%s\", you won't be able to distinguish between those items and the items you are changing now."%text) ): self.rd.update_by_criteria( self.rd.ingredients_table, curdic, {'ingkey':text} ) self.rd.delete_by_criteria( self.rd.keylookup_table, {'ingkey':key} ) elif field=='item': if de.getBoolean(label=_('Change all items "%s" to "%s"?')%(curdic['item'],text), sublabel=_("You won't be able to undo this action. If there are already ingredients with the item \"%s\", you won't be able to distinguish between those items and the items you are changing now.")%text ): self.rd.update_by_criteria( self.rd.ingredients_table, curdic, {'item':text} ) elif field=='unit': unit = curdic['unit']; key = curdic['ingkey']; item = curdic['item'] val = de.getRadio(label='Change unit', options=[ [_('Change _all instances of "%(unit)s" to "%(text)s"')%locals(),1], [_('Change "%(unit)s" to "%(text)s" only for _ingredients "%(item)s" with key "%(key)s"')%locals(),2], ], default = 2, ) if val==1: self.rd.update_by_criteria( self.rd.ingredients_table, {'unit':unit}, {'unit':text}, ) elif val==2: self.rd.update_by_criteria( self.rd.ingredients_table, curdic, {'unit':text} ) elif field=='amount': amount = curdic['amount']; unit = curdic['unit']; key = curdic['ingkey']; item = curdic['item'] try: new_amount = convert.frac_to_float(text) except: de.show_amount_error(text) return val = de.getRadio(label='Change amount', options=[ [_('Change _all instances of "%(amount)s" %(unit)s to %(text)s %(unit)s')%locals(),1], [_('Change "%(amount)s" %(unit)s to "%(text)s" %(unit)s only _where the ingredient key is %(key)s')%locals(),2], [_('Change "%(amount)s" %(unit)s to "%(text)s" %(unit)s only where the ingredient key is %(key)s _and where the item is %(item)s')%locals(),3], ], default=3, ) if val == 1: cond = {'unit':unit,'amount':amount} elif val == 2: cond = {'unit':unit,'amount':amount,'ingkey':key} elif val == 3: cond = curdic self.rd.update_by_criteria( self.rd.ingredients_table, {'unit':unit,'amount':convert.frac_to_float(amount)}, {'unit':unit,'amount':new_amount} ) else: return self.treeModel.set_value(itr, n, text) return
def tree_edited(self, renderer, path_string, text, n, head): indices = path_string.split(':') path = tuple(map(int, indices)) itr = self.treeModel.get_iter(path) curdic, field = self.get_dic_describing_iter(itr) value = curdic[field] if value == text: return if field == 'ingkey': key = curdic['ingkey'] if de.getBoolean( label=_('Change all keys "%s" to "%s"?') % (key, text), sublabel=_( "You won't be able to undo this action. If there are already ingredients with the key \"%s\", you won't be able to distinguish between those items and the items you are changing now." % text)): self.rd.update_by_criteria(self.rd.ingredients_table, curdic, {'ingkey': text}) self.rd.delete_by_criteria(self.rd.keylookup_table, {'ingkey': key}) elif field == 'item': if de.getBoolean( label=_('Change all items "%s" to "%s"?') % (curdic['item'], text), sublabel= _("You won't be able to undo this action. If there are already ingredients with the item \"%s\", you won't be able to distinguish between those items and the items you are changing now." ) % text): self.rd.update_by_criteria(self.rd.ingredients_table, curdic, {'item': text}) elif field == 'unit': unit = curdic['unit'] key = curdic['ingkey'] item = curdic['item'] val = de.getRadio( label='Change unit', options=[ [ _('Change _all instances of "%(unit)s" to "%(text)s"') % locals(), 1 ], [ _('Change "%(unit)s" to "%(text)s" only for _ingredients "%(item)s" with key "%(key)s"' ) % locals(), 2 ], ], default=2, ) if val == 1: self.rd.update_by_criteria( self.rd.ingredients_table, {'unit': unit}, {'unit': text}, ) elif val == 2: self.rd.update_by_criteria(self.rd.ingredients_table, curdic, {'unit': text}) elif field == 'amount': amount = curdic['amount'] unit = curdic['unit'] key = curdic['ingkey'] item = curdic['item'] try: new_amount = frac_to_float(text) except: de.show_amount_error(text) return val = de.getRadio( label='Change amount', options=[ [ _('Change _all instances of "%(amount)s" %(unit)s to %(text)s %(unit)s' ) % locals(), 1 ], [ _('Change "%(amount)s" %(unit)s to "%(text)s" %(unit)s only _where the ingredient key is %(key)s' ) % locals(), 2 ], [ _('Change "%(amount)s" %(unit)s to "%(text)s" %(unit)s only where the ingredient key is %(key)s _and where the item is %(item)s' ) % locals(), 3 ], ], default=3, ) if val == 1: cond = {'unit': unit, 'amount': amount} elif val == 2: cond = {'unit': unit, 'amount': amount, 'ingkey': key} elif val == 3: cond = curdic self.rd.update_by_criteria(self.rd.ingredients_table, { 'unit': unit, 'amount': frac_to_float(amount) }, { 'unit': unit, 'amount': new_amount }) else: return self.treeModel.set_value(itr, n, text) return
def applyEntriesCB(self, *args): newdic = {} for k, e in list(self.entries.items()): txt = e.get_text() if txt: if k == 'amount': try: newdic[k] = convert.frac_to_float(txt) except: print('Problem with amount:', txt) import traceback traceback.print_exc() de.show_amount_error(txt) return else: newdic[k] = txt if not newdic: print( 'We called applyEntriesCB with no text -- that shouldn\'t be possible' ) return mod, rows = self.treeview.get_selection().get_selected_rows() if not de.getBoolean( label=_("Change all selected rows?"), sublabel= (_('This action will not be undoable. Are you that for all %s selected rows, you want to set the following values:' ) % len(rows) + ('ingkey' in newdic and _('\nKey to %s') % newdic['ingkey'] or '') + ('item' in newdic and _('\nItem to %s') % newdic['item'] or '') + ('unit' in newdic and _('\nUnit to %s') % newdic['unit'] or '') + ('amount' in newdic and _('\nAmount to %s') % newdic['amount'] or ''))): return # Now actually apply our lovely new logic... changed_iters = True updated_iters = [] for path in rows: itr = self.treeModel.get_iter(path) # We check to see if we've updated the parent of our iter, # in which case the changes would already be inherited by # the current row (i.e. if the tree has been expanded and # all rows have been selected). parent = mod.iter_parent(itr) already_updated = False while parent: if parent in updated_iters: already_updated = True else: parent = mod.iter_parent(parent) if already_updated: continue # Now that we're sure we really need to update... curdic, field = self.get_dic_describing_iter(itr) curkey = self.treeModel.get_value(itr, self.VALUE_COL) if not already_updated: self.rd.update_by_criteria( self.rd.ingredients_table, curdic, newdic, ) if 'ingkey' in curdic and 'ingkey' in newdic: self.rd.delete_by_criteria(self.rd.keylookup_table, {'ingkey': curdic['ingkey']}) self.resetTree()