Ejemplo n.º 1
0
def add_post(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST)

        if form.is_valid():
            new_post = BlogPost(
                title=form.cleaned_data['title'],
                content=form.cleaned_data['content'],
                published=True,
                author=request.user
            )

            new_post.save()

            return redirect('view_posts', post_id=new_post.id)
    else:
        form = BlogPostForm()

    return render(
        request,
        'add_post.html',
        {
            'form': form,
        }
    )
Ejemplo n.º 2
0
def recover(request):
    xmldoc = dom.parse(dazhu.settings.BASE_DIR + '/backup.xml')
    itemlist = xmldoc.getElementsByTagName('blog')
    result = "over"
    tools.debug("start import")
    for item in itemlist:
        tempguid = item.attributes['guid'].value
        tools.debug("current guid is ", tempguid)
        try:
            tempblog = BlogPost.objects.get(guid=tempguid)
        except:
            tools.debug(tempguid, "tempguid is exist")
            result += tempguid + " imported <br>"
            tempblog = BlogPost()
            tempblog.guid = item.attributes["guid"].value
            tempblog.author = item.attributes["author"].value
            tools.debug("author ", tempblog.author)
            tempblog.title = item.attributes["title"].value
            tempblog.category = item.attributes["category"].value
            tempblog.timestamp = datetime.strptime(
                item.attributes["timestamp"].value, "%Y%m%d %H%M%S")

            bodynode = item.getElementsByTagName('value')[0]
            tempblog.body = bodynode.firstChild.nodeValue
            tools.debug("value ", tempblog.body)

            tempblog.save()

            attachments = item.getElementsByTagName('attachment')
            for atts in attachments:
                rndName = atts.attributes["rndName"].value
                sourceName = atts.attributes["sourceName"].value

                tempAttachment = attachment()
                tempAttachment.blog = tempblog
                tempAttachment.sourceName = sourceName
                tempAttachment.rndName = rndName
                tempAttachment.save()

            cmts = item.getElementsByTagName('comment')
            for cmt in cmts:
                author = cmt.attributes["author"].value
                body = cmt.attributes["body"].value
                timestamp = datetime.strptime(
                    cmt.attributes["timestamp"].value, "%Y%m%d %H%M%S")

                tempComment = Comment()
                tempComment.author = author
                tools.debug("commentUser ", author)
                tempComment.body = body
                tools.debug("message ", body)
                tempComment.blog = tempblog
                tempComment.timestamp = timestamp
                tools.debug("timestamp ", timestamp)
                tempComment.save()

    return HttpResponse(result)
Ejemplo n.º 3
0
    def setUp(self):
        # Every test needs access to the request factory.
        self.user = get_user_model().objects.create_user(username='******', email='*****@*****.**', password='******')
        self.factory = APIRequestFactory()

        blog1 = BlogPost(id=1, author=self.user, title="Test Blog Title 1", body="This is a test blog article.")
        blog1.save()

        blog2 = BlogPost(id=2, author=self.user, title="Test Blog Title 2", body="This is another test blog article.")
        blog2.save()
Ejemplo n.º 4
0
    def post(self):
        form = BlogPostForm(self.request.POST)

        if form.validate():
            post = BlogPost(**form.data)
            post.save()
            self.redirect_to("admin-blog-post-edit-extra", post_id = post.key().id(), extra="saved")

        return {
            "admin_section": "admin-blog-posts-new",
            "form": form,
        }
Ejemplo n.º 5
0
    def setUp(self):
        # Every test needs access to the request factory.
        self.user = get_user_model().objects.create_user(
            username='******',
            email='*****@*****.**',
            password='******')
        self.factory = APIRequestFactory()

        blog1 = BlogPost(id=1,
                         author=self.user,
                         title="Test Blog Title 1",
                         body="This is a test blog article.")
        blog1.save()

        blog2 = BlogPost(id=2,
                         author=self.user,
                         title="Test Blog Title 2",
                         body="This is another test blog article.")
        blog2.save()
Ejemplo n.º 6
0
def add_post(request):
    context = RequestContext(request)
    if request.method == 'POST':
        form = BlogPostForm(request.POST) 
        if form.is_valid():
            blog = BlogPost()
            blog.author = request.user
            blog.title = request.POST['title']
            blog.bodytext = request.POST['bodytext']
            blog.save()
            messages.add_message(request, SUCCESS, 'Post Added')
            
            return HttpResponseRedirect('/main/blog/')
        else:
            messages.add_message(request, SUCCESS, 'Enter a valid Post')
            return HttpResponseRedirect('/main/add/')
    else:
        form = BlogPostForm() 
    return render_to_response('blog/add.html', {'form': form}, context)
Ejemplo n.º 7
0
def loaddir(directory, clear=False):
    if clear:
        BlogPost.objects.all().delete()

    queue = os.listdir(directory)
    while queue:
        next = queue.pop()
        if next[0] == '.': continue
        if next in ('template.rst', 'template'): continue
        next = path.join(directory, next)
        if path.isdir(next):
            queue.extend([
                path.join(next,f) for f in os.listdir(next)
                ])
            continue

        filecontent = file(next).read()
        parts = filecontent.split('\n---\n', 1)
        fields, content = parts
        if len(parts) != 2:
            raise IOError('gitcms.blog.load: expected "---" separator in file %s' % next)
        fields = yaml.load(fields)
        fields['timestamp'] = parsedatetime(fields['timestamp'])
        fields['year_month_slug'] = time.strftime('%%Y/%%B/%s' % fields['slug'], fields['timestamp'])
        fields['timestamp'] = time.strftime('%Y-%m-%d %H:%M', fields['timestamp'])
        fields['content'] = preprocess_rst_content(content)
        categories = fields.get('categories', '')
        if 'categories' in fields: del fields['categories']
        ptags = []
        if categories:
            for c in categories.split():
                ptags.append(tag_for(c))
        # if we arrived here and no errors, then it is safe
        # to add our post.
        #
        P = BlogPost(**fields)
        P.save()
        for t in ptags:
            P.tags.add(t)
Ejemplo n.º 8
0
def loaddir(directory, clear=False):
    if clear:
        BlogPost.objects.all().delete()

    queue = listdir(directory)
    while queue:
        next = queue.pop()
        if next[0] == '.': continue
        if next in ('template.rst', 'template'): continue
        next = path.join(directory, next)
        if path.isdir(next):
            queue.extend([path.join(next, f) for f in listdir(next)])
            continue

        filecontent = file(next).read()
        parts = filecontent.split('\n---\n', 1)
        if len(parts) != 2:
            raise IOError(
                'gitcms.blog.load: expected "---" separator in file %s' % next)
        fields, content = parts
        fields = yaml.load(fields)
        fields['content'] = preprocess_rst_content(content)
        fields['timestamp'] = parsedatetime(fields['timestamp'])
        fields['timestamp'] = time.strftime('%Y-%m-%d %H:%M',
                                            fields['timestamp'])
        categories = fields.get('categories', '')
        if 'categories' in fields: del fields['categories']
        ptags = []
        if categories:
            for c in categories.split():
                ptags.append(tag_for(c))
        # if we arrived here and no errors, then it is safe
        # to add our post.
        #
        P = BlogPost(**fields)
        P.save()
        for t in ptags:
            P.tags.add(t)
Ejemplo n.º 9
0
def loaddir(directory, clear=False):
    if clear:
        BlogPost.objects.all().delete()

    queue = os.listdir(directory)
    while queue:
        next = queue.pop()
        if next[0] == '.':
            continue

        if next in ('template.rst', 'template'):
            continue

        next = os.path.join(directory, next)
        if os.path.isdir(next):
            queue.extend([os.path.join(next, f) for f in os.listdir(next)])
            continue

        filecontent = file(next).read()
        parts = filecontent.split('\n---\n', 1)
        fields, content = parts
        if len(parts) != 2:
            err_msg = 'gitcms.blog.load: expected "---" separator in file %s'
            raise IOError(err_msg % next)
        fields = yaml.load(fields)
        fields['content'] = preprocess_rst_content(content)
        categories = fields.get('categories', '')
        if 'categories' in fields:
            del fields['categories']
        ptags = []
        if categories:
            for c in categories.split():
                ptags.append(tag_for(c))
        P = BlogPost(**fields)
        P.save()
        for t in ptags:
            P.tags.add(t)
Ejemplo n.º 10
0
def loaddir(directory, clear=False):
    if clear:
        BlogPost.objects.all().delete()

    queue = os.listdir(directory)
    while queue:
        next = queue.pop()
        if next[0] == '.':
            continue

        if next in ('template.rst', 'template'):
            continue

        next = os.path.join(directory, next)
        if os.path.isdir(next):
            queue.extend([os.path.join(next, f) for f in os.listdir(next)])
            continue

        filecontent = file(next).read()
        parts = filecontent.split('\n---\n', 1)
        fields, content = parts
        if len(parts) != 2:
            err_msg = 'gitcms.blog.load: expected "---" separator in file %s'
            raise IOError(err_msg % next)
        fields = yaml.load(fields)
        fields['content'] = preprocess_rst_content(content)
        categories = fields.get('categories', '')
        if 'categories' in fields:
            del fields['categories']
        ptags = []
        if categories:
            for c in categories.split():
                ptags.append(tag_for(c))
        P = BlogPost(**fields)
        P.save()
        for t in ptags:
            P.tags.add(t)