def __init__(self, manual, recipe, account, *args, **kwargs): post = args[0] if len(args) > 0 else None # load scripts ( used when creating new recipe ) if 'scripts' in kwargs: scripts = [s for s in kwargs.pop('scripts').split(',') if s] else: scripts = [] # fetch the instance and load initial data self.instance = recipe or Recipe() self.setup = SetupForm(manual, account, post, instance=self.instance) super(ScriptForm, self).__init__(*args, **kwargs) # create blank forms self.blanks = [] for s in Script.get_scripts(account.email): if s.is_manual() == manual: self.blanks.append( ScriptJsonForm('[BLANK]', s, {}, prefix='%s_[BLANK]' % s.get_tag())) # processing incoming form self.forms = [] if post: for prefix in post.getlist('form_prefix'): tag, sequence = prefix.rsplit('_', 1) sequence = int(sequence) - 1 s = Script(tag) self.forms.append( ScriptJsonForm( sequence, s, self.instance.get_values()[sequence]['values'] if self.instance and sequence < len(self.instance.get_values()) else {}, post, prefix=prefix)) # loading from existing recipe elif self.instance: for sequence, script in enumerate(self.instance.get_values()): s = Script(script['tag']) self.forms.append( ScriptJsonForm(sequence + 1, s, script['values'], prefix='%s_%d' % (s.get_tag(), sequence + 1))) # starting a new recipe else: for sequence, script in enumerate(scripts): s = Script(script) self.forms.append( ScriptJsonForm(sequence + 1, s, [], prefix='%s_%d' % (s.get_tag(), sequence + 1)))
def __init__(self, recipe, account, *args, **kwargs): post = args[0] if len(args) > 0 else None # load scripts ( used when creating new recipe ) if 'scripts' in kwargs: scripts = [s for s in kwargs.pop('scripts').split(',') if s] else: scripts = [] # fetch the instance and load intial data self.instance = recipe or Recipe() self.setup = SetupForm(account, post, instance=self.instance) super(ScriptForm, self).__init__(*args, **kwargs) # group blanks by product self.products = {} for s in sorted(Script.get_scripts(account.email), key=lambda s: s.get_tag()): self.products.setdefault(s.get_product(), []) self.products[s.get_product()].append( ScriptJsonForm('[BLANK]', s, {}, prefix='%s_[BLANK]' % s.get_tag())) self.products = sorted([{ 'name': k, 'blanks': v } for k, v in self.products.items()], key=lambda p: p['name']) # processing incoming form self.forms = [] if post: for prefix in post.getlist('form_prefix'): tag, sequence = prefix.rsplit('_', 1) sequence = int(sequence) - 1 s = Script(tag) self.forms.append( ScriptJsonForm( sequence, s, self.instance.get_values()[sequence]['values'] if self.instance and sequence < len(self.instance.get_values()) else {}, post, prefix=prefix)) # loading from existing recipe elif self.instance: for sequence, script in enumerate(self.instance.get_values()): s = Script(script['tag']) self.forms.append( ScriptJsonForm(sequence + 1, s, script['values'], prefix='%s_%d' % (s.get_tag(), sequence + 1))) # starting a new recipe else: for sequence, script in enumerate(scripts): s = Script(script) self.forms.append( ScriptJsonForm(sequence + 1, s, [], prefix='%s_%d' % (s.get_tag(), sequence + 1)))