def test_add(self): from models import Image Image.add(name="Hallo.img") img = Image.by_id(1) self.assertEqual(1, img.id) self.assertEqual("Hallo.img", img.name) self.assertEqual("hallo.img", img.route)
def post(self): bf = self.request.get("file") if not bf: return self.redirect("/a/upload/common/") name = unicode(self.request.body_file.vars['file'].filename, 'utf-8') mime = self.request.body_file.vars['file'].headers['content-type'] #handle file > 1M if mime.find('image') == -1: self.redirect("/a/upload/common/") album = Album.get_by_key_name(self.request.get("album")) Image.add(album, name, mime, bf) self.redirect(album.url)
def test_image_deleted(self): img_id = Image.add(content_type='image/jpeg', raw_data='010101') news = dict( rank = 1, title = 'title', url = 'http://localhost/%s' % random.random(), comhead = 'localhost', score = '100', author = 'poly', author_link = 'http://localhost/', submit_time = '10 hours ago', comment_cnt = '100', comment_url = 'http://localhost', summary = 'Hello world!', img_id = img_id ) existing_keys = [n.url for n in StartupNews.query.all()] pk = StartupNews.add(**news) # Ensure saved self.assertEqual(StartupNews.query.get(pk).url, pk) self.assertEqual(Image.query.get(img_id).id, img_id) StartupNews.remove_except(existing_keys) # Ensure removed self.assertIsNone(StartupNews.query.get(pk)) self.assertIsNone(Image.query.get(img_id))
def test_image_deleted(self): img_id = Image.add(content_type='image/jpeg', raw_data='010101') news = dict(rank=1, title='title', url='http://localhost/%s' % random.random(), comhead='localhost', score='100', author='poly', author_link='http://localhost/', submit_time='10 hours ago', comment_cnt='100', comment_url='http://localhost', summary='Hello world!', img_id=img_id) existing_keys = [n.url for n in StartupNews.query.all()] pk = StartupNews.add(**news) # Ensure saved self.assertEqual(StartupNews.query.get(pk).url, pk) self.assertEqual(Image.query.get(img_id).id, img_id) StartupNews.remove_except(existing_keys) # Ensure removed self.assertIsNone(StartupNews.query.get(pk)) self.assertIsNone(Image.query.get(img_id))
def add(self): request = self.request form = ImageView.UploadForm(request.POST) if request.method == 'POST' and form.validate(): # looks like we will us form here only for validation an html gen # name = form.image.name doesn't work this way there is always 'image' as name name = request.POST['image'].filename path = os.path.join(os.path.dirname(__file__),'files', name) in_file = request.POST['image'].file with open(path, "w") as fd: in_file.seek(0) while 1: data = in_file.read(2<<16) if not data: break fd.write(data) image = Image.add(name) return HTTPFound(location=request.route_url( 'image_view', id=image.id, ) ) return dict( form=form, url=self.request.route_url('image_add'), pages=Page.all(), logged_in=authenticated_userid(self.request), )
def post(self): album = self.request.get("album") name = urllib.unquote(self.request.get("qqfile")) bf = self.request.body mime = getImageInfo(bf)[0] album = Album.get_by_key_name(album) if mime.find('image') > -1 and album: img = Image.add(album, name, mime, bf) return self.response.out.write("{success:true}") return self.response.out.write("{success:false}")
def post(self): skey = self.request.get("skey") logging.info(skey) if skey != self.settings.skey: self.response.out.write("") try: album = self.request.get("album") bf = self.request.get("Filedata") name = unicode(self.request.body_file.vars["Filedata"].filename,'utf-8') mime = getImageInfo(bf)[0] album = Album.get_by_key_name(album) img =Image.add(album,name,mime,bf) self.response.out.write(img.copyurl) except: self.response.out.write("")
def put(self, slug, name): '''Put a image file ''' album = Album.get_by_key_name(slug) if album is None: return self.error(404) name = urllib.unquote(name).decode('utf-8') mime = get_mime(name) bf = self.request.body if len(bf) > 1000 * 1000: return self.error(411) try: img = Image.add(album, name, mime, bf) self.template_value['key'] = img.key().name() self.template_value['f'] = img.f self.template_value['s'] = img.s return self.render() except: return self.error(500)
def post(self): album = self.request.get("album") u = self.request.get("u") des = self.request.get("des") name = self.request.get("name") retries = 3 result = None try: result = urlfetch.fetch(u, deadline=10) except: retries -= 1 if retries > 0: result = urlfetch.fetch(u, deadline=10) if result and result.status_code == 200: album = Album.get_by_key_name(album) bf = result.content mime = getImageInfo(bf)[0] if mime.find("image") < 0: raise Exception(u"不是正確的圖片") img = Image.add(album, name, mime, bf, **{"description": des}) self.redirect(album.m_url) else: self.response.out.write(u"此地址抓取錯誤")