def add_collections_to_sender_form(sender, initial): form = sender collection_choices = tuple([ ( str(collection['pk']), collection['name'] ) for collection in Collection.query().values('pk', 'name', ) if collection['name'] ]) form.fields['collections'] = london.forms.ChoiceField(name='collections', widget=london.forms.SelectMultiple, choices=collection_choices, required=False) # FIXME: initial should already have collections initial['collections'] = [str(collection['pk']) for collection in Collection.query().filter(items__contains=form.instance.pk)]
def _get_collection_items(name): result = [] try: for path in APPS_FOR_COLLECTION_APP: pk_items = Collection.query().get(name=name)['items'] items = get_model(path).query().filter(pk__in=pk_items) result.extend(items) if items.count() == len(pk_items): # all items are of one model: no need to search items in other models break except: pass return result
def save_collections_from_sender_form(sender): form = sender obj_pk = str(form.instance['pk']) for choice in form.fields['collections'].choices: collection_pk = choice[0] collection = Collection.query().get(pk=collection_pk) collection_items = collection['items'] if collection_pk in form.cleaned_data['collections'] and obj_pk not in collection_items: collection_items.append(obj_pk) elif collection_pk not in form.cleaned_data['collections'] and obj_pk in collection_items: collection_items.remove(obj_pk) collection['items'] = collection_items collection.save()
def _collection(self, obj, caller): try: return "\r\n".join(Collection.query().get(name=obj)["items"] or []) except Collection.DoesNotExist: return ""
def save_collection_items_from_sender_form(sender): obj, new = Collection.query().get_or_create(name=sender.instance) obj['items'] = sender.cleaned_data['items'] obj.save()