def test_posts_delete(self): tmp_post = models.Post(title='test title', body='test body', thumbnail_index=0) tmp_pic1 = models.Picture(source='www.appnexus.com', post=tmp_post) tmp_pic2 = models.Picture(source='www.google.com', post=tmp_post) tmp_pic3 = models.Picture(source='www.facebook.com', post=tmp_post) db.session.add(tmp_post) db.session.add(tmp_pic1) db.session.add(tmp_pic2) db.session.add(tmp_pic3) db.session.commit() self.app.delete('/api/post/delete?id=1') actual = models.Post.query.all() expected = [] assert actual == expected
def test_picture_serialize(self): tmp = models.Picture( source= 'https://pbs.twimg.com/profile_images/458794430200152064/XdQULww6.png' ) actual = tmp.serialize() expected = { 'source': 'https://pbs.twimg.com/profile_images/458794430200152064/XdQULww6.png' } assert expected['source'] == actual['source']
def test_posts_api_get(self): tmp_post = models.Post(title='test title', body='test body', thumbnail_index=0) tmp_pic1 = models.Picture(source='www.appnexus.com', post=tmp_post) tmp_pic2 = models.Picture(source='www.google.com', post=tmp_post) tmp_pic3 = models.Picture(source='www.facebook.com', post=tmp_post) db.session.add(tmp_post) db.session.add(tmp_pic1) db.session.add(tmp_pic2) db.session.add(tmp_pic3) db.session.commit() expected_status = 200 expected_data = { 'results': [{ 'body': 'test body', 'id': 1, 'pictures': [{ 'source': 'www.appnexus.com' }, { 'source': 'www.google.com' }, { 'source': 'www.facebook.com' }], 'thumbnailIndex': 0, 'title': 'test title' }] } response = self.app.get('/api/post/get') actual_status = response.status_code actual_data = json.loads(response.data) self.assertEqual(actual_status, expected_status) self.assertEqual(actual_data, expected_data)
def cam(): if (request.method == "POST"): f = request.form['file'] name = convert_and_save(f) userid = current_user.get_id() cate = request.form["cate"] p = models.Picture(user_id=userid, tag=cate, image_path=name, verified=None) db.session.add(p) db.session.commit() return render_template('cam.html', title='Cam')
def api_post_post(): data = request.form tmp_post = models.Post(title=data['title'], body=data['body'], thumbnail_index=data['thumbnailIndex']) db.session.add(tmp_post) pictures = data.getlist('pictures[]') for picture in pictures: tmp_pic = models.Picture(source=picture, post=tmp_post) db.session.add(tmp_pic) db.session.commit() return 'HTTP 1.1 200 OK\r\n'
def make_mock_picture(user=None, tags=None, despriction=None, address=None): _picture = models.Picture(userId=str(user.id), despriction=despriction or 'testdes', address=address or 'testaddress') _picture.tags = [models.Tag(tag=tags or 'testtags')] db.session.add(_picture) db.session.commit() # _tags = models.Tags( # picId=_picture.id, # tag='testtag' # ) # db.session.add(_tags) # db.session.commit() return _picture
def add_picture(): """ route for adding picture """ from .. import utils if request.cookies.get('masterkey') == utils.password_hashing(app.config['MASTER_PASSWORD']): class AddPictureForm(FlaskForm): name=StringField('Item name:') picture=HiddenField('Picture') submit = SubmitField('Add') add_picture_form=AddPictureForm() if request.method == 'POST': new_item=models.Picture(name=add_picture_form.name.data,picture=add_picture_form.picture.data) db.session.add(new_item) db.session.commit() return redirect('/admin_modify_picture') return render_template(app.config['TEMPLATE_NAME'] + '/admin-add-picture.html', config=app.config, active="add-picture",form=add_picture_form) else: return redirect('/admin')
async def upload_picture(request: Request, file: UploadFile = File(...), db: Session = Depends(get_db)): """ Upload a user picture. IF picture is valid, the face shape is detected, a new record is added to the pictures and history table, and the picture file is saved :param request: :param file: Selected binary picture file to be uploaded :param db: db session instance :return: Json response that contains the picture information, the detected face shape and the history record """ user_data = get_user_data_from_token(request) if user_data: save_path = PICTURE_UPLOAD_FOLDER file_name = picture_service.save_picture(file, save_path) face_detected = picture_service.detect_face(file_name, save_path) if face_detected is True: # try to find face landmark points face_landmarks = picture_service.detect_face_landmarks( file_name, save_path) if face_landmarks is None: raise HTTPException(status_code=422, detail="No face landmarks detected") else: picture_info = picture_service.get_picture_info( save_path, file_name) # detect face_shape face_shape = picture_service.detect_face_shape( face_landmarks, file_name, save_path) if face_shape is None: raise HTTPException( status_code=422, detail="Face shape could not be detected") new_picture = models.Picture(file_name=picture_info.file_name, file_path=picture_info.file_path, file_size=picture_info.file_size, height=picture_info.height, width=picture_info.width) orig_pic = picture_actions.add_picture(db=db, picture=new_picture) # Testing how to create a history record after detecting a face shape # create History instance # parse face shape string to int face_shape_id = face_shape_service.parse_face_shape( face_shape[0]) face_shape_detected: models.FaceShape = face_shape_actions.get_face_shape( db=db, face_shape_id=face_shape_id) user_id = user_data['id'] new_history: schemas.HistoryCreate = schemas.HistoryCreate( picture_id=orig_pic.id, original_picture_id=orig_pic.id, face_shape_id=face_shape_id, user_id=user_id) new_history_entry: models.History = history_actions.add_history( db=db, history=new_history) results = picture_actions.read_picture_by_file_name( db=db, file_name=new_picture.file_name, limit=1) return { 'picture': results[0], 'face_shape': face_shape[0], 'history_entry': new_history_entry } else: raise HTTPException(status_code=422, detail="No face detected") else: raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials")
async def change_hair_colour(picture_id: int, colour: str, r: int, b: int, g: int, db: Session = Depends(get_db)): """Applies changes to hair colour based on a base colour name (e.g. hot pink) and RGB values, which vary by lightness :param picture_id: ID of the picture to be processed :param db: db session instance :param colour: Base hair colour name :param r: Red channel of the colour :param g: Green channel of the colour :param b: Blue channel of the colour :returns: New picture object and history entry reflecting changes """ selected_picture: Union[models.Picture, None] = None if picture_id: # get latest entry from history where hair_colour_id == None first_history_entry = history_actions.get_first_picture_history_by_id( db=db, picture_id=picture_id) if first_history_entry: pic_history = history_actions.get_picture_history_by_original_picture_id( db=db, original_picture_id=first_history_entry.original_picture_id) if len(pic_history): latest_entry_no_colour = list( filter(lambda h: not h.hair_colour_id, pic_history))[-1] selected_picture = picture_actions.read_picture_by_id( db=db, picture_id=latest_entry_no_colour.picture_id) else: raise HTTPException(status_code=400, detail='Picture ID not found') if selected_picture: # apply hair colour try: picture_info = picture_service.change_hair_colour_RGB( file_name=selected_picture.file_name, r=r, b=b, g=g, file_path=selected_picture.file_path) # create new picture and add to db new_picture = models.Picture(file_name=picture_info.file_name, file_path=picture_info.file_path, file_size=picture_info.file_size, height=picture_info.height, width=picture_info.width) mod_pic = picture_actions.add_picture(db=db, picture=new_picture) # selected_picture = picture_actions.read_picture_by_id(db, picture_id=picture_id) # print(selected_picture.file_name) # print(selected_picture.file_path) # apply selected colour # picture_info = picture_service.change_hair_colour_RGB(file_name=selected_picture.file_name, selected_colour=colour, # r=r, g=g, b=b, # file_path=selected_picture.file_path) # print(picture_info) # create new picture and add to db # new_picture = models.Picture(file_name=picture_info.file_name, file_path=picture_info.file_path, # file_size=picture_info.file_size, height=picture_info.height, width=picture_info.width) # mod_pic = picture_actions.add_picture(db=db, picture=new_picture) # fake user_id user: models.User = history_actions.get_user_id_from_picture_id( db=db, picture_id=picture_id) if user: hair_colour_results: List[ models.HairColour] = hair_colour_actions.get_hair_colours( db=db, search=colour, limit=1) if len(hair_colour_results): hair_colour = hair_colour_results[0] # get latest history entry to extract face_shape_id and hair_style_id user_history: List[ models.History] = history_actions.get_user_history( db=db, user_id=user.id) latest_history_entry: models.History = user_history[-1] new_history: schemas.HistoryCreate = schemas.HistoryCreate( picture_id=mod_pic.id, original_picture_id=latest_history_entry. original_picture_id, previous_picture_id=selected_picture.id, hair_colour_id=hair_colour.id, face_shape_id=latest_history_entry.face_shape_id, hair_style_id=latest_history_entry.hair_style_id, user_id=user.id) history_entry: models.History = history_actions.add_history( db=db, history=new_history) hair_colour_entry: models.HairColour = hair_colour_actions.get_hair_colour_by_id( db=db, hair_colour_id=hair_colour.id) # get new picture with modified hair colour new_pic: models.Picture = picture_actions.read_picture_by_id( db=db, picture_id=history_entry.picture_id) return { 'history_entry': history_entry, 'picture': new_pic, 'hair_colour': hair_colour_entry } raise HTTPException( status_code=404, detail= 'No hair colour record associated with this colour name was found' ) except Exception as ex: print(ex) raise HTTPException( status_code=422, detail= 'Could not change hair colour. Please try a different picture. Exception: ' + ex) raise HTTPException(status_code=404, detail='Selected picture not found') raise HTTPException( status_code=404, detail='No user associated with this picture ID was found')