# Coverage for modules/resource/view.py : 77%

## 22 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import json

2 import os

3

4 from flask import Blueprint

5 from flask import current_app

6 from flask import send_from_directory

7

8# from flask import render_template

9# from flask import redirect

10

11# from flask import url_for

12

13# from flask_login import login_required

14# from PIL import Image as PILimage

15

16# from shopyo.api.enhance import get_setting

17# from shopyo.api.file import delete_file

18

19# from modules.box__ecommerce.product.models import Product

20# from modules.resource.models import Image

21

22# from modules.resource.models import Resource

23

24# from flask import flash

25# from flask import request#

26# from shopyo.api.html import notify_success

27# from shopyo.api.forms import flash_errors

28

29

30 dirpath = os.path.dirname(os.path.abspath(__file__))

31 module_info = {}

32

33 with open(dirpath + "/info.json") as f:

34 module_info = json.load(f)

35

36 globals()["{}_blueprint".format(module_info["module_name"])] = Blueprint(

37 "{}".format(module_info["module_name"]),

38 __name__,

39 template_folder="templates",

40 url_prefix=module_info["url_prefix"],

41)

42

43

44 module_blueprint = globals()["{}_blueprint".format(module_info["module_name"])]

45

46

47@module_blueprint.route("/")

48 def index():

49 return module_info["display_string"]

50

51

52@module_blueprint.route("/theme/front/<active_theme>/styles.css", methods=["GET"])

53 def active_front_theme_css(active_theme):

54 theme_dir = os.path.join(

55 current_app.config["BASE_DIR"],

56 "static",

57 "themes",

58 "front",

59 active_theme,

60 )

61 # return theme_dir

62 return send_from_directory(theme_dir, "styles.css")

63

64

65@module_blueprint.route("/theme/back/<active_theme>/styles.css", methods=["GET"])

66 def active_back_theme_css(active_theme):

67 theme_dir = os.path.join(

68 current_app.config["BASE_DIR"],

69 "static",

70 "themes",

71 "back",

72 active_theme,

73 )

74 # return theme_dir

75 return send_from_directory(theme_dir, "styles.css")

76

77

78# # Handles javascript image uploads from tinyMCE

79# @module_blueprint.route("/upload/tinymce/image", methods=["POST"])

80# @login_required

81# def upload_tinymce_image():

82# # Kevin Foong

83# file = request.files.get("file")

84# if file:

85# filename = file.filename.lower()

86# fn, ext = filename.split(".")

87# # truncate filename (excluding extension) to 30 characters

88# fn = fn[:30]

89# filename = fn + "." + ext

90# if ext in ["jpg", "gif", "png", "jpeg"]:

91# try:

92# # everything looks good, save file

93# img_fullpath = os.path.join(

94# current_app.config["UPLOADED_PATH_IMAGE"], filename

95# )

96# file.save(img_fullpath)

97# # get the file size to save to db

98# file_size = os.stat(img_fullpath).st_size

99# size = 160, 160

100# # read image into pillow

101# im = PILimage.open(img_fullpath)

102# # get image dimension to save to db

103# file_width, file_height = im.size

104# # convert to thumbnail

105# im.thumbnail(size)

106# thumbnail = fn + "-thumb.jpg"

107# tmb_fullpath = os.path.join(

108# current_app.config["UPLOADED_PATH_THUMB"], thumbnail

109# )

110# # PNG is index while JPG needs RGB

111# if not im.mode == "RGB":

112# im = im.convert("RGB")

113# # save thumbnail

114# im.save(tmb_fullpath, "JPEG")

115

116# # save to db

117# img = Image(

118# filename=filename,

119# thumbnail=thumbnail,

120# file_size=file_size,

121# file_width=file_width,

122# file_height=file_height,

123# )

124# db.session.add(img)

125# db.session.commit()

126# except IOError:

127# output = make_response(404)

128# output.headers["Error"] = (

129# "Cannot create thumbnail for " + filename

130# )

131# return output

132# return jsonify({"location": filename})

133

134# # fail, image did not upload

135# output = make_response(404)

136# output.headers["Error"] = "Filename needs to be JPG, JPEG, GIF or PNG"

137# return output