def AddArticle(request): """ Add a new blog. """ if not request.user.is_authenticated(): return render_to_response("user/login.html", RequestContext(request)) if request.method == 'POST': title = request.POST.get('title', '') body = request.POST.get('body', '') status = request.POST.get('status', '') abstract = request.POST.get('abstract', '') keywords = request.POST.get('keywords', '') en_keywords = request.POST.get('en_keywords', '') topped = request.POST.get('topped', '') price = request.POST.get('price', '') attachment = request.POST.get('attachment', '') category = request.POST.get('category', '') tag = request.POST.get('tag', '') myFile = request.FILES.get("attachment", None) if not myFile: return HttpResponse('Please upload file.') destination = open(os.path.join(settings.MEDIA_ROOT, myFile.name), 'wb+') for chunk in myFile.chunks(): destination.write(chunk) destination.close() attachedFile = Attachment() attachedFile.name = myFile.name attachedFile.attachment = myFile.name newFile = attachedFile.save() article = Article() article.title = title article.body = body article.status = status article.abstract = abstract article.keywords = keywords article.en_keywords = en_keywords article.topped = topped article.price = price article.attachment = Attachment.objects.filter(name=myFile.name)[0] article.category = Category.objects.filter(id=category)[0] article.tag = Tag.objects.filter(id=tag)[0] article.user = User.objects.filter(id=request.user.id)[0] article.save() article_id = Article.objects.filter(title=title, status=article.status, category=article.category, tag=article.tag)[0] url = "article/" + str(article_id.id) return HttpResponseRedirect(url)
def save(contents): for content in contents: b = Article() b.cateid = 1 b.catename = '绘画' b.title = content['title'] b.content = content['content'] b.url = content['url'] b.author = content['author'] b.abstract = content['abstract'] b.pubtime = datetime.utcnow() b.status = 0 b.save()
def parse_md_to_article(file_path): """ 解析 md文件 :param file_path: md文件路径 :return: Article 文章对象 """ with open(file_path) as f: print(file_path) content = f.read() head = re.findall(r"---[.\\n\s\S]*---", content) # print(re.findall(r"---[.\\n\s\S]*---", content)) title = re.findall( r"^title: .*$", content, re.M)[0].split(": ")[1] if not title: print(file_path, "-------") created_time = re.findall( r"^date: .*$", content, re.M)[0].split(": ")[1] tags = re.findall( r"^tags: .*$", content, re.M)[0].split(": ")[1] tags = list(filter(None, re.split(r"[\[ ,\]]", tags, re.M))) try: category_id = re.findall( r"^category: .*$", content, re.M)[0].split(": ")[1] except IndexError: category_id = re.findall( r"^categories: .*$", content, re.M)[0].split(": ")[1] body = content.split(head[0])[1].strip() abstract = body.split("<!--more-->")[0] last_modified_time = created_time status = 'p' views = 0 likes = 0 topped = 0 article = None try: article = Article.objects.get(title=title) except Exception: pass if not article: article = Article() article.title = title print("----------", title) if title is 'Android Debug Bridge': print("true") if created_time: p = parse_datetime(created_time.strip()).replace(tzinfo=utc) naive = p article.created_time = naive article.last_modified_time = naive else: print("----------error create time is null:", title) article.body = body article.abstract = abstract article.status = status article.views = views article.likes = likes article.topped = topped article.save() save_tags(tags=tags) for tag in tags: article.tags.add(Tag.objects.get(name=tag)) c = None save_category(category_id) try: c = Category.objects.get(name=category_id) except Exception: pass if not c: c = Category(name=category_id) article.category_id = c.id # print(article.title, article.created_time, article.tags, article.category_id) return article