Exemplo n.º 1
0
 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()
Exemplo n.º 2
0
 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()
Exemplo n.º 4
0
    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')
Exemplo n.º 5
0
def import_place_comment(content_type, place):
    comment = ThreadedComment()
    comment.content_type = content_type
    comment.site_id = settings.SITE_ID