vobject
Python Apps #
tags :
https://github.com/eventable/vobject A full-featured Python package for parsing and creating iCalendar and vCard files
Summary #
A full-featured Python package for parsing and creating iCalendar and vCard files github
Used in projects #
used in the Azm X CMS project to make the team members profile download-able to save directly as contact in mobile phones, page ref
class CreateVCardView(View):
template_name = "precious_body_block.html"
def post(self, request, *args, **kwargs):
nerd_id = request.POST.get("nerd_id")
nerd = Nerd.objects.get(id=int(nerd_id))
card = self._create_vcard(nerd)
response = HttpResponse(content_type="text/x-vcard")
response[
"Content-Disposition"
] = '''attachment; filename=".vcf"'''.format(
nerd.name
)
response.write(card.serialize())
return response
def _create_vcard(self, nerd):
card = vobject.vCard()
attr = card.add("n")
attr.value = vobject.vcard.Name(given=nerd.name)
attr = card.add("fn")
attr.value = nerd.name if nerd.name is not None else ""
attr = card.add("email")
attr.type_param = "Email"
attr.value = nerd.email if nerd.email is not None else ""
attr = card.add("title")
attr.value = nerd.title if nerd.title is not None else ""
attr = card.add("tel")
attr.type_param = "Mobile"
attr.value = nerd.contact_num if nerd.contact_num is not None else ""
attr = card.add("org")
attr.value = ["AZMX"]
attr = card.add("URL")
attr.value = "https://azmx.sa"
attr = card.add("socialProfile")
attr.type_param = "linkedin"
attr.value = (
nerd.linked_in_profile if nerd.linked_in_profile is not None else ""
)
attr = card.add("socialProfile")
attr.type_param = "instagram"
attr.value = (
nerd.instagram_profile if nerd.instagram_profile is not None else ""
)
attr = card.add("socialProfile")
attr.type_param = "twitter"
attr.value = nerd.twitter_profile if nerd.twitter_profile is not None else ""
if nerd.image is not None:
attr = card.add("photo")
attr.type_param = "jpeg"
attr.encoding_param = "b"
attr.value = self._compressed_binary_encoded_image(nerd.image)
adr = card.add("adr")
adr.value = vobject.vcard.Address(
street=settings.COMPANY_ADDRESS["street"],
city=settings.COMPANY_ADDRESS["city"],
region=settings.COMPANY_ADDRESS["region"],
code=settings.COMPANY_ADDRESS["code"],
country=settings.COMPANY_ADDRESS["country"],
)
adr.type_param = "work"
# url = card.add("url")
# url.group = "item1"
# url.value = settings.COMPANY_ADDRESS["google_map_url"]
# x_ablabel = card.add("x-ablabel")
# x_ablabel.group = "item1"
# x_ablabel.value = "map url"
return card
def _compressed_binary_encoded_image(self, django_image_field):
image_url = self._image_full_path(django_image_field)
with urllib.request.urlopen(image_url) as image_file:
data = image_file.read()
image_file.close()
# Open image and compress
image = Image.open(BytesIO(data))
# If the image has an alpha channel, convert it to RGB
if image.mode in ("RGBA", "LA") or (
image.mode == "P" and "transparency" in image.info
):
image = image.convert("RGB")
# maximum fit will be 400x400 but aspect ratio will not change
image.thumbnail((400, 400))
# Save compressed image to BytesIO object
compressed_image = BytesIO()
image.save(
compressed_image,
format="JPEG",
quality=35
)
compressed_image.seek(0)
return compressed_image.read()
def _image_full_path(self, image):
# image_url = settings.MEDIA_ROOT + '/' + str(image.file) # local
image_url = (
settings.AWS_S3_ENDPOINT_URL
+ "/"
+ settings.AWS_STORAGE_BUCKET_NAME
+ "/public-media/"
+ str(image.file)
)
return image_url