def post_comment(request, author=None, additional_fields=None): try: body = request.POST['body'] model_name = request.POST['owner_model'] owner_pk = request.POST['owner_id'] owner_model = get_model("%s.%s" % (MODELS_WITH_COMMENTS[model_name], model_name)) owner = owner_model.query().get(pk = owner_pk) if not author: author = request.user if request.user.is_authenticated() else None fields = additional_fields if isinstance(additional_fields,dict) else {} fields['owner'] = owner fields['body'] = body fields['author'] = author comment = Comment(_save=True, **fields) signals.post_comment.send(sender=owner, request=request, comment=comment) return JsonResponse({ "status":"ok", "pk": comment['pk'], "author": unicode(author) if author else "Anonymous", "body": comment['body'], "created": comment['created'].strftime('%Y-%m-%d, %H:%M'), }) except Exception as e: response = JsonResponse({"status":"error", "error":str(e).replace('"', '\\"')}) response.status_code = 500 return response
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 add_collection_items_to_sender_form(sender, initial): form = sender items_choices_from_app_settings = [] for path in app_settings.APPS_FOR_COLLECTION_APP: model = get_model(path) queryset = model.query() if 'site' in model._meta.fields and form.request.session[CURRENT_SITE_FILTER] != '': site = Site.query().get(pk = form.request.session[CURRENT_SITE_FILTER]) queryset = queryset.filter(site=site) for item in queryset: items_choices_from_app_settings.append((str(item['pk']), "%s: %s: %s" % (item._meta.app_label, item._meta.model_label, item.__unicode__() ))) form.fields['items'] = london.forms.ChoiceField(name='items', widget=london.forms.SelectMultiple, choices=tuple(items_choices_from_app_settings), required=False, help_text='app: model: item')
def __init__(self, obj): self.model = get_model(obj.pop("_model_class")) self.natural_key = obj.pop("_natural_key") self._fields = obj try: if hasattr(self.model.query(), "get_by_natural_key"): self.object = self.model.query().get_by_natural_key(**self.natural_key) else: self.object = self.model.query().get(**self.natural_key) except self.model.DoesNotExist: self.object = self.model() for k, v in self._fields.items(): self.object[k] = v