def handle(self, *args, **options): newsfeed = get_collection("newsfeed") newsfeed.ensure_index([ ("recipients", DESCENDING), ("date_created", DESCENDING), ]) newsfeed.remove() for user in User.objects.all(): newsfeed.insert({ "object_id": user.id, "news_type": NEWS_TYPE_REGISTRATION, "date_created": user.date_joined, "recipients": [], "sender": { "username": user.username, "email": user.email # it's required for gravatar }, }) for document in map(Document, get_collection("documents").find()): news_type = NEWS_TYPE_DOCUMENT if document.fork_of else NEWS_TYPE_FORK newsfeed.insert({ "object_id": document.pk, "news_type": news_type, "date_created": document.date_created, "recipients": [], "sender": { "username": document.user.username, "email": document.user.email # it's required for gravatar }, }) for comment in map(Comment, get_collection("comments").find()): newsfeed.insert({ "object_id": comment.pk, "news_type": NEWS_TYPE_COMMENT, "date_created": comment.date_created, "recipients": [], "sender": { "username": comment.user.username, "email": comment.user.email # it's required for gravatar }, }) print get_collection("newsfeed").count()
def get_context_data(self, **kwargs): documents = get_collection("documents") documents.ensure_index([ ("featured", 1), ("star_count", 1) ]) featured_documents = imap(Document, documents.find({ "featured": True }).sort([("date_created", -1)]).limit(9)) most_rated_documents = imap(Document, documents.find().sort([("star_count", -1)]).limit(9)) recently_added_documents = imap(Document, documents.find({ "$where": "this.entities && this.entities.length > 1" }).sort([("date_created", -1)]).limit(9)) return { "featured_documents": featured_documents, "most_rated_documents": most_rated_documents, "recently_added_documents": recently_added_documents, "search_form": SearchForm() }
def handle(self, *args, **options): get_collection("documents").ensure_index([ ("_keywords", 1), ("title", 1), ]) for document in get_collection("documents").find(): print document.get("_id") get_collection("documents").update({ "_id": document.get("_id") }, { "$set": { "_keywords": extract_keywords(document.get("title")) } })
def form_valid(self, form, **kwargs): self.object_id = get_collection("documents").insert({ "title": form.cleaned_data.get("title"), "user_id": self.request.user.pk, "date_created": datetime.now(), "entities": form.cleaned_data.get("entities"), "_keywords": extract_keywords(form.cleaned_data.get("title")) }) return super(NewDocumentView, self).form_valid(form)
def get_queryset(self): form = self.get_form() if not form.is_valid(): return [] keyword = form.cleaned_data.get("keyword") collection = get_collection("documents").find( {"_keywords": { "$all": keyword.split() }}) return map(Document, collection)
def get_context_data(self, **kwargs): documents = get_collection("documents") most_rated_documents = map( Document, documents.find().sort([("star_count", -1)]).limit(9)) recently_added_documents = map( Document, documents.find({ "$where": "this.entities && this.entities.length > 1" }).sort([("date_created", -1)]).limit(9)) return { "most_rated_documents": most_rated_documents, "recently_added_documents": recently_added_documents, "search_form": SearchForm() }
def notifications(request): """ Returns unread notification count """ if request.user.is_anonymous(): notification_count = 0 else: notification_count = get_collection("notifications").find({ "recipient": request.user.id, "is_read": False }).count() return {"notification_count": notification_count}
def get_queryset(self): form = self.get_form() if not form.is_valid(): return [] keyword = form.cleaned_data.get("keyword") collection = get_collection("documents").find({ "_keywords": { "$all": keyword.split() } }) return map(Document, collection)
def notifications(request): """ Returns unread notification count """ if request.user.is_anonymous(): notification_count = 0 else: notification_count = get_collection("notifications").find({ "recipient": request.user.id, "is_read": False }).count() return { "notification_count": notification_count }
def form_valid(self, form, **kwargs): resource = DocumentResource() document = self.get_document() self.object_id = get_collection("documents").insert({ "title": form.cleaned_data.get("title"), "user_id": self.request.user.pk, "entities": document.entities, "fork_of": document.pk, "date_created": datetime.now() }) # TODO: use atomic operations for incrementing! resource.obj_update(bundle=resource.build_bundle(data={ "fork_count": (document.fork_count or 0) + 1 }), pk=document.pk) return super(NewDocumentView, self).form_valid(form)
def comment_on(sender, comment_id, **kwargs): comment = Comment(get_collection("comments").find_one({ "_id": ObjectId(comment_id) })) document = comment.document send_mail( subject = "You have new comment(s) on your pattern", message = COMMENT_TEMPLATE % { "document_title": document.title, "document_link": settings.SITE_URL + document.get_absolute_url() }, from_email = settings.COMMENTS_FROM_EMAIL, recipient_list = ['"%s" <%s>' % ( document.user.get_full_name() or document.user.username, document.user.email)], fail_silently = True )
def comment_on(sender, comment_id, **kwargs): comment = Comment( get_collection("comments").find_one({"_id": ObjectId(comment_id)})) document = comment.document send_mail( subject="You have new comment(s) on your pattern", message=COMMENT_TEMPLATE % { "document_title": document.title, "document_link": settings.SITE_URL + document.get_absolute_url() }, from_email=settings.COMMENTS_FROM_EMAIL, recipient_list=[ '"%s" <%s>' % (document.user.get_full_name() or document.user.username, document.user.email) ], fail_silently=True)
def form_valid(self, form, **kwargs): resource = DocumentResource() document = self.get_document() self.object_id = get_collection("documents").insert({ "title": form.cleaned_data.get("title"), "user_id": self.request.user.pk, "entities": document.entities, "fork_of": document.pk, "date_created": datetime.now(), "_keywords": extract_keywords(form.cleaned_data.get("title")) }) # TODO: use atomic operations for incrementing! resource.obj_update(bundle=resource.build_bundle( data={"fork_count": (document.fork_count or 0) + 1}), pk=document.pk) return super(NewDocumentView, self).form_valid(form)
def get_collection(self): return get_collection("comments")
def comment_count(self): return get_collection("comments").find({ "document_id": self.pk }).count()
def forks(self): return map(self.__class__, get_collection("documents").find({ "fork_of": self.pk }))
def get_collection(self): return get_collection("documents")
def load(self): self.collection = get_collection("documents") self.collection.ensure_index([ ("date_created", DESCENDING), ])
def get_queryset(self): collection = get_collection("documents").find( {"user_id": self.request.user.pk}) return map(Document, collection)
def document(self): return Document( get_collection("documents").find_one( {"_id": ObjectId(self.document_id)}))
def forks(self): """Returns forks of document""" forks = get_collection("documents").find({"fork_of": self.pk}) return map(Document, forks)
def load(self): self.collection = get_collection("notifications") self.collection.ensure_index([ ("date_created", DESCENDING), ])
def document(self): """Returns the document of comment""" query = {"_id": ObjectId(self.document_id)} return Document(get_collection("documents").find_one(query))
def document(self): return Document(get_collection("documents").find_one({ "_id": ObjectId(self.document_id) }))
def get(self, **kwargs): return Comment(get_collection("comments").find_one(kwargs))
def load(self): self.collection = get_collection("newsfeed")
def forks(self): return map(self.__class__, get_collection("documents").find({"fork_of": self.pk}))
def comment_count(self): """Returns the comment count of document""" comments = get_collection("comments") return comments.find({"document_id": self.pk}).count()
def get_queryset(self): resource = DocumentResource() collection = get_collection("documents").find({"user_id": self.request.user.pk}) return map(Document, collection)
def load(self): """Reloads mongodb connection""" self.collection = get_collection("documents") self.collection.ensure_index([ ("date_created", DESCENDING), ])