0

This is my flask script which takes in submissions from a HTML form and sends it to Contentful:

from flask import Flask, request, render_template
import os
from .extensions import register_extensions, assets
from contentful_management import Client
import uuid
import random

atoken = os.environ.get("ACCESS_TOKEN")
space = os.environ.get("SPACE_ID")

def create_app():
    app = Flask(__name__)   
    assets._named_bundles = {}
    register_extensions(app)

    @app.route("/", methods =["GET", "POST"])
    def index():
        if request.method == "POST":
           #  Get User Details
            authorname = request.form["name"]
            email = request.form["email"] 
            major = request.form["major"]
            tools_list = request.form["tools"]
            tools_list = tools_list.split(",")
            year_study = request.form["year"]
            project_title = request.form["projectname"]
            project_description = request.form["projectdescription"]
            no_files = request.form["filenumber"]
            client = Client(atoken)

            # Entry ID Generation
            uid = uuid.uuid4().hex[:20] 
   
               # File Counter
            x = 0 

            # Array for File IDs
            files_array = []
            while x < int(no_files):
                  fieldid = "file" + str(x)
                  file_name = "filename" + str(x)
                  fname = request.form[file_name]
                  asset_id = uuid.uuid4().hex[:20] 
                  files_array.append(asset_id)
                  uploadmedia = request.files[fieldid]
                  # Upload File
                  new_upload = client.uploads(space).create(uploadmedia.stream)  
                  # Upload to Asset             
                  client.assets(space, 'master').create(asset_id,
                     {
                     'fields': {
                         'title': {
                           'en-US': fname
                           },
                        'file': {
                           'en-US': {
                           'fileName': file_name,
                           'contentType': uploadmedia.content_type,
                           'uploadFrom': new_upload.to_link().to_json()
                           }
                        }
                     }
                     }
                  )
                  
                  # Process and Publish Asset
                  asset = client.assets(space, 'master').find(asset_id)
                  asset.process()
                  asset.publish() 
                  x = x + 1
            
            # Generate ID List
            ids = []
            for asset_id in files_array:
               y = asset_id
               x = {"sys": {"type": "Link", "linkType": "Asset", "id": y}}
               ids.append(x)
          
            # Create an Entry:
            entry_id = uid  
            entry = client.entries(space, 'master').create(entry_id, {
              'content_type_id': 'portfolio',
               "fields": {
                "name": {
                   "en-US":  project_title,
                },
                "author": {
                   "en-US":  authorname,
                },
                "contact": {
                   "en-US":  email,
                },
                "major": {
                   "en-US":  major,
                },
                "tools": {
                   "en-US":  tools_list,
                },
                "year": {
                   "en-US":  year_study,
                },
                "slug": {
                   "en-US":  project_title,
                },
                "description": {
                   "en-US":  project_description,
                },
                 "files":{
                  "en-US" : ids
                 } 
                } })
                  # Update the Entry:
            entry.title = project_title
            entry.save()
        return render_template("index.html") # print(uploadmedia.filename)
    
    return app

This works perfectly locally, and I am able to submit and create entries all in one go. However, when I deployed this app to Heroku, I am getting the following error:

2021-06-16T08:58:55.000000+00:00 app[api]: Build succeeded
2021-06-16T08:58:56.489660+00:00 heroku[router]: at=info method=POST path="/" host=submission-form.herokuapp.com request_id=cf4e8b77-02a6-4dfc-8780-19339d9138f5 fwd="202.12.82.198" dyno=web.1 connect=1ms service=1155ms status=500 bytes=244 protocol=https
2021-06-16T08:58:56.485604+00:00 app[web.1]: [2021-06-16 08:58:56 +0000] [8] [ERROR] Error handling request /
2021-06-16T08:58:56.485645+00:00 app[web.1]: Traceback (most recent call last):
2021-06-16T08:58:56.485646+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in handle
2021-06-16T08:58:56.485647+00:00 app[web.1]: self.handle_request(listener, req, client, addr)
2021-06-16T08:58:56.485648+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
2021-06-16T08:58:56.485648+00:00 app[web.1]: respiter = self.wsgi(environ, resp.start_response)
2021-06-16T08:58:56.485648+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask/app.py", line 2088, in __call__
2021-06-16T08:58:56.485649+00:00 app[web.1]: return self.wsgi_app(environ, start_response)
2021-06-16T08:58:56.485649+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
2021-06-16T08:58:56.485649+00:00 app[web.1]: response = self.handle_exception(e)
2021-06-16T08:58:56.485650+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
2021-06-16T08:58:56.485650+00:00 app[web.1]: response = self.full_dispatch_request()
2021-06-16T08:58:56.485650+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
2021-06-16T08:58:56.485651+00:00 app[web.1]: rv = self.handle_user_exception(e)
2021-06-16T08:58:56.485651+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
2021-06-16T08:58:56.485651+00:00 app[web.1]: rv = self.dispatch_request()
2021-06-16T08:58:56.485651+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
2021-06-16T08:58:56.485652+00:00 app[web.1]: return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
2021-06-16T08:58:56.485652+00:00 app[web.1]: File "/app/app/__init__.py", line 69, in index
2021-06-16T08:58:56.485664+00:00 app[web.1]: asset.publish()
2021-06-16T08:58:56.485665+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/contentful_management/resource.py", line 408, in publish
2021-06-16T08:58:56.485665+00:00 app[web.1]: result = self._client._put(
2021-06-16T08:58:56.485665+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/contentful_management/client.py", line 789, in _put
2021-06-16T08:58:56.485666+00:00 app[web.1]: return self._request('put', url, attributes, **kwargs)
2021-06-16T08:58:56.485666+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/contentful_management/client.py", line 757, in _request
2021-06-16T08:58:56.485667+00:00 app[web.1]: raise error
2021-06-16T08:58:56.485667+00:00 app[web.1]: contentful_management.errors.UnprocessableEntityError: HTTP status code: 422
2021-06-16T08:58:56.485667+00:00 app[web.1]: Message: Validation error
2021-06-16T08:58:56.485667+00:00 app[web.1]: Details:
2021-06-16T08:58:56.485668+00:00 app[web.1]: * Name: required - Path: '['fields', 'file', 'en-US', 'url']'
2021-06-16T08:58:56.485668+00:00 app[web.1]: Request ID: dd3048f785c9384dff75db0481cdb730
2021-06-16T08:58:56.485854+00:00 app[web.1]: 10.30.201.120 - - [16/Jun/2021:08:58:56 +0000] "POST / HTTP/1.1" 500 0 "-" "-"

Why is this happening? What does * Name: required - Path: '['fields', 'file', 'en-US', 'url']' even mean? I don't understand how it is possible for one script to work fine locally and not on the cloud, even though it doesn't seem to be a part which should create problems.

My repo is hosted here, if it helps

4

1 回答 1

2

Looking at your code, I'd suggest some changes (this is based on the documentation example https://github.com/contentful/contentful-management.py#assets - 'Creating asset' section):

1 - Store the asset you are creating in a variable

2 - Reload (if necessary the asset before publishing)

#[...]
new_upload = client.uploads(space).create(uploadmedia.stream)  

# Save the asset into a variable
new_asset = client.assets(space, 'master').create(asset_id,
    {
        'fields': {
            'title': {
                'en-US': fname
            },
            'file': {
                'en-US': {
                    'fileName': file_name,
                    'contentType': uploadmedia.content_type,
                    'uploadFrom': new_upload.to_link().to_json()
                }
            }
        }
    }
)

# Important: process the new_asset that was previously stored in the variable
# This calls `.reload()` in the asset as well
new_asset.process() 

# Process might take some time - normally it doesn't

# Process and Publish Asset
new_asset.reload().publish() 

The reload() refetches the asset, it's normally not needed since process() will do the same.

You can check the repository code for more hints in the behaviour of the library: https://github.com/contentful/contentful-management.py/blob/658341bc5af529b00fa317362c0b6cca221d76e4/contentful_management/asset.py#L42

于 2021-06-16T10:51:25.320 回答