def send_mail(snap_id, group_id): ''' Sends an email to users specified in the group, with all the images for a supplied snap ''' 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 = search_generic(document_type='picture', args_dict={'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) file_contents = get_file_contents(pic_path) msg.attach(pic_name, "image/jpeg", file_contents) pics_have_been_attached = True # 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 clean_up_files(snap_id, group_id): """ Deletes all picture files for a given snap if those pictures sources are designated in the group to be deleted after processing """ 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 = search_generic(document_type="picture", args_dict={"snap_id": str(snap_id)}) clean_up_files = True if len(pictures): snap_document = get_generic(snap_id, "snap") if snap_document["clean_up_files"] == False: # TODO make sure this comparison works clean_up_files = False if clean_up_files: 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_generic(pictures[pic_id], "picture") os.rmdir(os.path.join(current_app.config["PICTURE_SAVE_DIRECTORY"], str(snap_id))) snap_document["files_have_been_cleaned_up"] = True update_generic(snap_document, "snap")
def clean_up_files(snap_id, group_id): ''' Deletes all picture files for a given snap if those pictures sources are designated in the group to be deleted after processing ''' 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 = search_generic(document_type='picture', args_dict={'snap_id': str(snap_id)}) clean_up_files = True if len(pictures): snap_document = get_generic(snap_id, 'snap') if snap_document['clean_up_files'] == False: # TODO make sure this comparison works clean_up_files = False if clean_up_files: 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_generic(pictures[pic_id], 'picture') os.rmdir(os.path.join(current_app.config['PICTURE_SAVE_DIRECTORY'], str(snap_id))) snap_document['files_have_been_cleaned_up'] = True update_generic(snap_document, 'snap')
def generic_list_view(document_type='', args_dict={}): try: # raise exception if no document type is not supplied documents = search_generic(document_type=document_type, args_dict=args_dict) return Response(json.dumps(documents), status=200, mimetype='application/json') except Exception as e: # TODO add tests, bad paging info or strings that kill the map string could cause abends return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def send_mail(snap_id, group_id): """ Sends an email to users specified in the group, with all the images for a supplied snap """ 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 = search_generic(document_type="picture", args_dict={"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) file_contents = get_file_contents(pic_path) msg.attach(pic_name, "image/jpeg", file_contents) pics_have_been_attached = True # 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_search_generic_gets_request_args(self, ts_get_documents_from_criteria): with current_app.test_request_context('/whatever?the_parameter=66'): ret_val = ts.search_generic(document_type='cereal', args_dict={}) assert ts_get_documents_from_criteria.called_once_with({ 'the_parameter': '66', 'type': 'cereal' })
def build_distortion_pair_strings(distortion_set_id): distortion_pairs = search_generic(document_type='distortion_pair', args_dict={'distortion_set_id': distortion_set_id}) return_array = [] for distortion_pair in distortion_pairs: build_string = '{0},{1},{2},{3}'.format(distortion_pair['start_x'], distortion_pair['start_y'], distortion_pair['end_x'], distortion_pair['end_y']) return_array.append(build_string) return return_array
def build_distortion_pair_strings(distortion_set_id): distortion_pairs = search_generic( document_type='distortion_pair', args_dict={'distortion_set_id': distortion_set_id}) return_array = [] for distortion_pair in distortion_pairs: build_string = '{0},{1},{2},{3}'.format(distortion_pair['start_x'], distortion_pair['start_y'], distortion_pair['end_x'], distortion_pair['end_y']) return_array.append(build_string) return return_array
def test_search_generic_calls_expected_functions(self, ts_get_documents_from_criteria, ts_gather_and_enforce_request_args): ts_gather_and_enforce_request_args.return_value = {'coco': 'puffs'} ts_get_documents_from_criteria.return_value = {'franken': 'berry'} ret_val = ts.search_generic(document_type='cereal', args_dict={'special': 'k'}) assert ts_gather_and_enforce_request_args.called_once_with(['ANY_SEARCHABLE']) assert ts_get_documents_from_criteria.called_once_with({'special': 'k', 'coco': 'puffs', 'type': 'cereal'}) assert ret_val == {'franken': 'berry'}
def test_search_generic_calls_expected_functions( self, ts_get_documents_from_criteria, ts_gather_and_enforce_request_args): ts_gather_and_enforce_request_args.return_value = {'coco': 'puffs'} ts_get_documents_from_criteria.return_value = {'franken': 'berry'} ret_val = ts.search_generic(document_type='cereal', args_dict={'special': 'k'}) assert ts_gather_and_enforce_request_args.called_once_with( ['ANY_SEARCHABLE']) assert ts_get_documents_from_criteria.called_once_with({ 'special': 'k', 'coco': 'puffs', 'type': 'cereal' }) assert ret_val == {'franken': 'berry'}
def upload_files_to_s3(snap_id, group_id): """ Uploads pictures for a given snap to s3 Takes a snap id and group id, if the group is configured to use a gallery it looks for all pictures whose values for source matches those are specified in the group to use for the gallery """ group_document = get_group_document(group_id) if group_document["use_gallery"]: image_sources_for_gallery = group_document["image_sources_for_gallery"].split(",") pictures = search_generic(document_type="picture", args_dict={"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_generic(pictures[pic_id], "picture")
def upload_files_to_s3(snap_id, group_id): ''' Uploads pictures for a given snap to s3 Takes a snap id and group id, if the group is configured to use a gallery it looks for all pictures whose values for source matches those are specified in the group to use for the gallery ''' group_document = get_group_document(group_id) if group_document['use_gallery']: image_sources_for_gallery = group_document['image_sources_for_gallery'].split(',') pictures = search_generic(document_type='picture', args_dict={'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_generic(pictures[pic_id], 'picture')
def test_search_generic_gets_request_args(self, ts_get_documents_from_criteria): with current_app.test_request_context('/whatever?the_parameter=66'): ret_val = ts.search_generic(document_type='cereal', args_dict={}) assert ts_get_documents_from_criteria.called_once_with({'the_parameter': '66', 'type': 'cereal'})