import urllib
from django import template
from django.conf import settings

register = template.Library()


@register.simple_tag
def convert_url(request, sourceurl, format='pdf'):
    fullurl = request.build_absolute_uri(sourceurl)
    conversion_url = "%s?url=%s&to=%s" % (settings.CONVERSION_SERVER,
                                          urllib.quote(fullurl),
                                          format)
    return conversion_url

import urllib
from mimetypes import types_map

from django import template
from django.conf import settings


register = template.Library()


@register.simple_tag
def convert_url(request, sourceurl, format='pdf'):
    if '/' not in format:
        extension = '.' + format if not format.startswith('.') else format
        format = types_map[extension]
    fullurl = request.build_absolute_uri(sourceurl)
    conversion_url = "%s?url=%s&to=%s" % (settings.CONVERSION_SERVER,
                                          urllib.quote(fullurl),
                                          format)
    return conversion_url

Support conversion format as extension, instead of mimetype
Support conversion format as extension, instead of mimetype

  import urllib
+ from mimetypes import types_map
+ 
  from django import template
  from django.conf import settings
+ 
  
  register = template.Library()
  
  
  @register.simple_tag
  def convert_url(request, sourceurl, format='pdf'):
+     if '/' not in format:
+         extension = '.' + format if not format.startswith('.') else format
+         format = types_map[extension]
      fullurl = request.build_absolute_uri(sourceurl)
      conversion_url = "%s?url=%s&to=%s" % (settings.CONVERSION_SERVER,
                                            urllib.quote(fullurl),
                                            format)
      return conversion_url
  
Support conversion format as extension, instead of mimetype
## Code Before:
import urllib
from django import template
from django.conf import settings

register = template.Library()


@register.simple_tag
def convert_url(request, sourceurl, format='pdf'):
    fullurl = request.build_absolute_uri(sourceurl)
    conversion_url = "%s?url=%s&to=%s" % (settings.CONVERSION_SERVER,
                                          urllib.quote(fullurl),
                                          format)
    return conversion_url

## Instruction:
Support conversion format as extension, instead of mimetype
## Code After:
import urllib
from mimetypes import types_map

from django import template
from django.conf import settings


register = template.Library()


@register.simple_tag
def convert_url(request, sourceurl, format='pdf'):
    if '/' not in format:
        extension = '.' + format if not format.startswith('.') else format
        format = types_map[extension]
    fullurl = request.build_absolute_uri(sourceurl)
    conversion_url = "%s?url=%s&to=%s" % (settings.CONVERSION_SERVER,
                                          urllib.quote(fullurl),
                                          format)
    return conversion_url
