# Path: twobuntu/touch/forms.py
# class DeviceArtForm(forms.Form):
#     """
#     Form for uploading an app image.
#     """
# 
#     template = forms.TypedChoiceField(
#         initial='bq-aquaris',
#         choices=[(k, TEMPLATES[k]['title']) for k in sorted(TEMPLATES.keys())],
#     )
#     add_panel = forms.BooleanField(
#         label="Add Unity panel",
#         required=False,
#         help_text='Remember to take into account the height of the panel when uploading the image.',
#     )
#     glossy_screen = forms.BooleanField(
#         required=False,
#         initial=True,
#     )
#     image = forms.ImageField()
# 
# Path: twobuntu/touch/generator.py
# def generate_device_art(template, image, panel, glossy):
#     """
#     Combine the layers for the template into a final image.
#     """
#     output = Image.new('RGBA', TEMPLATES[template]['frame'])
#     blit_source_image(output, template, image, panel)
#     if panel:
#         output = blit_template_image(output, template, 'panel.png')
#     output = blit_template_image(output, template, 'frame.png')
#     if glossy:
#         output = blit_template_image(output, template, 'gloss.png')
#     response = BytesIO()
#     output.save(response, format='PNG')
#     return response.getvalue()


from django.http import HttpResponse
from django.shortcuts import render
from twobuntu.touch.forms import DeviceArtForm
from twobuntu.touch.generator import generate_device_art



def generator(request):
    """
    Generate a picture of a device with a user-supplied image.
    """
    if request.method == 'POST':
        form = DeviceArtForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            image = generate_device_art(
                form.cleaned_data['template'],
                request.FILES['image'],
                form.cleaned_data['add_panel'],
                form.cleaned_data['glossy_screen'],
            )
            response = HttpResponse(image, content_type='image/png')
            response['Content-Disposition'] = 'attachment; filename="ubuntu-touch-device-art.png"'
            response['Content-Length'] = len(image)
            return response
    else:

# Path: twobuntu/touch/forms.py
# class DeviceArtForm(forms.Form):
#     """
#     Form for uploading an app image.
#     """
# 
#     template = forms.TypedChoiceField(
#         initial='bq-aquaris',
#         choices=[(k, TEMPLATES[k]['title']) for k in sorted(TEMPLATES.keys())],
#     )
#     add_panel = forms.BooleanField(
#         label="Add Unity panel",
#         required=False,
#         help_text='Remember to take into account the height of the panel when uploading the image.',
#     )
#     glossy_screen = forms.BooleanField(
#         required=False,
#         initial=True,
#     )
#     image = forms.ImageField()
# 
# Path: twobuntu/touch/generator.py
# def generate_device_art(template, image, panel, glossy):
#     """
#     Combine the layers for the template into a final image.
#     """
#     output = Image.new('RGBA', TEMPLATES[template]['frame'])
#     blit_source_image(output, template, image, panel)
#     if panel:
#         output = blit_template_image(output, template, 'panel.png')
#     output = blit_template_image(output, template, 'frame.png')
#     if glossy:
#         output = blit_template_image(output, template, 'gloss.png')
#     response = BytesIO()
#     output.save(response, format='PNG')
#     return response.getvalue()


# Path: twobuntu/touch/views.py
from django.http import HttpResponse
from django.shortcuts import render
from twobuntu.touch.forms import DeviceArtForm
from twobuntu.touch.generator import generate_device_art




def generator(request):
    """
    Generate a picture of a device with a user-supplied image.
    """
    if request.method == 'POST':
        form = DeviceArtForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            image = generate_device_art(
                form.cleaned_data['template'],
                request.FILES['image'],
                form.cleaned_data['add_panel'],
                form.cleaned_data['glossy_screen'],
            )
            response = HttpResponse(image, content_type='image/png')
            response['Content-Disposition'] = 'attachment; filename="ubuntu-touch-device-art.png"'
            response['Content-Length'] = len(image)
            return response
    else:

        form = DeviceArtForm()