def add_pdf(self, post_data): ''' Adding the pdf file. ''' img_entity = self.request.files['file'][0] img_desc = post_data['desc'] filename = img_entity["filename"] # qian, hou = os.path.splitext(filename) if filename and allowed_file_pdf(filename): pass else: return False _, hou = os.path.splitext(filename) signature = str(uuid.uuid1()) outfilename = '{0}{1}'.format(signature, hou) outpath = 'static/upload/{0}'.format(signature[:2]) if os.path.exists(outpath): pass else: os.makedirs(outpath) with open(os.path.join(outpath, outfilename), "wb") as fout: fout.write(img_entity["body"]) sig_save = os.path.join(signature[:2], signature) path_save = os.path.join(signature[:2], outfilename) MEntity.create_entity(signature, path_save, img_desc, kind=2) self.redirect('/entity/{0}{1}'.format(sig_save, hou.lower()))
def add_pdf(self): img_entiry = self.request.files['file'][0] filename = img_entiry["filename"] qian, hou = os.path.splitext(filename) if filename and allowed_file_pdf(filename): pass else: return False (qian, hou) = os.path.splitext(filename) signature = str(uuid.uuid1()) outfilename = '{0}{1}'.format(signature, hou) outpath = 'static/upload/{0}'.format(signature[:2]) if os.path.exists(outpath): pass else: os.makedirs(outpath) with open(os.path.join(outpath, outfilename), "wb") as fout: fout.write(img_entiry["body"]) sig_save = os.path.join(signature[:2], signature) MEntity.create_entity(signature, sig_save) self.redirect('/entity/{0}{1}'.format(sig_save, hou.lower()))
def test_create_entity(self): uid = self.uid post_data = { 'path': self.path, } MEntity.create_entity(uid, post_data['path']) assert True
def _add_download_entity(self, ext_dic): download_url = ext_dic['tag_file_download'].strip().lower() if ( 'tag_file_download' in ext_dic) else '' the_entity = MEntity.get_id_by_impath(download_url) if the_entity: return True if download_url: MEntity.create_entity(path=download_url, desc=download_url, kind=4)
def add_pic(self, post_data): ''' Adding the picture. ''' img_entity = self.request.files['file'][0] filename = img_entity["filename"] if filename and allowed_file(filename): pass else: return False _, hou = os.path.splitext(filename) signature = str(uuid.uuid1()) outfilename = '{0}{1}'.format(signature, hou) outpath = 'static/upload/{0}'.format(signature[:2]) if os.path.exists(outpath): pass else: os.makedirs(outpath) with open(os.path.join(outpath, outfilename), "wb") as fileout: fileout.write(img_entity["body"]) path_save = os.path.join(signature[:2], outfilename) sig_save = os.path.join(signature[:2], signature) imgpath = os.path.join(outpath, signature + '_m.jpg') imgpath_sm = os.path.join(outpath, signature + '_sm.jpg') ptr_image = Image.open(os.path.join('static/upload', path_save)) tmpl_size = (768, 768) thub_size = (256, 256) (imgwidth, imgheight) = ptr_image.size if imgwidth < tmpl_size[0] and imgheight < tmpl_size[1]: tmpl_size = (imgwidth, imgheight) ptr_image.thumbnail(tmpl_size) im0 = ptr_image.convert('RGB') im0.save(imgpath, 'JPEG') im0.thumbnail(thub_size) im0.save(imgpath_sm, 'JPEG') MEntity.create_entity(signature, path_save, post_data['desc'] if 'desc' in post_data else '', kind=1) self.redirect('/entity/{0}_m.jpg'.format(sig_save))
def test_create_entity(self): uid = self.uid path = self.path desc = 'create entity' kind = '1' tt = MEntity.create_entity(uid, path, desc, kind) assert tt == True
def down(self, down_uid): ''' Download the entity by UID. ''' post_data = {} for key in self.request.arguments: post_data[key] = self.get_arguments(key)[0] down_url = MPost.get_by_uid(down_uid).extinfo.get( 'tag__file_download', '') if down_url: if allowed_file(down_url): kind = '1' elif allowed_file_pdf(down_url): kind = '2' else: kind = '3' if str(down_url)[:17] == '/static/datasets/': str_down_url = str(down_url)[8:] else: str_down_url = str(down_url)[15:] if kind == '3': str_down_url = down_url ment_id = MEntity.get_id_by_impath(str_down_url) if ment_id: MEntity2User.create_entity2user(ment_id, self.userinfo.uid, post_data['userip']) return True else: MEntity.create_entity(uid='', path=str_down_url, desc='', kind=kind) ment_id = MEntity.get_id_by_impath(str_down_url) if ment_id: MEntity2User.create_entity2user(ment_id, self.userinfo.uid, post_data['userip']) return True else: return False
def add_url(self, post_data): ''' Adding the URL as entity. ''' img_desc = post_data['desc'] img_path = post_data['file1'] cur_uid = tools.get_uudd(4) while MEntity.get_by_uid(cur_uid): cur_uid = tools.get_uudd(4) MEntity.create_entity(cur_uid, img_path, img_desc, kind=3) kwd = { 'kind': '3', } self.render('misc/entity/entity_view.html', filename=img_path, cfg=config.CMS_CFG, kwd=kwd, userinfo=self.userinfo)
def down(self, down_uid): ''' Download the entity by UID. ''' post_data = {} for key in self.request.arguments: post_data[key] = self.get_arguments(key)[0] down_url = MPost.get_by_uid(down_uid).extinfo.get( 'tag__file_download', '') if down_url: if allowed_file(down_url): kind = '1' elif allowed_file_pdf(down_url): kind = '2' else: kind = '3' ment_id = MEntity.get_id_by_impath(down_url) userip = self.get_host_ip() if ment_id: MEntity2User.create_entity2user(ment_id, self.userinfo.uid, userip) else: MEntity.create_entity(uid='', path=down_url, desc='', kind=kind) ment_id = MEntity.get_id_by_impath(down_url) if ment_id: MEntity2User.create_entity2user(ment_id, self.userinfo.uid, userip) output = {'down_code': 1, 'down_url': down_url} else: output = {'down_code': 0} return json.dump(output, self)
def add_pic(self): img_entiry = self.request.files['file'][0] filename = img_entiry["filename"] if filename and allowed_file(filename): pass else: return False (qian, hou) = os.path.splitext(filename) signature = str(uuid.uuid1()) outfilename = '{0}{1}'.format(signature, hou) outpath = 'static/upload/{0}'.format(signature[:2]) if os.path.exists(outpath): pass else: os.makedirs(outpath) with open(os.path.join(outpath, outfilename), "wb") as f: f.write(img_entiry["body"]) path_save = os.path.join(signature[:2], outfilename) sig_save = os.path.join(signature[:2], signature) imgpath = os.path.join(outpath, signature + '_m.jpg') imgpath_sm = os.path.join(outpath, signature + '_sm.jpg') im = Image.open(os.path.join('static/upload', path_save)) tmpl_size = (768, 768) thub_size = (256, 256) (imgwidth, imgheight) = im.size if imgwidth < tmpl_size[0] and imgheight < tmpl_size[1]: tmpl_size = (imgwidth, imgheight) im.thumbnail(tmpl_size) im0 = im.convert('RGB') im0.save(imgpath, 'JPEG') im0.thumbnail(thub_size) im0.save(imgpath_sm, 'JPEG') MEntity.create_entity(signature, sig_save) self.redirect('/entity/{0}_m.jpg'.format(sig_save))
def add_pdf(self, post_data): ''' Adding the pdf file. ''' img_entity = self.request.files['file'][0] img_desc = post_data['desc'] filename = img_entity["filename"] if filename and allowed_file_pdf(filename): pass else: kwd = { 'pager': '', 'err_info': '* The formats of uploadable files are: pdf, doc, docx, zip, rar, ppt, 7z, xlsx' } self.render('misc/entity/entity_add.html', cfg=config.CMS_CFG, kwd=kwd, userinfo=self.userinfo) # return False _, hou = os.path.splitext(filename) signature = str(uuid.uuid1()) outfilename = '{0}{1}'.format(signature, hou) outpath = 'static/upload/{0}'.format(signature[:2]) if os.path.exists(outpath): pass else: os.makedirs(outpath) with open(os.path.join(outpath, outfilename), "wb") as fout: fout.write(img_entity["body"]) sig_save = os.path.join(signature[:2], signature) path_save = os.path.join(signature[:2], outfilename) create_pdf = MEntity.create_entity( signature, path_save, img_desc, kind=post_data['kind'] if 'kind' in post_data else '2') if self.entity_ajax is False: self.redirect('/entity/{0}{1}'.format(sig_save, hou.lower())) else: if create_pdf: output = {'path_save': path_save} else: output = {'path_save': ''} return json.dump(output, self)
def add_pdf(self, post_data): ''' Adding the pdf file. ''' img_entity = self.request.files['file'][0] img_desc = post_data['desc'] filename = img_entity["filename"] if filename and allowed_file_pdf(filename): pass else: return False _, hou = os.path.splitext(filename) signature = str(uuid.uuid1()) outfilename = '{0}{1}'.format(signature, hou) outpath = 'static/upload/{0}'.format(signature[:2]) if os.path.exists(outpath): pass else: os.makedirs(outpath) with open(os.path.join(outpath, outfilename), "wb") as fout: fout.write(img_entity["body"]) sig_save = os.path.join(signature[:2], signature) path_save = os.path.join(signature[:2], outfilename) create_pdf = MEntity.create_entity( signature, path_save, img_desc, kind=post_data['kind'] if 'kind' in post_data else '2') if self.entity_ajax == False: self.redirect('/entity/{0}{1}'.format(sig_save, hou.lower())) else: if create_pdf: output = {'path_save': path_save} else: output = {'path_save': ''} return json.dump(output, self)
class TestMEntity2User(): def setup(self): print('setup 方法执行于本类中每条用例之前') self.M2U = MEntity2User() self.username = '******' self.uu = MUser() self.user_uid = '' self.e_uid = 'q112' self.path = '/static/123123' self.userip = '10.6.0.177' self.ee = MEntity() self.uid = '' def add_user(self, **kwargs): name = kwargs.get('user_name', self.username) post_data = { 'user_name': name, 'user_pass': kwargs.get('user_pass', 'g131322'), 'user_email': kwargs.get('user_email', '{}@kljhqq.com'.format(random.randint(1, 1000000))), } self.uu.create_user(post_data) aa = self.uu.get_by_name(name) self.user_uid = aa.uid def add_entity(self): desc = 'create entity' self.ee.create_entity(self.e_uid, self.path, desc) def add_E2U(self): self.add_user() self.add_entity() self.M2U.create_entity2user(self.e_uid, self.user_uid, self.userip) tt = self.M2U.query_all() for i in tt: if i.entity_id == self.e_uid: self.uid = i.uid def test_get_by_uid(self): self.add_E2U() tt = self.M2U.get_by_uid(self.uid) assert tt.user_ip == self.userip assert tt.entity_id == self.e_uid self.tearDown() def test_delete_by_uid(self): self.add_E2U() tt = self.M2U.get_by_uid(self.uid) assert tt.user_ip == self.userip self.M2U.delete_by_uid(self.e_uid) tt = self.M2U.query_all() tf = True for i in tt: if i.entity_id == self.e_uid: tf = False self.tearDown() assert tf def test_query_all(self): tt = self.M2U.query_all() tf = True for i in tt: if i.entity_id == self.e_uid: tf = False assert tf self.add_E2U() tt = self.M2U.query_all() tf = True for i in tt: if i.entity_id == self.e_uid: tf = True self.tearDown() assert tf def test_get_all_pager(self): self.add_E2U() aa = self.M2U.query_all(limit=200) a = int(aa.count() / 10) + 2 tf = False for i in range(a): tt = self.M2U.get_all_pager(current_page_num=i) for t in tt: if t.uid == self.uid: tf = True assert t.user_id == self.user_uid assert t.user_ip == self.userip self.tearDown() assert tf def test_get_all_pager_by_username(self): self.add_E2U() tf = False aa = self.M2U.get_all_pager_by_username(self.user_uid) for t in aa: if t.uid == self.uid: tf = True assert t.user_ip == self.userip self.tearDown() assert tf def test_create_entity2user(self): self.add_E2U() tt = self.M2U.get_by_uid(self.uid) assert tt.user_ip == self.userip self.tearDown() def test_total_number(self): a = self.M2U.total_number() self.add_E2U() b = self.M2U.total_number() self.tearDown() assert a + 1 <= b def test_total_number_by_user(self): self.add_user() aa = self.M2U.total_number_by_user(self.user_uid) self.add_E2U() bb = self.M2U.total_number_by_user(self.user_uid) self.tearDown() assert aa + 1 <= bb def tearDown(self): print("function teardown") self.uu.delete_by_user_name(self.username) self.ee.delete(self.e_uid) self.M2U.delete_by_uid(self.e_uid)
def add_pic(self, post_data): ''' Adding the picture. ''' img_entity = self.request.files['file'][0] filename = img_entity["filename"] if filename and allowed_file(filename): pass else: kwd = { 'pager': '', 'err_info': '* The formats of uploadable files are: png, jpg, jpeg, gif, tif, bmp', } self.render('misc/entity/entity_add.html', cfg=config.CMS_CFG, kwd=kwd, userinfo=self.userinfo) # return False _, hou = os.path.splitext(filename) signature = str(uuid.uuid1()) outfilename = '{0}{1}'.format(signature, hou) outpath = 'static/upload/{0}'.format(signature[:2]) if os.path.exists(outpath): pass else: os.makedirs(outpath) with open(os.path.join(outpath, outfilename), "wb") as fileout: fileout.write(img_entity["body"]) path_save = os.path.join(signature[:2], outfilename) sig_save = os.path.join(signature[:2], signature) imgpath = os.path.join(outpath, signature + '_m.jpg') imgpath_sm = os.path.join(outpath, signature + '_sm.jpg') ptr_image = Image.open(os.path.join('static/upload', path_save)) tmpl_size = (768, 768) thub_size = (256, 256) (imgwidth, imgheight) = ptr_image.size if imgwidth < tmpl_size[0] and imgheight < tmpl_size[1]: tmpl_size = (imgwidth, imgheight) ptr_image.thumbnail(tmpl_size) im0 = ptr_image.convert('RGB') im0.save(imgpath, 'JPEG') im0.thumbnail(thub_size) im0.save(imgpath_sm, 'JPEG') create_pic = MEntity.create_entity( signature, path_save, post_data['desc'] if 'desc' in post_data else '', kind=post_data['kind'] if 'kind' in post_data else '1') if self.entity_ajax is False: self.redirect('/entity/{0}_m.jpg'.format(sig_save)) else: if create_pic: output = {'path_save': imgpath} else: output = {'path_save': ''} return json.dump(output, self)
def add_message(self): desc = 'create entity' kind = 'f' MEntity.create_entity(self.uid, self.path, desc, kind)