Esempio n. 1
0
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        formset = self.ModelFormSet(
            request.POST, request.FILES, prefix=self.formset_prefix)
        if formset.is_valid():
            instances = formset.save(commit=False)
            for instance in instances:
                instance.event = self.object
                try:
                    instance.user = User.objects.get(username=instance.email)
                    try:
                        instance.save()
                    except IntegrityError:
                        pass
                    juror_added.send(
                        self, juror=instance, created=False, password='')
                except User.DoesNotExist:
                    title = u'{0} {1} {2}'.format(
                        instance.info_second_name,
                        instance.info_name,
                        instance.info_middle_name
                    )
                    profile = Profile(
                        username=instance.email,
                        title=title,
                        info_second_name=instance.info_second_name,
                        info_name=instance.info_name,
                        info_middle_name=instance.info_middle_name
                    )
                    password = User.objects.make_random_password()
                    profile.set_password(password)
                    profile.save()
                    instance.user_id = profile.id
                    instance.save()

                    instance.min_avatar.seek(0)
                    minified_image = Image.open(instance.min_avatar)
                    minified_image = image_crop_rectangle_center(minified_image)
                    minified_image.thumbnail((125, 125), Image.ANTIALIAS)
                    # reinitialize memory file
                    memory_file = StringIO()
                    minified_image.save(memory_file, 'jpeg')
                    memory_file.seek(0)

                    profile.min_avatar.save(
                        '{id}-min-avatar.jpg'.format(id=profile.id),
                        ContentFile(memory_file.read())
                    )
                    profile.save()
                    juror_added.send(
                        self, juror=instance, created=True, password=password)
            return HttpResponseRedirect(self.get_success_url())
        return self.render_to_response(self.get_context_data(formset=formset))
Esempio n. 2
0
def relate_juror_and_profile(sender, **kwargs):
    if kwargs['raw']:
        return
    instance = kwargs['instance']
    try:
        instance.user = Profile.objects.get(username=instance.email)
        juror_added.send(None, juror=instance, created=False, password='')
    except Profile.DoesNotExist:
        title = u'{0} {1} {2}'.format(
            instance.info_second_name,
            instance.info_name,
            instance.info_middle_name
        )
        profile = Profile(
            username=instance.email,
            title=title,
            info_second_name=instance.info_second_name,
            info_name=instance.info_name,
            info_middle_name=instance.info_middle_name
        )
        password = Profile.objects.make_random_password()
        profile.set_password(password)
        profile.save()
        instance.user_id = profile.id

        instance.min_avatar.seek(0)
        minified_image = Image.open(instance.min_avatar)
        minified_image = image_crop_rectangle_center(minified_image)
        minified_image.thumbnail((125, 125), Image.ANTIALIAS)
        # reinitialize memory file
        memory_file = StringIO()
        minified_image.save(memory_file, 'jpeg')
        memory_file.seek(0)

        profile.min_avatar.save(
            '{id}-min-avatar.jpg'.format(id=profile.id),
            ContentFile(memory_file.read())
        )
        profile.save()
        juror_added.send(None, juror=instance, created=True, password=password)
Esempio n. 3
0
    def post(self, request, *args, **kwargs):
        formset = self.FormSet(request.POST,
                               request.FILES,
                               instance=self.get_object())

        if formset.is_valid():
            formset.save()

            for fm in formset:
                photo = fm.cleaned_data.get('photo')

                if photo:
                    model_instance = fm.save(commit=False)

                    # setting read pointer to beginning of the file
                    photo.file.seek(0)
                    # loading image into memory
                    original_image = Image.open(photo)
                    memory_file = StringIO()
                    # setting up correct mode
                    if original_image.mode not in ('L', 'RGB'):
                        original_image = original_image.convert('RGB')

                    # if width is greater, then resize image
                    MAX_WIDTH = 800

                    if original_image.size[0] > MAX_WIDTH:
                        aspect = original_image.size[0] / MAX_WIDTH
                        new_height = int(round(original_image.size[1] / aspect))
                        original_image = original_image.resize(
                            (MAX_WIDTH, new_height), Image.ANTIALIAS)

                    # storing image into memory file
                    original_image.save(memory_file, 'jpeg')
                    memory_file.seek(0)

                    # deleting old image file
                    model_instance.photo.delete()
                    # and saving new processed original image
                    model_instance.photo.save(
                        '{id}-full.jpg'.format(
                            id=model_instance.id
                        ),
                        ContentFile(memory_file.read())
                    )

                    # loading original image, already normalized
                    minified_image = Image.open(model_instance.photo)

                    minified_image = image_crop_rectangle_center(minified_image)

                    minified_image.thumbnail((125, 125), Image.ANTIALIAS)

                    # reinitialize memory file
                    memory_file = StringIO()
                    minified_image.save(memory_file, 'jpeg')
                    memory_file.seek(0)

                    model_instance.photo_min.save(
                        '{id}-min.jpg'.format(
                            id=model_instance.id
                        ),
                        ContentFile(memory_file.read())
                    )

            notify_success(self.request, message=u'''
                Информация вашего профиля успешно обновлена.<br/>
                 <a href="{0}">Перейти к просмотру профиля</a>
            '''.format(reverse("profile-detail", args=[self.get_object().id])))

            return HttpResponseRedirect(self.get_success_url())
        else:
            self.object = self.get_object()
            notify_error(self.request, message=u'''
                При обновлении данных профиля произошла ошибка.
            ''')

            return self.render_to_response(self.get_context_data(formset=formset))