Repository: fossasia/open-event-server

Issue:
HTTP 500 Error in Order Creation
**Describe the bug**
Getting HTTP 500 when trying to create Order using sample data from API docs.

**To Reproduce**
Create Order using sample API docs.

**Stacktrace**
`{
    "errors": [
        {
            "detail": "Unknown error",
            "source": {
                "pointer": ""
            },
            "status": 500,
            "title": "Unknown error"
        }
    ],
    "jsonapi": {
        "version": "1.0"
    }
}`

**Additional details (please complete the following information):**
The request was made using Postman on Linux.

**Would you like to work on it**
Yes



Discussion:
@Masquerade0097 Have you started working on this ? If yes, then please do it as a priority as it is needed in order to implement other features in the orders api. If no, then I would suggest that you let me fix this as I am already implementing other features. 
```
Traceback (most recent call last):
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/app/__init__.py", line 64, in __call__
    return self.app(environ, start_response)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/app/api/helpers/permissions.py", line 45, in decorator
    return fn(*args, **kwargs)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 32, in wrapper
    return func(*args, **kwargs)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/views.py", line 88, in view
    return self.dispatch_request(*args, **kwargs)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 68, in dispatch_request
    response = method(*args, **kwargs)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 56, in wrapper
    return func(*args, **kwargs)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 204, in post
    obj = self._data_layer.create_object(data, kwargs)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/src/flask-rest-jsonapi/flask_rest_jsonapi/data_layers/alchemy.py", line 51, in create_object
    self.after_create_object(obj, data, view_kwargs)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/app/api/orders.py", line 84, in after_create_object
    pdf = create_save_pdf(render_template('/pdf/ticket_attendee.html', order=order, holder=holder))
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/templating.py", line 135, in render_template
    context, ctx.app)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/flask/templating.py", line 117, in _render
    rv = template.render(context)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/app/templates/pdf/ticket_attendee.html", line 62, in top-level template code
    <img src="data:image/png;base64,{{ holder.qr_code }}" />
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
    return getattr(obj, attribute)
  File "/media/srv_twry/work/Projects/Community-Projects/Fossasia/open-event-server/app/models/ticket_holder.py", line 138, in qr_code
    img.save(buffer, format="JPEG")
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/qrcode/image/pil.py", line 47, in save
    self._img.save(stream, format=format, **kwargs)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/PIL/Image.py", line 1935, in save
    save_handler(self, fp, filename)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 761, in _save
    ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
  File "/home/srv_twry/anaconda3/envs/open-event-server/lib/python3.6/site-packages/PIL/ImageFile.py", line 492, in _save
    fp.write(d)
TypeError: string argument expected, got 'bytes'
```
@srv-twry Yes, please take this.

Resolving patch:
diff --git a/app/api/helpers/files.py b/app/api/helpers/files.py
--- a/app/api/helpers/files.py
+++ b/app/api/helpers/files.py
@@ -14,9 +14,9 @@
 from xhtml2pdf import pisa
 
 from app import get_settings
+from app.api.helpers.exceptions import UnprocessableEntity
 from app.api.helpers.storage import UploadedFile, upload, generate_hash, UPLOAD_PATHS
 from app.models.image_size import ImageSizes
-from app.api.helpers.exceptions import UnprocessableEntity
 
 
 def get_file_name():
@@ -211,7 +211,7 @@ def create_save_pdf(pdf_data):
     dest = filedir + filename
 
     file = open(dest, "wb")
-    pisa.CreatePDF(io.StringIO(pdf_data.encode('utf-8')), file)
+    pisa.CreatePDF(io.BytesIO(pdf_data.encode('utf-8')), file)
     file.close()
 
     uploaded_file = UploadedFile(dest, filename)
diff --git a/app/models/ticket_holder.py b/app/models/ticket_holder.py
--- a/app/models/ticket_holder.py
+++ b/app/models/ticket_holder.py
@@ -1,5 +1,5 @@
 import base64
-from io import StringIO
+from io import BytesIO
 
 import qrcode
 
@@ -134,7 +134,7 @@ def qr_code(self):
         qr.make(fit=True)
         img = qr.make_image()
 
-        buffer = StringIO()
+        buffer = BytesIO()
         img.save(buffer, format="JPEG")
         img_str = str(base64.b64encode(buffer.getvalue()), 'utf-8')
         return img_str


Test patch: