예제 #1
0
파일: admin.py 프로젝트: Texo/texo-cms
def adminWritePost():
	success = True
	message = ""
	mode = ""

	if "btnDraft" in request.all:
		mode = "draft"

	if "btnPublish" in request.all:
		mode = "publish"

	if mode != "":
		post = postservice.createPost(
			title             = request.all["postTitle"],
			author            = request.session["user"],
			slug              = request.all["postSlug"],
			content           = request.all["postContent"],
			createdDateTime   = dthelper.utcNow(),
			tags              = request.all["postTags"],
			status            = postservice.getPostStatus(status=("Draft" if mode == "draft" else "Published")),
			publishedDateTime = dthelper.utcNow() if mode == "publish" else None,
			publishedYear     = dthelper.utcNow() if mode == "publish" else None,
			publishedMonth    = dthelper.utcNow() if mode == "publish" else None
		)

		message = "Your post has been %s." % ("saved as a draft" if mode == "draft" else "published")
		redirect("/admin/posts/" + message)

	return {
		"title": "Write Post",
		"success": success,
		"message": message,
		"postTitle": "" if not "postTitle" in request.all else request.all["postTitle"],
		"postSlug": "" if not "postSlug" in request.all else request.all["postSlug"],
		"postTags": "" if not "postTags" in request.all else request.all["postTags"],
		"postContent": "" if not "postContent" in request.all else request.all["postContent"],
	}
예제 #2
0
파일: admin.py 프로젝트: Texo/texo-cms
def adminUploadMarkdownFile():
	logger = logging.getLogger(__name__)

	f = request.files.get("upload")
	name, ext = os.path.splitext(f.filename)

	if ext not in (".md"):
		return "Invalid file type"

	#
	# Upload the file, then parse it
	#
	f.save(config.UPLOAD_PATH, True)

	try:
		fullUploadedFilePath = os.path.join(config.UPLOAD_PATH, f.filename)
		metadata, content = postservice.parseMarkdownFile(fullUploadedFilePath, config.TIMEZONE)

		#
		# Find the author based on name
		#
		firstName, lastName = metadata["author"].split(" ")
		possibleAuthors = userservice.getUsersByName(firstName=firstName, lastName=lastName)

		if len(possibleAuthors) <= 0:
			raise Exception("Could not find author")

		#
		# Find status by name
		#
		status = postservice.getPostStatus(status="Published")

		dt = metadata["date"]
		d, t = metadata["date"].split(" ")

		#
		# Do we already have a matching post? If so say no
		#
		matchingPost = postservice.getPostByDateAndSlug(
			year=dthelper.getYear(date=d),
			month=dthelper.getMonth(date=d),
			slug=metadata["slug"]
		)

		if matchingPost:
			raise Exception("Already have a post that matches this slug, year and month")

		postservice.createPost(
			title             = metadata["title"],
			author            = possibleAuthors[0],
			slug              = metadata["slug"],
			content           = content,
			createdDateTime   = metadata["date"],
			tags              = metadata["tags"],
			status            = status,
			publishedDateTime = metadata["date"],
			publishedMonth    = d,
			publishedYear     = d
		)


	except Exception as e:
		logger.error(e.message, exc_info=True)
		return e.message

	finally:
		try:
			os.remove(fullUploadedFilePath)

		except OSError as ose:
			logger.error(ose.message, exc_info=True)

	return "ok"