def test_photo_faves(self): p1 = Photos( # creating photo image_url= "https://images.unsplash.com/photo-1500375592092-40eb2168fd21?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=668&q=80" ) p2 = Photos( # creating photo image_url= "https://images.unsplash.com/photo-1444944232907-0b9e9ace348c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=80" ) # creating a test user instance u = User.signup("testing12", "password") uid = 888 # creating an id u.id = uid # assigning it to our test user db.session.add_all([p1, p2, u]) # adding to sesh db.session.commit() # committing u.favorites.append(p1) # adding p1 to user faves db.session.commit() # committing # find faves where their user id = the test user id f = Favorites.query.filter(Favorites.user_id == uid).all() self.assertEqual(len(f), 1) # there should be one
def upload_data(self, data): print('Uploading data into {0}.'.format(self.args.database)) for row in self.db.batch_commit(data, 100): id = Information.create(**row) photos = [] for photo in row['photos']: photos.append((id, photo)) Photos.insert_many(photos, fields=[Photos.master_id, Photos.link]).execute()
def uploadimg(x_s=0, y_s=0): if request.method == 'POST': userid = session.get('userid') x_s = request.form['x_s'] y_s = request.form['y_s'] rannew = request.form['rannew'] hero = request.form['hero'] photo = request.files['heroupload'] alt = '' if photo: img_x_y = Upload.img_x_y(photo) href = Upload.upload_image(photo, '_o_', 0, 800) src = Upload.upload_image(photo, '_s_', x_s, y_s) Photos.add_a_photo(href, src, alt, img_x_y * hero, userid, rannew) return json.dumps({'error': 0, 'url': src}) else: flash('请上传正确的照片') return json.dumps({'error': 1, 'info': '请上传正确的照片'}) return render_template("uploadimg.html")
def add_photo(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file'], request.POST["title"]) return HttpResponseRedirect('/admin/') else: form = UploadFileForm() return render_to_response('upload.html', {'form': form}, context_instance=RequestContext(request)) new_photo = Photos() return render_to_response("add.html")
def add_photo(): if request.method == "POST": p = request.files.get("photo") if p.filename != '': p.save(p.filename) x = open(p.filename, 'rb').read() y = open('static/images/' + p.filename, 'wb') y.write(x) os.remove(p.filename) ph = Photos(session["user"], p.filename) db.session.add(ph) db.session.commit() return redirect("/photos")
def test_photo_model(self): """Does basic model work?""" p = Photos( # creating photo image_url= "https://images.unsplash.com/photo-1500375592092-40eb2168fd21?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=668&q=80" ) self.u.favorites.append(p) db.session.add(p) db.session.commit() # adding to sesh # check that there is one photo present in favorites self.assertEqual(len(self.u.favorites), 1)
def setup_faves(self): # setting up faves for faves tests # creating 3 photos, last is the only one with id p1 = Photos( image_url= "https://images.unsplash.com/photo-1464802686167-b939a6910659?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2533&q=80", ) p2 = Photos( image_url= "https://images.unsplash.com/photo-1601132359864-c974e79890ac?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1502&q=80", ) p3 = Photos( id=9876, image_url= "https://images.unsplash.com/photo-1484662020986-75935d2ebc66?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80", ) db.session.add_all([p1, p2, p3]) db.session.commit() # adding and committing to session f1 = Favorites(user_id=self.testuser_id, photo_id=9876) # setting up faves db.session.add(f1) db.session.commit() # committing faves to session
def uploadfile(request): if not request.user.is_authenticated(): return redirect('/') if request.method == "POST": uf = UserForm(request.POST, request.FILES) if uf.is_valid(): print uf.cleaned_data photo_name= uf.cleaned_data['photo_name'] albumid= uf.cleaned_data['albumid'] headImg= uf.cleaned_data['headImg'] photo = Photos() photo.photo_name = photo_name al = Album.objects.get(pk=albumid) photo.albumname = al photo.headimg = headImg photo.save() return redirect('/uploadfile/') else: uf = UserForm() maincat=MainCat.objects.all() nart,cart,rart,cm,tg=getthree() #日历 today=today=datetime.datetime.now() s=calendar.HTMLCalendar(6) cals=list(s.itermonthdays2(today.year,today.month)) tdarts=Article.objects.values('id','createtime').filter(createtime__year=today.year,createtime__month=today.month).order_by('createtime') #列表字典[{'createtime': datetime.datetime(2014, 4, 6, 4, 36, 32, 896000, tzinfo=<UTC>)}, tdart=set([i['createtime'].day for i in tdarts]) tmpq=Article.objects.exclude(createtime__year=today.year,createtime__month=today.month) premon=tmpq.filter(createtime__lt=today).order_by('-createtime')[:1] aftmon=tmpq.filter(createtime__gt=today).order_by('createtime')[:1] tt=[] for i in cals: tt.append(list(i)) ttt=[] for a in tt: for i in tdart: if a[0] == i: a.append(1) if len(a)==2: a.append(0) ttt.append(a) return render_to_response('uploadfile.html',locals(),context_instance=RequestContext(request))
def create_post(): if request.method == 'POST': try: title = request.form.get('title') body = request.form.get('body') author = current_user.login p = Post(title=title, body=body, author=author) db.session.add(p) db.session.commit() except Exception: raise (Exception) photos = request.form.get('files') photos = json.loads(photos) for photo in photos: if allowed_file(photo['name']): img = Photos(link='static/images/' + slugify(str(p.created), '') + photo['name'], post_id=p.id) db.session.add(img) db.session.commit() image_data = re.sub('^data:image/.+;base64,', '', photo['dataURL']) image = Image.open( io.BytesIO(base64.decodebytes(image_data.encode()))) image.save( UPLOAD_FOLDER + '/' + slugify(str(p.created), '') + photo['name'], 'JPEG') last_post = Post.query.order_by(Post.id.desc()).first() last_alert = Alert.query.order_by(Alert.id.desc()).first() return render_template('create_post.html', last_post=last_post, last_alert=last_alert, weekDays=rusWeekDays, months=rusMonths)
def show_curiosity_photos(): """route to display Curiosity info and photos""" mission_info_resp = requests.get( f'https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/latest_photos?api_key={APIKEY}' ) favorites = Favorites.query.all() data = mission_info_resp.json() if g.user: favorites = [photo.id for photo in g.user.favorites] photo_data = data["latest_photos"] for datum in photo_data: photo = datum["img_src"] new_photo = Photos(image_url=photo) db.session.add(new_photo) db.session.commit() photos_from_db = Photos.query.limit(24).all() return render_template("curiousity_photos.html", photos=photos_from_db, favorites=favorites)
def create_alert(): if request.method == 'POST': body = request.form.get('body') file = request.files['file'] alert = Alert(body=body, author=current_user.login) db.session.add(alert) db.session.commit() photo = Photos(link=(UPLOAD_FOLDER + '/' + slugify(str(alert.created), '') + file.filename), alert_id=alert.id) db.session.add(photo) db.session.commit() file.save( os.path.join(UPLOAD_FOLDER, slugify(str(alert.created), '') + file.filename)) last_post = Post.query.order_by(Post.id.desc()).first() last_alert = Alert.query.order_by(Alert.id.desc()).first() return render_template('create_alert.html', last_post=last_post, last_alert=last_alert, weekDays=rusWeekDays, months=rusMonths)
def test_add_fave(self): # creating photo instance and adding to session p = Photos( id=1984, image_url= "https://images.unsplash.com/photo-1464802686167-b939a6910659?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2533&q=80" ) # creating a favorite instance db.session.add(p) db.session.commit() with self.client as c: with c.session_transaction() as sess: # checking that user must be signed in sess[CURR_USER_KEY] = self.testuser_id # setting route to fave instance resp = c.post("/photos/1984/favorite", follow_redirects=True) # checking that page shows up self.assertEqual(resp.status_code, 200) # getting faves with photo_id 1984 faves = Favorites.query.filter(Favorites.photo_id == 1984).all() # checking that the number of faves = 1 self.assertEqual(len(faves), 1) # checking that the faves with the user_id we have matches our test fave self.assertEqual(faves[0].user_id, self.testuser_id)
def handle_uploaded_file(img, caption): new_photo = Photos() new_photo.photo.save("test_name", img) new_photo.caption = caption new_photo.save()
def handle_uploaded_file(img, caption): new_photo = Photos() new_photo.photo.save("test_name",img) new_photo.caption = caption new_photo.save()