def test_find_pictures_only_fetches_pictures(self): snap_id_1 = uuid.uuid4() id_1 = uuid.uuid4() id_2 = uuid.uuid4() doc_1 = { '_id': str(id_1), 'snap_id': str(snap_id_1), 'type': 'picture' } ps.save_picture_document(doc_1) doc_2 = { '_id': str(id_2), 'snap_id': str(snap_id_1), 'type': 'not_picture' } current_app.db[str(id_2)] = doc_2 pictures_dict = ps.find_pictures({'snap_id': str(snap_id_1)}) expected_dict = { str(id_1): { '_id': str(id_1), '_rev': ANY, 'snap_id': str(snap_id_1), 'type': 'picture' } } assert pictures_dict == expected_dict
def send_mail(snap_id, group_id): group_document = get_group_document(group_id) if ('email_recipients' in group_document and 'send_email_contents' in group_document and group_document['email_recipients'] and group_document['send_email_contents']): pics_have_been_attached = False subject = "pictures from snap {0}".format(snap_id) recipients = group_document['email_recipients'].split(',') sender_addr = os.environ.get('MAIL_USERNAME') msg = Message(subject, sender=sender_addr, recipients=recipients) msg.body = "this is the image for snap id {0}\n\n".format(snap_id) pictures = find_pictures({'snap_id': str(snap_id)}) picture_types = group_document['send_email_contents'].split(',') for pic_id in pictures.keys(): if pictures[pic_id]['source'] in picture_types: pic_name = build_picture_name(pic_id) pic_path = build_picture_path(picture_name=pic_name, snap_id=snap_id) with current_app.open_resource(pic_path) as fp: msg.attach(pic_name, "image/jpeg", fp.read()) pics_have_been_attached = True if pics_have_been_attached: mail.send(msg)
def test_find_pictures_works_with_snap_id(self): snap_id_1 = uuid.uuid4() snap_id_2 = uuid.uuid4() pic_id_1 = uuid.uuid4() pic_id_2 = uuid.uuid4() pic_id_3 = uuid.uuid4() pic_doc_1 = { '_id': str(pic_id_1), 'snap_id': str(snap_id_1), 'type': 'picture' } ps.save_picture_document(pic_doc_1) pic_doc_2 = { '_id': str(pic_id_2), 'snap_id': str(snap_id_2), 'type': 'picture' } ps.save_picture_document(pic_doc_2) pic_doc_3 = { '_id': str(pic_id_3), 'snap_id': str(snap_id_1), 'type': 'picture' } ps.save_picture_document(pic_doc_3) pictures_dict = ps.find_pictures({'snap_id': str(snap_id_1)}) expected_dict = {str(pic_id_1): {'_id': str(pic_id_1), '_rev': ANY, 'snap_id': str(snap_id_1), 'type': 'picture'}, str(pic_id_3): {'_id': str(pic_id_3), '_rev': ANY, 'snap_id': str(snap_id_1), 'type': 'picture'} } assert pictures_dict == expected_dict
def list_pictures(): search_dict = {} for key in request.args.keys(): search_dict[key] = request.args[key] pictures = find_pictures(search_dict) return Response(json.dumps(pictures), status=200, mimetype='application/json')
def get_group_gallery(group_id): try: group_dict = get_group_document(group_id) group_id = group_dict['_id'] args_dict = {'group_id': group_id} (page, items_per_page) = get_paging_info_from_request(request) pictures_dict = find_pictures(args_dict, gallery_url_not_null=True, page=page, items_per_page=items_per_page) except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json') return Response(json.dumps(pictures_dict), status=200, mimetype='application/json')
def clean_up_files(snap_id, group_id): group_document = get_group_document(group_id) if 'image_sources_to_delete' in group_document: image_sources_to_delete = group_document['image_sources_to_delete'].split(',') pictures = find_pictures({'snap_id': str(snap_id)}) for pic_id in pictures.keys(): if pictures[pic_id]['source'] in image_sources_to_delete: os.remove(pictures[pic_id]['uri']) pictures[pic_id]['uri'] = '' pictures[pic_id]['deleted'] = True else: shutil.move(pictures[pic_id]['uri'], current_app.config['PICTURE_SAVE_DIRECTORY']) pictures[pic_id]['uri'] = os.path.join(current_app.config['PICTURE_SAVE_DIRECTORY'], pictures[pic_id]['filename']) update_picture_document(pictures[pic_id]) os.rmdir(os.path.join(current_app.config['PICTURE_SAVE_DIRECTORY'], str(snap_id)))
def get_group_pictures(group_id): try: group_dict = get_group_document(group_id) group_id = group_dict['_id'] args_dict = {'group_id': group_id} (page, items_per_page) = get_paging_info_from_request(request) pictures_dict = find_pictures(args_dict, page=page, items_per_page=items_per_page) except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json') return Response(json.dumps(pictures_dict), status=200, mimetype='application/json')
def upload_files_to_s3(snap_id, group_id): group_document = get_group_document(group_id) if group_document['use_gallery']: image_sources_for_gallery = group_document['image_sources_for_gallery'].split(',') pictures = find_pictures({'snap_id': str(snap_id)}) #TODO the following assumes s3 and internet are working fine, make it more robust, with py.test tests too conn = boto.connect_s3(current_app.config['S3_ACCESS_KEY_ID'], current_app.config['S3_SECRET_ACCESS_KEY']) bucket = conn.get_bucket(current_app.config['S3_BUCKET_NAME']) for pic_id in pictures.keys(): if pictures[pic_id]['source'] in image_sources_for_gallery: destination = boto.s3.key.Key(bucket) destination.key = pictures[pic_id]['filename'] destination.set_contents_from_filename(pictures[pic_id]['uri']) destination.make_public() pic_gallery_url = destination.generate_url(expires_in=0, query_auth=False) pictures[pic_id]['gallery_url'] = pic_gallery_url update_picture_document(pictures[pic_id])
def upload_files_to_s3(snap_id, group_id): group_document = get_group_document(group_id) if group_document['use_gallery']: image_sources_for_gallery = group_document[ 'image_sources_for_gallery'].split(',') pictures = find_pictures({'snap_id': str(snap_id)}) #TODO the following assumes s3 and internet are working fine, make it more robust, with py.test tests too conn = boto.connect_s3(current_app.config['S3_ACCESS_KEY_ID'], current_app.config['S3_SECRET_ACCESS_KEY']) bucket = conn.get_bucket(current_app.config['S3_BUCKET_NAME']) for pic_id in pictures.keys(): if pictures[pic_id]['source'] in image_sources_for_gallery: destination = boto.s3.key.Key(bucket) destination.key = pictures[pic_id]['filename'] destination.set_contents_from_filename(pictures[pic_id]['uri']) destination.make_public() pic_gallery_url = destination.generate_url(expires_in=0, query_auth=False) pictures[pic_id]['gallery_url'] = pic_gallery_url update_picture_document(pictures[pic_id])
def clean_up_files(snap_id, group_id): group_document = get_group_document(group_id) if 'image_sources_to_delete' in group_document: image_sources_to_delete = group_document[ 'image_sources_to_delete'].split(',') pictures = find_pictures({'snap_id': str(snap_id)}) for pic_id in pictures.keys(): if pictures[pic_id]['source'] in image_sources_to_delete: os.remove(pictures[pic_id]['uri']) pictures[pic_id]['uri'] = '' pictures[pic_id]['deleted'] = True else: shutil.move(pictures[pic_id]['uri'], current_app.config['PICTURE_SAVE_DIRECTORY']) pictures[pic_id]['uri'] = os.path.join( current_app.config['PICTURE_SAVE_DIRECTORY'], pictures[pic_id]['filename']) update_picture_document(pictures[pic_id]) os.rmdir( os.path.join(current_app.config['PICTURE_SAVE_DIRECTORY'], str(snap_id)))
def test_find_pictures_works_with_snap_id(self): snap_id_1 = uuid.uuid4() snap_id_2 = uuid.uuid4() pic_id_1 = uuid.uuid4() pic_id_2 = uuid.uuid4() pic_id_3 = uuid.uuid4() pic_doc_1 = { '_id': str(pic_id_1), 'snap_id': str(snap_id_1), 'type': 'picture' } ps.save_picture_document(pic_doc_1) pic_doc_2 = { '_id': str(pic_id_2), 'snap_id': str(snap_id_2), 'type': 'picture' } ps.save_picture_document(pic_doc_2) pic_doc_3 = { '_id': str(pic_id_3), 'snap_id': str(snap_id_1), 'type': 'picture' } ps.save_picture_document(pic_doc_3) pictures_dict = ps.find_pictures({'snap_id': str(snap_id_1)}) expected_dict = { str(pic_id_1): { '_id': str(pic_id_1), '_rev': ANY, 'snap_id': str(snap_id_1), 'type': 'picture' }, str(pic_id_3): { '_id': str(pic_id_3), '_rev': ANY, 'snap_id': str(snap_id_1), 'type': 'picture' } } assert pictures_dict == expected_dict
def test_find_pictures_only_fetches_pictures(self): snap_id_1 = uuid.uuid4() id_1 = uuid.uuid4() id_2 = uuid.uuid4() doc_1 = { '_id': str(id_1), 'snap_id': str(snap_id_1), 'type': 'picture' } ps.save_picture_document(doc_1) doc_2 = { '_id': str(id_2), 'snap_id': str(snap_id_1), 'type': 'not_picture' } current_app.db[str(id_2)] = doc_2 pictures_dict = ps.find_pictures({'snap_id': str(snap_id_1)}) expected_dict = {str(id_1): {'_id': str(id_1), '_rev': ANY, 'snap_id': str(snap_id_1), 'type': 'picture'} } assert pictures_dict == expected_dict