def post(self, request, modelType, objectID): # User has submitted new/reply comment - validate and save to DB. obj, con_type = BioGPSModel.get_object_and_ctype(modelType, pk=objectID) comment_form = BiogpsThreadedCommentForm(target_object=obj, parent=request.POST['parent'], data=request.POST) if comment_form.is_valid(): #parent_link = request.META['HTTP_REFERER'] user_comment = comment_form.cleaned_data['comment'] username = getattr(request.user, 'username') c = ThreadedComment() c.user_name = username c.user_id = getattr(request.user, 'id') c.comment = user_comment cleaned_parent = comment_form.cleaned_data['parent'] if cleaned_parent is not None: #parent_link += '#%s' % cleaned_parent c.parent = get_object_or_404(ThreadedComment, pk=cleaned_parent) c.object_pk = comment_form.cleaned_data['object_pk'] c.content_type = con_type c.site = Site.objects.get_current() c.save() # Use new comment ID in link if no parent #if parent_link.find('#') == -1: # parent_link += '#%s' % c.id # If missing request HTTP_REFERER re-direct to landing page http_referer = 'http://biogps.org/%s/%s/' % (modelType, objectID) try: http_referer = request.META['HTTP_REFERER'] except KeyError: # No referer, possibly b/c of private browsing pass # If comment was made on plugin email owner bcc admins if con_type.model == 'biogpsplugin': from django.core.mail import EmailMessage msg = EmailMessage('[BioGPS] New comment on plugin: %s' % obj.title, 'New comment:<br><br>%s<br> '\ ' -posted by %s<br><br>Link: %s' % (user_comment, request.user.get_valid_name(), http_referer), # request.user.get_valid_name(), parent_link), settings.DEFAULT_FROM_EMAIL, [obj.owner.email], [i[1] for i in settings.ADMINS]) msg.content_subtype = "html" msg.send(fail_silently=False) # Re-direct to parent comment that user replied to. #return HttpResponseRedirect(parent_link) return HttpResponseRedirect(http_referer) else: return HttpResponse("A comment is required. Please use your\ browser's back button to edit your comment.")
def handle_comments(self): """ Converts all legacy ``Comment`` objects into ``ThreadedComment`` objects. """ comments = Comment.objects.all() for c in comments: new = ThreadedComment(content_type=c.content_type, object_id=c.object_id, comment=c.comment, user=c.user, date_submitted=c.submit_date, date_modified=c.submit_date, date_approved=c.submit_date, is_public=c.is_public, ip_address=c.ip_address, is_approved=not c.is_removed) new.save()
def handle_comments(self): """ Converts all legacy ``Comment`` objects into ``ThreadedComment`` objects. """ comments = Comment.objects.all() for c in comments: new = ThreadedComment( content_type = c.content_type, object_id = c.object_id, comment = c.comment, user = c.user, date_submitted = c.submit_date, date_modified = c.submit_date, date_approved = c.submit_date, is_public = c.is_public, ip_address = c.ip_address, is_approved = not c.is_removed ) new.save()
def import_comments_list(comments, obj, parent=None): # import comments to a database count = 0 for c in comments: # consider comments with equal date to be the same # this makes possible to update all other comment fields try: comment = ThreadedComment.objects.get( submit_date=c['published']) except ThreadedComment.DoesNotExist: comment_pk = None except ThreadedComment.MultipleObjectsReturned: logger.error( "more then one comment with the same " "publish date: %s", c['published']) continue else: comment_pk = comment.pk comment = ThreadedComment( pk=comment_pk, content_type=ContentType.objects.get_for_model(obj.__class__), object_pk=unicode(obj.pk), user_name=c['author'], user_url=c.get('website', ''), comment=c['content'], submit_date=c['published'], site_id=settings.SITE_ID, is_public=True, is_removed=False, parent=parent, comment_html=safe_markdown(c['content'])) comment.save() count += 1 if 'replies' in c: count += import_comments_list(c['replies'], obj, comment) return count
def import_comments_list(comments, obj, parent=None): # import comments to a database count = 0 for c in comments: # consider comments with equal date to be the same # this makes possible to update all other comment fields try: comment = ThreadedComment.objects.get( submit_date=c['published']) except ThreadedComment.DoesNotExist: comment_pk = None except ThreadedComment.MultipleObjectsReturned: logger.error("more then one comment with the same " "publish date: %s", c['published']) continue else: comment_pk = comment.pk comment = ThreadedComment( pk=comment_pk, content_type=ContentType.objects.get_for_model( obj.__class__), object_pk=unicode(obj.pk), user_name=c['author'], user_url=c.get('website', ''), comment=c['content'], submit_date=c['published'], site_id=settings.SITE_ID, is_public=True, is_removed=False, parent=parent, comment_html=safe_markdown(c['content'])) comment.save() count += 1 if 'replies' in c: count += import_comments_list(c['replies'], obj, comment) return count
def handle(self, *args, **options): transaction.commit_unless_managed() transaction.enter_transaction_management() transaction.managed(True) site = Site.objects.all()[0] cursor = connection.cursor() cursor.execute(FREE_SQL) for row in cursor: (content_type_id, object_id, parent_id, name, website, email, date_submitted, date_modified, date_approved, comment, markup, is_public, is_approved, ip_address) = row tc = ThreadedComment( content_type_id=content_type_id, object_pk=object_id, user_name=name, user_email=email, user_url=website, comment=comment, submit_date=date_submitted, ip_address=ip_address, is_public=is_public, is_removed=not is_approved, parent_id=parent_id, site=site, ) tc.save(skip_tree_path=True) cursor = connection.cursor() cursor.execute(USER_SQL) for row in cursor: (content_type_id, object_id, parent_id, user_id, date_submitted, date_modified, date_approved, comment, markup, is_public, is_approved, ip_address) = row tc = ThreadedComment( content_type_id=content_type_id, object_pk=object_id, user_id=user_id, comment=comment, submit_date=date_submitted, ip_address=ip_address, is_public=is_public, is_removed=not is_approved, parent_id=parent_id, site=site, ) tc.save(skip_tree_path=True) for comment in ThreadedComment.objects.all(): path = [str(comment.id).zfill(PATH_DIGITS)] current = comment while current.parent: current = current.parent path.append(str(current.id).zfill(PATH_DIGITS)) comment.tree_path = PATH_SEPARATOR.join(reversed(path)) comment.save(skip_tree_path=True) if comment.parent: ThreadedComment.objects.filter(pk=comment.parent.pk).update( last_child=comment) transaction.commit() transaction.leave_transaction_management()
def threadedcomment_can_delete(self, **kwargs): project = self.content_object user = kwargs.get("user") return project.can_delete(user=user) def threadedcomment_absolute_deeplink_url(self, **kwargs): if not hasattr(self.content_object, "get_absolute_url"): return None else: # we have an absolute url parent_id = self.parent_id if self.parent_id is not None else self.pk return "{path}#/discussion/{parent_id}".format(path=self.content_object.get_absolute_url(), parent_id=parent_id) ThreadedComment.add_to_class("can_read", threadedcomment_can_read) ThreadedComment.add_to_class("can_edit", threadedcomment_can_edit) ThreadedComment.add_to_class("can_delete", threadedcomment_can_delete) ThreadedComment.add_to_class("absolute_deeplink_url", threadedcomment_absolute_deeplink_url) ThreadedComment.add_to_class("tags", TaggableManager(blank=True)) """ Import our on_user_logged_in """ from .signals import on_user_logged_in
def handle(self, *args, **options): with open('media/csv/users.csv') as csvfile: reader = csv.DictReader(csvfile) for row in reader: user = User() user.id = int(row['ID']) user.username = row['user_login'] user.email = row['user_email'] user.date_joined = row['user_registered'] user.first_name = row['display_name'].split(' ')[0] user.last_name = row['display_name'].split(' ')[-1] user.save() with open('media/csv/posts.csv') as csvfile: reader = csv.DictReader(csvfile) for row in reader: post = Post() post.id = int(row['ID']) post.title = row['post_title'] post.content = row['post_content'] post.published_at = row['post_date'] post.slug = row['post_name'] post.user_id = row['post_author'] post.save() with open('media/csv/comments.csv') as csvfile: reader = csv.DictReader(csvfile) for row in reader: comment = ThreadedComment() comment.id = int(row['comment_ID']) comment.object_pk = row['comment_post_ID'] comment.content_type_id = 15 comment.site_id = 1 comment.user_name = row['comment_author'] comment.user_email = row['comment_author_email'] comment.user_url = row['comment_author_url'] comment.ip_address = row['comment_author_IP'] comment.submit_date = row['comment_date'] comment.comment = row['comment_content'] if row['comment_parent'] != '0': comment.parent_id = row['comment_parent'] if row['user_id'] != '0': comment.user_id = row['user_id'] comment.save()
def post(self, request, *args, **kwargs): """Start a discussion of an arbitrary model instance.""" title = request.POST['comment_html'] comment = request.POST.get('comment', '') # Find the object we're discussing. model = request.POST['model'] the_content_type = ContentType.objects.get( app_label=request.POST['app_label'], model=model) assert the_content_type is not None the_object = the_content_type.get_object_for_this_type( pk=request.POST['obj_pk']) assert the_object is not None try: obj_sc = Collaboration.objects.get_for_object(the_object) except Collaboration.DoesNotExist: obj_sc = Collaboration() # TODO: populate this collab with sensible auth defaults. obj_sc.content_object = the_object obj_sc.save() # sky: I think what I want to do is have the ThreadedComment # point to the_object # and the collaboration will point to the threaded root comment # that way, whereas, if we live in Collaboration-land, # we can get to ThreadedComments # threaded comments can also live in it's own world without 'knowing' # about SC OTOH, threaded comment shouldn't be able # to point to the regular object # until Collaboration says it's OK (i.e. has permissions) # ISSUE: how to migrate? (see models.py) # now create the CHILD collaboration object for the # discussion to point at. # This represents the auth for the discussion itself. collaboration_context = cached_course_collaboration(request.course) disc_sc = Collaboration( _parent=obj_sc, title=title, context=collaboration_context, ) disc_sc.set_policy(request.POST.get('publish', None)) disc_sc.save() # finally create the root discussion object, pointing it at the CHILD. new_threaded_comment = ThreadedComment(parent=None, title=title, comment=comment, user=request.user, content_object=disc_sc) # TODO: find the default site_id new_threaded_comment.site_id = 1 new_threaded_comment.save() disc_sc.content_object = new_threaded_comment disc_sc.save() DiscussionIndex.update_class_references( new_threaded_comment.comment, new_threaded_comment.user, new_threaded_comment, new_threaded_comment.content_object, new_threaded_comment.user) if not request.is_ajax(): if model == 'project': discussion_url = reverse('project-workspace', args=(request.course.pk, the_object.pk)) else: discussion_url = reverse('discussion-view', args=(request.course.pk, new_threaded_comment.id)) return HttpResponseRedirect(discussion_url) else: vocabulary = VocabularyResource().render_list( request, Vocabulary.objects.filter(course=request.course)) user_resource = UserResource() owners = user_resource.render_list(request, request.course.members) data = { 'panel_state': 'open', 'panel_state_label': "Instructor Feedback", 'template': 'discussion', 'owners': owners, 'vocabulary': vocabulary, 'context': threaded_comment_json(request, new_threaded_comment) } return HttpResponse(json.dumps(data, indent=2), content_type='application/json')
Updates the parent topic's score for the featured posts list """ topic = instance.content_object topic.update_score(settings.FEATURED_REPLY_SCORE) post_save.connect(update_scores, sender=ThreadedComment, dispatch_uid='updatetopicreplyscore') def update_reply_date(sender, instance, created, **kwargs): """ Updates the parent topic's "last reply" date """ if created: topic = instance.content_object topic.last_reply = datetime.now() topic.save() post_save.connect(update_reply_date, sender=ThreadedComment, dispatch_uid='updatetopicreplydate') # add an the coniverted field directly to the ThreadedComment model ThreadedComment.add_to_class('converted', models.BooleanField(default=True)) # and do wiki-to-HTML conversion as needed def threadedcomment_init(self, *args, **kwargs): super(ThreadedComment, self).__init__(*args, **kwargs) # wiki parse if needed if self.pk and not self.converted and self.comment: self.comment = wiki_convert(self.comment) self.converted = True self.save() ThreadedComment.__init__ = threadedcomment_init
def import_place_comment(content_type, place): comment = ThreadedComment() comment.content_type = content_type comment.site_id = settings.SITE_ID