示例#1
0
 def input_edit_data(self, item):
     data = {'own': {}, 'relation': {}}
     for (key, field) in self.model:
         if not field.updatable:
             continue
         if isinstance(field, ForeignKeyField):
             if Menu.yes_no_question('Change %s?' %
                                     field.relationship.name):
                 foreign_key = Relationship.select_foreign_key_relationship(
                     field.relationship)
                 if foreign_key:
                     data['own'][key] = foreign_key
             else:
                 data['own'][key] = item[key]
         elif isinstance(field, OneToManyField):
             foreign_key = Relationship.select_foreign_key_relationship_inverse(
                 field.relationship)
             if foreign_key:
                 data['relation'][key] = foreign_key
             else:
                 data['relation'][key] = item[key]
             # Todo: handle removing relationships
         else:
             data['own'][key] = self.update_field(item[key],
                                                  Style.make_label(key))
     return data
示例#2
0
 def select_foreign_key_relationship_inverse(relationship):
     foreign_keys = []
     repository = relationship.repository()
     while Menu.yes_no_question('Add %s' % relationship.name):
         print(Style.create_title('Select %s' % relationship.name))
         paginated_menu = relationship.paginated_menu or Pagination(
             repository)
         item = paginated_menu()
         if item:
             foreign_keys.append(item['id'])
         else:
             print('\nNo relationship added\n')
     return relationship.related_name, foreign_keys