Example #1
0
    def post(self, category):
        if not self.has_permission:
            return
        if self.request.files == {} or 'myfile' not in self.request.files:
            self.write({"status": "error", "message": "对不起,请选择文件"})
            return

        file_type_list = []
        if category == 'music':
            file_type_list = ['audio/mpeg', 'audio/x-wav', 'audio/mp3']
        if file_type_list == []:
            return
        send_file = self.request.files['myfile'][0]
        if send_file['content_type'] not in file_type_list:
            if category == 'music':
                self.write({
                    "status": "error",
                    "message": "对不起,仅支持 mp3, wav 格式的音乐文件"
                })
                return

        if category == 'music':
            if len(send_file['body']) > 20 * 1024 * 1024:
                self.write({"status": "error", "message": "对不起,请上传20M以下的音乐文件"})
                return

        user = self.current_user
        if category == 'music':
            upload_path = sys.path[0] + "/static/upload/music/" + get_year() + '/' +\
                get_month() + "/"
        if not os.path.exists(upload_path):
            try:
                os.system('mkdir -p %s' % upload_path)
            except:
                pass

        timestamp = str(int(time.time())) +\
            ('').join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba',
                6)) + '_' + str(user.id)
        image_format = send_file['filename'].split('.').pop().lower()
        file_path = upload_path + timestamp + '.' + image_format
        with open(file_path, 'wb') as f:
            f.write(send_file['body'])

        path = '/' +\
            '/'.join(file_path.split('/')[file_path.split('/').index("static"):])
        if category == 'music':
            if self.is_ajax:
                return self.write({
                    'path': path,
                    'status': "success",
                    'message': '上传成功',
                    'category': 'music',
                    'content_type': send_file['content_type']
                })
        return
Example #2
0
    def post(self, category):
        if not self.has_permission:
            return
        if self.request.files == {} or 'myfile' not in self.request.files:
            self.write({"status": "error",
                        "message": "对不起,请选择文件"})
            return

        file_type_list = []
        if category == 'music':
            file_type_list = ['audio/mpeg', 'audio/x-wav', 'audio/mp3']
        if file_type_list == []:
            return
        send_file = self.request.files['myfile'][0]
        if send_file['content_type'] not in file_type_list:
            if category == 'music':
                self.write({"status": "error",
                            "message": "对不起,仅支持 mp3, wav 格式的音乐文件"})
                return

        if category == 'music':
            if len(send_file['body']) > 20 * 1024 * 1024:
                self.write({"status": "error",
                            "message": "对不起,请上传20M以下的音乐文件"})
                return

        user = self.current_user
        if category == 'music':
            upload_path = sys.path[0] + "/static/upload/music/" + get_year() + '/' +\
                get_month() + "/"
        if not os.path.exists(upload_path):
            try:
                os.system('mkdir -p %s' % upload_path)
            except:
                pass

        timestamp = str(int(time.time())) +\
            ('').join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba',
                                    6)) + '_' + str(user.id)
        image_format = send_file['filename'].split('.').pop().lower()
        file_path = upload_path + timestamp + '.' + image_format
        with open(file_path, 'wb') as f:
            f.write(send_file['body'])

        path = '/' +\
            '/'.join(file_path.split('/')[file_path.split('/').index("static"):])
        if category == 'music':
            if self.is_ajax:
                return self.write({
                    'path': path,
                    'status': "success",
                    'message': '上传成功',
                    'category': 'music',
                    'content_type': send_file['content_type'],
                })
        return
Example #3
0
class ImgUploadHandler(BaseHandler):
    @db_session
    @tornado.web.authenticated
    def post(self):
        if not self.has_permission:
            return
        user = self.current_user
        if not user:
            return self.redirect_next_url()
        if self.request.files == {} or 'myimage' not in self.request.files:
            self.write({"status": "error", "message": "对不起,请选择图片"})
            return
        image_type_list = [
            'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp',
            'image/x-png'
        ]
        send_file = self.request.files['myimage'][0]
        if send_file['content_type'] not in image_type_list:
            self.write({
                "status":
                "error",
                "message":
                "对不起,仅支持 jpg, jpeg, bmp, gif, png\
                    格式的图片"
            })
            return
        if len(send_file['body']) > 6 * 1024 * 1024:
            self.write({"status": "error", "message": "对不起,请上传6M以下的图片"})
            return
        tmp_file = tempfile.NamedTemporaryFile(delete=True)
        tmp_file.write(send_file['body'])
        tmp_file.seek(0)
        try:
            image_one = Image.open(tmp_file.name)
        except IOError, error:
            logging.info(error)
            logging.info('+' * 30 + '\n')
            logging.info(self.request.headers)
            tmp_file.close()
            self.write({"status": "error", "message": "对不起,此文件不是图片"})
            return
        width = image_one.size[0]
        height = image_one.size[1]
        if width < 80 or height < 80 or width > 30000 or height > 30000:
            tmp_file.close()
            self.write({
                "status": "error",
                "message": "对不起,请上传长宽在80px~30000px之间的图片!"
            })
            return
        user = self.current_user
        upload_path = sys.path[0] + "/static/upload/" + get_year() + '/' +\
            get_month() + "/"
        if not os.path.exists(upload_path):
            try:
                os.system('mkdir -p %s' % upload_path)
            except:
                pass
        timestamp = str(int(time.time())) +\
            ('').join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba',
                6)) + '_' + str(user.id)
        image_format = send_file['filename'].split('.').pop().lower()
        tmp_name = upload_path + timestamp + '.' + image_format
        image_one.save(tmp_name)
        tmp_file.close()
        path = '/' +\
            '/'.join(tmp_name.split('/')[tmp_name.split('/').index("static"):])
        category = self.get_argument('category', None)
        print category
        del_path = None
        if category == 'head':
            del_path = user.head_img
            user.head_img = path
            result = {
                'path': path,
                'status': "success",
                'message': '头部背景设置成功',
                'category': 'head'
            }
        elif category == 'background':
            del_path = user.background_img
            user.background_img = path
            result = {
                'path': path,
                'status': "success",
                'message': '背景设置成功',
                'category': 'background'
            }
        else:
            result = {'path': path, 'status': "success", 'message': '图片上传成功'}
        if del_path:
            try:
                os.system('rm -f %s%s' % (sys.path[0], del_path))
            except:
                pass
        if self.is_ajax:
            return self.write(result)
        return
Example #4
0
#
# In order to see this, execute this: pprint(df.head(10).to_dict())
#
# TODO: Replace with df.to_dict()
#
df_dict = df.to_dict()

# Get the access to the date time dictionary values
df_datetimes_dict = df_dict.get('Datetime')

# Loop through the date times and create a list of datetime strings
date_times = []
for key, value in df_datetimes_dict.items():
    date_times.append(value)

# Group the date times by year into a dictionary of values.
#
# So if you want all dates for 2010 for example, then you can access them by:
#   datetimes_grouped_by_year.get('2010')
#
datetimes_grouped_by_year = {}
for single_date in date_times:
    # Get the year for the date.
    single_date_year = helpers.get_year(single_date)
    datetimes_grouped_by_year.setdefault(str(single_date_year),
                                         []).append(single_date)

dataset_object = ParseCalendarDays(datetimes_grouped_by_year)

pprint(dataset_object.get_tos_feature_scale())
Example #5
0
class ImgUploadHandler(BaseHandler):
    @orm.db_session
    @tornado.web.authenticated
    @require_admin
    def post(self, node_id):
        category = self.get_argument('category', None)
        node = Node.get(id=node_id)
        if not node:
            return self.redirect_next_url()
        if not self.request.files or 'myimage' not in self.request.files:
            self.write({"status": "error", "message": "对不起,请选择图片"})
            return
        image_type_list = [
            'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp',
            'image/x-png'
        ]
        icon_type_list = [
            'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp',
            'image/x-png', 'image/ico .ico', 'image/x-icon'
        ]
        send_file = self.request.files['myimage'][0]
        if category != 'icon' and send_file[
                'content_type'] not in image_type_list:
            self.write({
                "status":
                "error",
                "message":
                "对不起,仅支持 jpg, jpeg, bmp, gif, png\
                        格式的图片"
            })
            return
        if category == 'icon' and send_file[
                'content_type'] not in icon_type_list:
            self.write({
                "status":
                "error",
                "message":
                "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\
                        格式的图片"
            })
            return
        if len(send_file['body']) > 6 * 1024 * 1024:
            self.write({"status": "error", "message": "对不起,请上传6M以下的图片"})
            return
        tmp_file = tempfile.NamedTemporaryFile(delete=True)
        tmp_file.write(send_file['body'])
        tmp_file.seek(0)
        try:
            image_one = Image.open(tmp_file.name)
        except IOError, error:
            logging.info(error)
            logging.info('+' * 30 + '\n')
            logging.info(self.request.headers)
            tmp_file.close()
            self.write({"status": "error", "message": "对不起,此文件不是图片"})
            return
        width = image_one.size[0]
        height = image_one.size[1]
        if width < 20 or height < 20 or width > 30000 or height > 30000:
            tmp_file.close()
            self.write({
                "status": "error",
                "message": "对不起,请上传长宽在20px~30000px之间的图片!"
            })
            return
        user = self.current_user
        upload_path = sys.path[0] + "/static/upload/" + get_year() + '/' +\
            get_month() + "/"
        if not os.path.exists(upload_path):
            try:
                os.system('mkdir -p %s' % upload_path)
            except:
                pass
        timestamp = str(int(time.time())) + \
                    ''.join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba',
                                          6)) + '_' + str(user.id)
        image_format = send_file['filename'].split('.').pop().lower()
        tmp_name = upload_path + timestamp + '.' + image_format
        image_one.save(tmp_name)
        tmp_file.close()
        path = '/' +\
            '/'.join(tmp_name.split('/')[tmp_name.split('/').index("static"):])
        del_path = None
        if category == 'head':
            del_path = node.head_img
            node.head_img = path
            msg = '节点头部背景设置成功'
            data = {'path': path, 'category': 'head'}
        elif category == 'icon':
            image_two = image_one.resize((75, 75), Image.ANTIALIAS)

            tmp_name2 = upload_path + timestamp + 'x75.' + image_format
            image_two.save(tmp_name2)
            try:
                os.system('rm -f %s%s' % (sys.path[0], path))
            except:
                pass
            path = '/' +\
                '/'.join(tmp_name2.split('/')[tmp_name2.split('/').index("static"):])
            del_path = node.icon_img
            node.icon_img = path
            msg = '节点图标设置成功'
            data = {'path': path, 'category': 'icon'}
        elif category == 'background':
            del_path = node.background_img
            node.background_img = path
            msg = '节点背景设置成功'
            data = {'path': path, 'category': 'background'}
        else:
            msg = '图片上传成功'
            data = {'path': path}
        if del_path:
            try:
                os.system('rm -f %s%s' % (sys.path[0], del_path))
            except:
                pass
        return self.send_success_result(msg=msg, data=data)