Esempio n. 1
0
def adminSettings():
	logger = logging.getLogger(__name__)

	result = engineservice.getSettings()

	result["success"] = True
	result["message"] = ""

	if "btnSave" in request.all:
		try:
			if request.all["timezone"] not in pytz.common_timezones:
				raise ValueError("Please provide a valid timezone")

			if request.all["theme"] not in engineservice.getInstalledThemeNames():
				raise ValueError("Please provide a valid theme name")

			engineservice.saveSettings(
				timezone = request.all["timezone"],
				theme    = request.all["theme"]
			)

			awsservice.saveSettings(
				accessKeyId=request.all["awsAccessKeyId"],
				secretAccessKey=request.all["awsSecretAccessKey"],
				s3Bucket="" if "awsBucket" not in request.all else request.all["awsBucket"]
			)

			result["timezone"] = request.all["timezone"]
			result["themeName"] = request.all["theme"]
			result["awsAccessKeyId"] = request.all["awsAccessKeyId"]
			result["awsSecretAccessKey"] = request.all["awsSecretAccessKey"]
			result["awsBucket"] = "" if "awsBucket" not in request.all else request.all["awsBucket"]

			result["message"] = "Settings updated"

		except Exception as e:
			result["success"] = False
			result["message"] = e.message

	awsSettings = awsservice.getSettings()

	result["title"] = "Settings"
	result["timezones"] = dthelper.getTimezoneArray()
	result["themes"] = engineservice.getInstalledThemeNames()

	result["awsAccessKeyId"] = awsSettings["accessKeyId"]
	result["awsSecretAccessKey"] = awsSettings["secretAccessKey"]
	result["awsBucket"] = awsSettings["s3Bucket"]
	result["awsBuckets"] = []

	if len(result["awsAccessKeyId"]) and len(result["awsSecretAccessKey"]):
		awsConnection = s3service.connect(accessKeyId=result["awsAccessKeyId"], secretAccessKey=result["awsSecretAccessKey"])
		result["awsBuckets"] = s3service.getBucketList(connection=awsConnection)

	return result
Esempio n. 2
0
def adminUploadDeleteImageFile():
	logger = logging.getLogger(__name__)

	try:
		awsSettings = awsservice.getSettings()
		connection = s3service.connect(accessKeyId=awsSettings["accessKeyId"], secretAccessKey=awsSettings["secretAccessKey"])

		s3service.deleteFile(connection=connection, bucketName=awsSettings["s3Bucket"], keyName=request.all["key"])

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

	return "ok"
Esempio n. 3
0
def ajaxGetBucket():
	try:
		awsSettings = awsservice.getSettings()
		connection = s3service.connect(accessKeyId=awsSettings["accessKeyId"], secretAccessKey=awsSettings["secretAccessKey"])
		items = s3service.getBucketItems(connection=connection, bucketName=awsSettings["s3Bucket"])

		data = []

		for i in items:
			_, ext = os.path.splitext(i.name)
			if ext in [".png", ".jpg", ".jpeg", ".gif"]:
				fullUrl = i.generate_url(expires_in=300, force_http=True)
				sanitizedUrl = urlservice.removeQueryString(url=fullUrl)
				data.append({ "url": sanitizedUrl, "name": i.name })

		return {
			"success": True,
			"data": data
		}

	except Exception as e:
		return httpservice.error(response=response, message=e.message)
Esempio n. 4
0
def adminUploadImageFile():
	logger = logging.getLogger(__name__)

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

	if ext not in (".jpg", ".jpeg", ".png",):
		return "Invalid file type"

	#
	# Upload the file, then send it to Amazon S3
	#
	f.save(config.UPLOAD_PATH, True)

	try:
		fullUploadedFilePath = os.path.join(config.UPLOAD_PATH, f.filename)

		awsSettings = awsservice.getSettings()
		connection = s3service.connect(accessKeyId=awsSettings["accessKeyId"], secretAccessKey=awsSettings["secretAccessKey"])

		name = os.path.basename(fullUploadedFilePath)
		keyName = "/posts/%s" % name

		s3service.saveFile(connection=connection, bucketName=awsSettings["s3Bucket"], filePathAndName=fullUploadedFilePath, keyName=keyName)

	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"