def old_create_wiki(wiki): tags = wiki['tags'] admin = User.objects.first() root = Article.objects.first().urlpath_set.first() slug_list = deque(wiki["root"].split("/")[1:]) parent = traverse_ancestors(root, slug_list) # create Article article = Article(owner=admin, ) article.save() # create URLPath urlpath = URLPath(parent=parent, article=article, slug=wiki['slug'], site_id=1) urlpath.save() # add URLPath to Article article.urlpath_set.add(urlpath) article_revision = ArticleRevision( article=article, title=wiki['name'], #long_name=wiki['long_name'], content=wiki['content'], revision_number=1) # save wiki article_revision.save()
def update(article, content, user=None, message="", title=None): """ Update an article, creating a new revision. Args: article (Article): the article object. content (str): the new content of the article. user (Account, optional): the user responsible for the update. message (str, optional): the message of the update. title (str, optional): the new article's title. Note: If `user` isn't set, get the account #1 (the superuser). If `title` is unset, will use the current article's title. """ user = user or AccountDB.objects.get(id=1) current = article.current_revision title = title if title is not None else current.title revision = ArticleRevision() revision.inherit_predecessor(article) revision.title = title revision.content = content revision.user_message = message revision.deleted = False revision.ip_address = "127.0.0.1" article.add_revision(revision) return revision
def test_articlerevision_presave_signals(self): a = Article.objects.create() ar1 = ArticleRevision(article=a, title="revision1") a.add_revision(ar1) self.assertEqual(ar1, a.current_revision) ar2 = ArticleRevision(article=a, title="revision2") ar2.save() self.assertEqual(ar2.previous_revision, ar1)
def post(self, request): """ POST /api/user/v1/accounts/retire_misc/ { 'username': '******' } Retires the user with the given username in the LMS. """ username = request.data['username'] try: retirement = UserRetirementStatus.get_retirement_for_retirement_action( username) RevisionPluginRevision.retire_user(retirement.user) ArticleRevision.retire_user(retirement.user) PendingNameChange.delete_by_user_value(retirement.user, field='user') PasswordHistory.retire_user(retirement.user.id) course_enrollments = CourseEnrollment.objects.filter( user=retirement.user) ManualEnrollmentAudit.retire_manual_enrollments( course_enrollments, retirement.retired_email) CreditRequest.retire_user(retirement) ApiAccessRequest.retire_user(retirement.user) CreditRequirementStatus.retire_user(retirement) # This signal allows code in higher points of LMS to retire the user as necessary USER_RETIRE_LMS_MISC.send(sender=self.__class__, user=retirement.user) # This signal allows code in higher points of LMS to unsubscribe the user # from various types of mailings. USER_RETIRE_MAILINGS.send(sender=self.__class__, email=retirement.original_email, new_email=retirement.retired_email, user=retirement.user) except UserRetirementStatus.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) except RetirementStateError as exc: return Response(text_type(exc), status=status.HTTP_400_BAD_REQUEST) except Exception as exc: # pylint: disable=broad-except return Response(text_type(exc), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(status=status.HTTP_204_NO_CONTENT)
def test_manager(self): article = Article() article.add_revision(ArticleRevision(title="Root", content="Hello\nworld"), save=True) self.assertEqual("Hello\r\nworld", article.current_revision.content)
def form_valid(self, form): """Create a new article revision for the bot wiki page when the form is valid""" # If the article content is different, add a new revision if form.instance.wiki_article.current_revision.content != form.cleaned_data['wiki_article_content']: revision = ArticleRevision() revision.inherit_predecessor(form.instance.wiki_article) revision.title = form.instance.name revision.content = form.cleaned_data['wiki_article_content'] # revision.user_message = form.cleaned_data['summary'] revision.deleted = False revision.set_from_request(self.request) form.instance.wiki_article.add_revision(revision) return super(BotUpdate, self).form_valid(form)
def post(self, request): """ POST /api/user/v1/accounts/retire_misc/ { 'username': '******' } Retires the user with the given username in the LMS. """ username = request.data['username'] try: retirement = UserRetirementStatus.get_retirement_for_retirement_action(username) RevisionPluginRevision.retire_user(retirement.user) ArticleRevision.retire_user(retirement.user) PendingNameChange.delete_by_user_value(retirement.user, field='user') PasswordHistory.retire_user(retirement.user.id) course_enrollments = CourseEnrollment.objects.filter(user=retirement.user) ManualEnrollmentAudit.retire_manual_enrollments(course_enrollments, retirement.retired_email) CreditRequest.retire_user(retirement.original_username, retirement.retired_username) ApiAccessRequest.retire_user(retirement.user) CreditRequirementStatus.retire_user(retirement.user.username) # This signal allows code in higher points of LMS to retire the user as necessary USER_RETIRE_LMS_MISC.send(sender=self.__class__, user=retirement.user) # This signal allows code in higher points of LMS to unsubscribe the user # from various types of mailings. USER_RETIRE_MAILINGS.send( sender=self.__class__, email=retirement.original_email, new_email=retirement.retired_email, user=retirement.user ) except UserRetirementStatus.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) except RetirementStateError as exc: return Response(text_type(exc), status=status.HTTP_400_BAD_REQUEST) except Exception as exc: # pylint: disable=broad-except return Response(text_type(exc), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(status=status.HTTP_204_NO_CONTENT)
def add_revision(self, newrevision, request, article_revision=None, save=True): """ Given a revision to make to the metadata, save it, and create a new article revision so they are in parallel unless one is provided. If an article revision is provided, then link the metadata revision to it; otherwise, initially save metadata_revision.article_revision as null (the signal triggered by the new article revision being saved will lead to this method being called again with the article_revision). """ if article_revision is None: # first save the submitted metadata. article_revision will be null super(Metadata, self).add_revision(newrevision, save=save) # create article revision article_revision = ArticleRevision() article_revision.inherit_predecessor(self.article) article_revision.set_from_request(request) article_revision.automatic_log = newrevision.automatic_log self.article.add_revision(article_revision, save=save) # will trigger the on_article_revision_pre_save signal handler, # which calls this method again, supplying an article_revision else: # update the metadata revision so it is attached to an article_revision newrevision.article = self.article # defined in ArticlePlugin, which SimplePlugin and RevisionPlugin both inherit from newrevision.article_revision = article_revision super(Metadata, self).add_revision(newrevision, save=save)
def post(self, request): """ POST /api/user/v1/accounts/retire_misc/ { 'username': '******' } Retires the user with the given username in the LMS. """ username = request.data['username'] if is_username_retired(username): return Response(status=status.HTTP_404_NOT_FOUND) try: retirement = UserRetirementStatus.get_retirement_for_retirement_action( username) RevisionPluginRevision.retire_user(retirement.user) ArticleRevision.retire_user(retirement.user) PendingNameChange.delete_by_user_value(retirement.user, field='user') PasswordHistory.retire_user(retirement.user.id) course_enrollments = CourseEnrollment.objects.filter( user=retirement.user) ManualEnrollmentAudit.retire_manual_enrollments( course_enrollments, retirement.retired_email) CreditRequest.retire_user(retirement.original_username, retirement.retired_username) ApiAccessRequest.retire_user(retirement.user) CreditRequirementStatus.retire_user(retirement.user.username) SurveyAnswer.retire_user(retirement.user.id) except UserRetirementStatus.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) except RetirementStateError as exc: return Response(text_type(exc), status=status.HTTP_400_BAD_REQUEST) except Exception as exc: # pylint: disable=broad-except return Response(text_type(exc), status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(status=status.HTTP_204_NO_CONTENT)
def create_bot_wiki_article(self): article_kwargs = {'owner': self.user, 'group': None, 'group_read': True, 'group_write': False, 'other_read': True, 'other_write': False} article = Article(**article_kwargs) article.add_revision(ArticleRevision(title=self.name), save=True) article.save() self.wiki_article = article
def save(self, *args, **kwargs): self.instance = self.article.category data = self.cleaned_data oldparent = self.instance.parent newparent = data['parent'] if newparent != oldparent: # parent has changed # make a new revision of the article to record the change revision = ArticleRevision() revision.inherit_predecessor(self.article) revision.set_from_request(self.request) revision.parent = newparent revision.categories = self.article.categories if oldparent: oldparent = oldparent.name revision.automatic_log = f'Parent Category: {oldparent} → {newparent.name if newparent else "None"} (not revertible)' self.article.add_revision(revision) # reverting to a previous revision from the Changes page currently does not affect the category parent self.instance.parent = newparent self.instance.save() super(EditCategoryForm, self).save(*args, **kwargs)
def setUp(self): super().setUp() self.root_article.add_revision(ArticleRevision(title="New Revision"), save=True) self.new_revision = self.root_article.current_revision