python-card-me icon indicating copy to clipboard operation
python-card-me copied to clipboard

New PyPI Release

Open nsomaru opened this issue 6 years ago • 0 comments

edit: The fix for this is to install directly from the git repository, PyPI release is not the latest code on master. Leaving the below for @tBaxter reference.

Good Day TBaxter,

Thank you for a the project. I had been using it fine with python2, but a migration to python3 is tripping me up and I'm not sure how to fix it. I hope you can help.

The exception:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/profiles/testuser/vcard/

Django Version: 2.2.8
Python Version: 3.6.9
Installed Applications:
['whitenoise.runserver_nostatic',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'django.contrib.admin',
 'crispy_forms',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'rest_framework',
 'django_celery_beat',
 'ckeditor',
 'import_export',
 'sorl.thumbnail',
 'django_countries',
 'cities_light',
 'django_select2',
 'photologue',
 'sortedm2m',
 'reversion',
 'notifications',
 'lifconnect.users.apps.UsersConfig',
 'lifconnect.invitations',
 'lifconnect.events',
 'lifconnect.general',
 'compressor',
 'kombu.transport.redis',
 'debug_toolbar',
 'django_extensions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'lifconnect.general.middleware.StaffMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware']



Traceback:

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.6/contextlib.py" in inner
  52.                 return func(*args, **kwds)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "/home/myuser/dev/vedantaworld/myproject/lifconnect/users/views.py" in create_vcard
  113.         user.profile.vcard().serialize(), content_type="text/vcard"

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/card_me/base.py" in serialize
  216.             return behavior.serialize(self, buf, lineLength, validate)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/card_me/behavior.py" in serialize
  162.         out = base.defaultSerialize(transformed, buf, lineLength)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/card_me/base.py" in defaultSerialize
  966.             child.serialize(outbuf, lineLength, validate=False)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/card_me/base.py" in serialize
  216.             return behavior.serialize(self, buf, lineLength, validate)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/card_me/vcard.py" in serialize
  214.         VCardTextBehavior.serialize(obj, buf, lineLength, validate)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/card_me/behavior.py" in serialize
  162.         out = base.defaultSerialize(transformed, buf, lineLength)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/card_me/base.py" in defaultSerialize
  974.             obj.behavior.encode(obj)

File "/home/myuser/.virtualenvs/myproject/lib/python3.6/site-packages/card_me/vcard.py" in encode
  140.                 line.value = line.value.encode('base64').replace('\n', '')

Exception Type: AttributeError at /profiles/testuser/vcard/
Exception Value: 'bytes' object has no attribute 'encode'

This is how I'm creating the card (it works fine):

    def vcard(self):
        the_card = card_me.vCard()
        the_card.add("n")
        the_card.n.value = card_me.vcard.Name(given=self.user.name)
        the_card.add("fn")
        the_card.fn.value = u"%s" % (self.user.name)
        the_card.add("email")
        the_card.email.value = self.user.email
        the_card.email.type_param = "WORK"
        the_card.add("tel")
        the_card.tel.value = "%s" % self.mobile_number
        the_card.tel.type_param = "WORK"
        the_card.add("adr")
        the_card.adr.value = card_me.vcard.Address(country=(self.country.name,))
        the_card.adr.type_parm = "HOME"
        the_card.add("photo")
        buf = BytesIO()
        img = Image.open(self.image.file)
        img.thumbnail((600, 600), Image.ANTIALIAS)
        img.save(buf, img.format)
        the_card.photo.value = buf.getvalue()
        the_card.photo.encoding_param = "b"
        the_card.photo.type_param = img.format
        if self.company_name:
            the_card.add("org")
            # we need to do this like this because of the way this is serialised
            the_card.org.value = (self.company_name,)
        if self.website:
            the_card.add("url")
            the_card.url.value = self.website
        return the_card

However, when trying to serialize as so the problem occurs:

@login_required
def create_vcard(request, username):
    user = get_object_or_404(User, username=username, is_staff=False)
    response = HttpResponse(
        user.profile.vcard().serialize(), content_type="text/vcard" # problem occurs here
    )
    filename = "contact.vcf"
    response["Filename"] = filename
    response["Content-Disposition"] = "attachment; filename=%s" % filename
    return response

Thanks again for the project. Apologies if this is the wrong place to ask this.

nsomaru avatar Jan 26 '20 12:01 nsomaru