コード例 #1
0
def write_to_api_params(items=None, user=None, content_type=None,
                        imported=False, user_request=None):
    data = APIData()
    data.additional_data = {}
    if user:
        tags = []
        try:
            for arg in items:
                if arg.startswith('unindexed_'):
                    ad_key = arg.replace("unindexed_", "")
                    ad_value = user_request.request.get(arg)
                    data.additional_data[ad_key] = ad_value.strip()
                if arg.startswith('indexed_'):
                    ad_key = arg.replace("indexed_", "")
                    ad_value = user_request.request.get(arg)
                    data.additional_data[ad_key] = ad_value
                    try:
                        tags += create_tags(ad_value)
                    except Exception as e:
                        logging.exception("Cannot create tag from: ")
                    data.indexed_data.append(create_indexed_tag(arg,
                                             user_request.request.get(arg)))

                if arg.startswith('file_'):
                    logging.debug(arg)
                    filename = BUCKET_NAME
                    filename += random_string(128) + "/"
                    ad_key = arg.replace("file_", "")
                    data.additional_data[ad_key] = {}
                    try:
                        if not user_request:
                            file_name = items[arg].filename
                            filename += file_name
                            gcs_options = {'x-goog-acl': 'public-read'}
                            gcs_file = gcs.open(filename, 'w',
                                                options=gcs_options)
                            gcs_file.write(items[arg].file.read())
                            gcs_file.close()
                        else:
                            file_name = user_request.request.POST.get(arg)
                            file_name = file_name.filename
                            filename += file_name
                            gcs_options = {'x-goog-acl': 'public-read'}
                            gcs_file = gcs.open(filename, 'w',
                                                options=gcs_options)
                            gcs_file.write(user_request.request.get(arg))
                            gcs_file.close()
                        full_url = "https://storage.googleapis.com" + filename
                        data.file_url = full_url
                        data.additional_data[ad_key]["file_url"] = full_url
                        try:
                            blob_key = blobstore.create_gs_key("/gs" + filename)
                            data.serving_url = images.get_serving_url(blob_key)
                            data.additional_data[ad_key]["serving_url"] = data.serving_url
                            data.gcs_key = blobstore.BlobKey(blob_key)
                        except Exception as e:
                            logging.exception(e)
                            logging.error("FILE IS NOT AN IMAGE")
                            data.additional_data[ad_key]["serving_url"] = full_url
                    except AttributeError, e:
                        logging.exception(e)
                        logging.exception("NO FILE ATTACHED")
            if user:
                data.username = user.name
                data.user = user.key
                data.indexed_data.append(create_indexed_tag("USER_ID",
                                         str(user.key.id())))
            data.tags = uniquify(tags)
            data.put()
            return data
        except Exception as e:
            logging.exception('ERROR')
            logging.debug(e)
コード例 #2
0
def write_to_api(items=None, user=None, content_type=None, imported=False,
                 user_request=None):
    logging.debug(items)
    logging.debug(user)
    logging.debug(content_type)
    logging.debug(imported)
    logging.debug(user_request)
    if imported:
        data = APIData(id=str(items['indexed_code']))
    else:
        data = APIData()

    if user == '*****@*****.**':
        user = User.query(User.current_email == '*****@*****.**').get()

    data.additional_data = {}
    if content_type == "application/json":
        tags = []
        logging.debug(items)
        try:
            for key, value in items.items():
                if key.startswith('unindexed_'):
                    ad_key = key.replace("unindexed_", "")
                    data.additional_data[ad_key] = value.strip()

                if key.startswith('indexed_'):
                    ad_key = key.replace('indexed_', '').replace('_array', '')
                    if key.endswith('_array'):
                        value_arr = json.loads(value)
                        for v_arr in value_arr:
                            data.indexed_data.append(create_indexed_tag(key,
                                                     v_arr))
                    else:
                        data.indexed_data.append(create_indexed_tag(key,
                                                 value))
                    data.additional_data[ad_key] = value
                    try:
                        tags += create_tags(value)
                    except Exception as e:
                        logging.exception("Cannot create tag from: ")
            if user:
                data.username = user.name
                data.user = user.key
                data.indexed_data.append(create_indexed_tag("USER_ID",
                                         str(user.key.id())))

            data.indexed_data = uniquify(data.indexed_data)
            data.tags = uniquify(tags)
            data.put()
            return data
        except Exception as e:
            logging.exception(e)
    else:
        tags = []
        try:
            for arg in items:
                if arg.startswith('unindexed_'):  # unindexed_
                    ad_key = arg.replace("unindexed_", "")
                    ad_value = items.get(arg)
                    data.additional_data[ad_key] = ad_value.strip()
                if arg.startswith('indexed_'):
                    ad_key = arg.replace("indexed_", "")
                    ad_value = items.get(arg)
                    data.additional_data[ad_key] = ad_value
                    try:
                        tags += create_tags(ad_value)
                    except Exception as e:
                        logging.exception("Cannot create tag from: ")
                    data.indexed_data.append(create_indexed_tag(arg,
                                             items.get(arg)))
                if arg.startswith('file_'):
                    logging.debug(arg)
                    filename = BUCKET_NAME
                    filename += random_string(128) + "/"
                    ad_key = arg.replace("file_", "")
                    data.additional_data[ad_key] = {}
                    try:
                        if not user_request:
                            if 'file_image' in items:
                                file_field = 'file_image'
                            if 'file_file' in items:
                                file_field = 'file_file'
                            if 'file_kml' in items:
                                file_field = 'file_kml'
                            file_name = items[file_field].filename
                            filename += file_name
                            gcs_options = {'x-goog-acl': 'public-read'}
                            gcs_file = gcs.open(filename, 'w',
                                                options=gcs_options)
                            gcs_file.write(items[file_field].file.read())
                            gcs_file.close()
                        else:
                            file_name = user_request.request.POST.get('file')
                            file_name = file_name.filename
                            filename += file_name
                            gcs_options = {'x-goog-acl': 'public-read'}
                            gcs_file = gcs.open(filename, 'w',
                                                options=gcs_options)
                            gcs_file.write(user_request.request.get('file'))
                            gcs_file.close()
                        full_url = "https://storage.googleapis.com" + filename
                        data.file_url = full_url
                        data.additional_data[ad_key]["file_url"] = full_url
                        try:
                            blob_key = blobstore.create_gs_key("/gs" + filename)
                            data.serving_url = images.get_serving_url(blob_key)
                            data.additional_data[ad_key]["serving_url"] = data.serving_url
                            data.gcs_key = blobstore.BlobKey(blob_key)
                        except Exception as e:
                            logging.exception(e)
                            logging.error("FILE IS NOT AN IMAGE")
                            data.additional_data[ad_key]["serving_url"] = full_url
                    except AttributeError, e:
                        logging.exception(e)
                        logging.exception("NO FILE ATTACHED")
            if user:
                data.username = user.name
                data.user = user.key
                data.indexed_data.append(create_indexed_tag("USER_ID",
                                         str(user.key.id())))
            data.tags = uniquify(tags)
            data.put()
            return data
        except Exception as e:
            logging.exception('ERROR')
            logging.debug(e)