Example #1
0
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        if not self.object.auth_user_id  == request.user.id:
            return HttpResponse('<div align="center"><h1>You are not the author of this brab</h1><br>...therefore you may not edit it!</div>')

        brabform = BrabForm(data=request.POST, instance=self.object, prefix="B")

        if brabform.is_valid():

            brabform.instance.auth_user_id = request.user.id

            tags = brabform.cleaned_data['tags']
            tags = self.parse_tags(tags)
            category = brabform.cleaned_data['category']

            brab = brabform.save(commit=False)

            brab.save()
            tag_count = self.add_tag_records(tags, request.user.id, brab.pk)
            category_count = self.add_category_records(category, request.user.id, brab.pk)


            picture_form = PictureForm(data=request.POST, prefix="P", files=request.FILES )
            picture_string = picture_form.data['P-new_picture']
            # This POST variable will only be filled if client browser supports awesomecropper plugin
            # because then the HTML5 canvas content will be posted, and not file read from the disk

            number_of_turns = int(picture_form.data['P-rotate'])

            if picture_string:
                file_to_add = CreateInMemoryUploadedFileFromBase64EncodedImage(picture_string, "P-picture", number_of_turns)
                if file_to_add:
                    request.FILES.appendlist("P-picture", file_to_add)
                    picture_form = PictureForm(data=request.POST, prefix="P", files=request.FILES )
            else:
                try:
                    image_file_name = request.FILES[u'P-picture'].name
                    new_image_file_present = True
                except:
                    new_image_file_present = False

                if new_image_file_present:
                    image_file_extension = image_file_name.split(".")[-1]
                    content_type = request.FILES[u'P-picture'].content_type

                    new_file_name = md5(str(localtime())).hexdigest()+image_file_extension

                    if number_of_turns == 0:
                        request.FILES[u'P-picture'].name = new_file_name
                    else:
                        rotated_file = RotateImage(request.FILES["P-picture"], number_of_turns, image_file_extension)
                        if rotated_file:

                            in_memory_uploaded_file = InMemoryUploadedFile(rotated_file, "P-picture", new_file_name, content_type, rotated_file.size, None)
                            request.FILES["P-picture"] = in_memory_uploaded_file

                    picture_form = PictureForm(data=request.POST, prefix="P", files=request.FILES )

            if picture_form.is_valid():

                if self.object.pictures_set.count():
                    picture_title = picture_form.cleaned_data['title'].title()
                    title_counter = 0
                    temp_title = picture_title
                    while self.object.pictures_set.filter(title__exact=temp_title):
                        title_counter = title_counter + 1
                        temp_title = picture_title + ' '+ str(title_counter).zfill(3)
                        picture_form.instance.title = temp_title

                if not self.object.pictures_set.count():
                    # This is the first picture we are adding to a brab
                    picture_form.instance.main = 1

                #   Fill picture.brab_id with pk of the current brab
                picture_form.instance.brab_id = self.object.pk
                picture_form.instance.visible = 1

                picture_form.save()
                new_picture_pk = picture_form.instance.id
            else:
                new_picture_pk = None

            for key in request.POST:
                if key.startswith('rotate_') and not (request.POST[key] == "0"):
                    picture_record_id = re.sub(r"\D", "", key)
                    number_of_turns = int(request.POST[key])
                    picture_to_rotate = Pictures.objects.get(id = picture_record_id)
                    RotateImageFromS3(picture_to_rotate.picture.name, number_of_turns)


                if key.startswith('delete_') and request.POST[key] == 'on':
                    picture_record_id = re.sub(r"\D", "", key)
                    Pictures.objects.filter(id = picture_record_id).update(deleted = 1)
                if key.startswith('makemain_') and request.POST[key] == 'on':
                    if self.object.pictures_set.count():
                        for picture in self.object.pictures_set.all():
                            picture.main = 0
                            picture.save()

                    picture_record_id = re.sub(r"\D", "", key)
                    if not picture_record_id:
                        picture_record_id = new_picture_pk
                    Pictures.objects.filter(id = picture_record_id).update(main = 1)

                if key.startswith('hide_') and request.POST[key] == 'on':
                    picture_record_id = re.sub(r"\D", "", key)
                    if not picture_record_id:
                        picture_record_id = new_picture_pk
                    Pictures.objects.filter(id = picture_record_id).update(visible = 0)

                if key.startswith('show_') and request.POST[key] == 'on':
                    picture_record_id = re.sub(r"\D", "", key)
                    Pictures.objects.filter(id = picture_record_id).update(visible = 1)

            return HttpResponseRedirect(brab.get_absolute_url())
        else:

            #        Find what categories and tags is currently edited brab marked with so that
            #        appropriate fields would appear pre-filled in the template
            selected_categories = Category_to_brab.objects.filter(brab_id = self.object.pk)
            categories = []
            if selected_categories:
                for category_instance in selected_categories:
                    categories.append(category_instance.category_id)

            selected_tags = Tag_to_brab.objects.filter(brab_id = self.object.pk)
            tags = ''
            tag_count = 0
            if selected_tags:
                for tag_instance in selected_tags:
                    if tag_count:
                        tags = tags + ', '
                    tags = tags + tag_instance.tag.tag
                    tag_count = tag_count + 1

            # brabform = BrabForm(instance=self.object, initial={'category':categories, 'tags':tags}, prefix="B")
            picture_form = PictureForm(prefix="P")

            # self.object = None
            context = self.get_context_data(object=self.object, brabform=brabform, P_form=picture_form)
            return self.render_to_response(context)
Example #2
0
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        if not self.object.auth_user_id == request.user.id:
            return HttpResponse(
                '<div align="center"><h1>You are not the author of this brab</h1><br>...therefore you may not edit it!</div>'
            )

        brabform = BrabForm(data=request.POST,
                            instance=self.object,
                            prefix="B")

        if brabform.is_valid():

            brabform.instance.auth_user_id = request.user.id

            tags = brabform.cleaned_data['tags']
            tags = self.parse_tags(tags)
            category = brabform.cleaned_data['category']

            brab = brabform.save(commit=False)

            brab.save()
            tag_count = self.add_tag_records(tags, request.user.id, brab.pk)
            category_count = self.add_category_records(category,
                                                       request.user.id,
                                                       brab.pk)

            picture_form = PictureForm(data=request.POST,
                                       prefix="P",
                                       files=request.FILES)
            picture_string = picture_form.data['P-new_picture']
            # This POST variable will only be filled if client browser supports awesomecropper plugin
            # because then the HTML5 canvas content will be posted, and not file read from the disk

            number_of_turns = int(picture_form.data['P-rotate'])

            if picture_string:
                file_to_add = CreateInMemoryUploadedFileFromBase64EncodedImage(
                    picture_string, "P-picture", number_of_turns)
                if file_to_add:
                    request.FILES.appendlist("P-picture", file_to_add)
                    picture_form = PictureForm(data=request.POST,
                                               prefix="P",
                                               files=request.FILES)
            else:
                try:
                    image_file_name = request.FILES[u'P-picture'].name
                    new_image_file_present = True
                except:
                    new_image_file_present = False

                if new_image_file_present:
                    image_file_extension = image_file_name.split(".")[-1]
                    content_type = request.FILES[u'P-picture'].content_type

                    new_file_name = md5(str(
                        localtime())).hexdigest() + image_file_extension

                    if number_of_turns == 0:
                        request.FILES[u'P-picture'].name = new_file_name
                    else:
                        rotated_file = RotateImage(request.FILES["P-picture"],
                                                   number_of_turns,
                                                   image_file_extension)
                        if rotated_file:

                            in_memory_uploaded_file = InMemoryUploadedFile(
                                rotated_file, "P-picture", new_file_name,
                                content_type, rotated_file.size, None)
                            request.FILES[
                                "P-picture"] = in_memory_uploaded_file

                    picture_form = PictureForm(data=request.POST,
                                               prefix="P",
                                               files=request.FILES)

            if picture_form.is_valid():

                if self.object.pictures_set.count():
                    picture_title = picture_form.cleaned_data['title'].title()
                    title_counter = 0
                    temp_title = picture_title
                    while self.object.pictures_set.filter(
                            title__exact=temp_title):
                        title_counter = title_counter + 1
                        temp_title = picture_title + ' ' + str(
                            title_counter).zfill(3)
                        picture_form.instance.title = temp_title

                if not self.object.pictures_set.count():
                    # This is the first picture we are adding to a brab
                    picture_form.instance.main = 1

                #   Fill picture.brab_id with pk of the current brab
                picture_form.instance.brab_id = self.object.pk
                picture_form.instance.visible = 1

                picture_form.save()
                new_picture_pk = picture_form.instance.id
            else:
                new_picture_pk = None

            for key in request.POST:
                if key.startswith('rotate_') and not (request.POST[key]
                                                      == "0"):
                    picture_record_id = re.sub(r"\D", "", key)
                    number_of_turns = int(request.POST[key])
                    picture_to_rotate = Pictures.objects.get(
                        id=picture_record_id)
                    RotateImageFromS3(picture_to_rotate.picture.name,
                                      number_of_turns)

                if key.startswith('delete_') and request.POST[key] == 'on':
                    picture_record_id = re.sub(r"\D", "", key)
                    Pictures.objects.filter(id=picture_record_id).update(
                        deleted=1)
                if key.startswith('makemain_') and request.POST[key] == 'on':
                    if self.object.pictures_set.count():
                        for picture in self.object.pictures_set.all():
                            picture.main = 0
                            picture.save()

                    picture_record_id = re.sub(r"\D", "", key)
                    if not picture_record_id:
                        picture_record_id = new_picture_pk
                    Pictures.objects.filter(id=picture_record_id).update(
                        main=1)

                if key.startswith('hide_') and request.POST[key] == 'on':
                    picture_record_id = re.sub(r"\D", "", key)
                    if not picture_record_id:
                        picture_record_id = new_picture_pk
                    Pictures.objects.filter(id=picture_record_id).update(
                        visible=0)

                if key.startswith('show_') and request.POST[key] == 'on':
                    picture_record_id = re.sub(r"\D", "", key)
                    Pictures.objects.filter(id=picture_record_id).update(
                        visible=1)

            return HttpResponseRedirect(brab.get_absolute_url())
        else:

            #        Find what categories and tags is currently edited brab marked with so that
            #        appropriate fields would appear pre-filled in the template
            selected_categories = Category_to_brab.objects.filter(
                brab_id=self.object.pk)
            categories = []
            if selected_categories:
                for category_instance in selected_categories:
                    categories.append(category_instance.category_id)

            selected_tags = Tag_to_brab.objects.filter(brab_id=self.object.pk)
            tags = ''
            tag_count = 0
            if selected_tags:
                for tag_instance in selected_tags:
                    if tag_count:
                        tags = tags + ', '
                    tags = tags + tag_instance.tag.tag
                    tag_count = tag_count + 1

            # brabform = BrabForm(instance=self.object, initial={'category':categories, 'tags':tags}, prefix="B")
            picture_form = PictureForm(prefix="P")

            # self.object = None
            context = self.get_context_data(object=self.object,
                                            brabform=brabform,
                                            P_form=picture_form)
            return self.render_to_response(context)
Example #3
0
    def post(self, request, *args, **kwargs):

        brabform = BrabForm(data=request.POST)

        if brabform.is_valid():
        #            Fill comments.auth_user_id with request.user.id
            brabform.instance.auth_user_id = request.user.id
            #            Fill comments.brab_id with pk of the current brab
            tags = brabform.cleaned_data['tags']
            tags = self.parse_tags(tags)
            category = brabform.cleaned_data['category']

            brab = brabform.save(commit=False)
            brabformset = BrabFormSet(request.POST, request.FILES, instance=brab)

            # --------------------------------------------------------------------------------------------
            picture_string = brabformset.forms[0].data['pictures_set-0-new_picture']
            # This POST variable will only be filled if client browser supports awesomecropper plugin
            # because then the HTML5 canvas content will be posted, and not file read from the disk

            number_of_turns = int(brabformset.forms[0].data['pictures_set-0-rotate'])

            if picture_string:
                file_to_add = CreateInMemoryUploadedFileFromBase64EncodedImage(picture_string, "pictures_set-0-picture", number_of_turns)
                if file_to_add:
                    request.FILES.appendlist("pictures_set-0-picture", file_to_add)
                    brabformset = BrabFormSet(request.POST, request.FILES, instance=brab)
            else:
                try:
                    image_file_name = request.FILES[u'pictures_set-0-picture'].name
                    new_image_file_present = True
                except:
                    new_image_file_present = False

                if new_image_file_present:
                    image_file_extension = image_file_name.split(".")[-1]
                    content_type = request.FILES[u'pictures_set-0-picture'].content_type

                    new_file_name = md5(str(localtime())).hexdigest()+image_file_extension

                    if number_of_turns == 0:
                        request.FILES[u'pictures_set-0-picture'].name = new_file_name
                    else:
                        rotated_file = RotateImage(request.FILES["pictures_set-0-picture"], number_of_turns, image_file_extension)
                        if rotated_file:

                            in_memory_uploaded_file = InMemoryUploadedFile(rotated_file, "pictures_set-0-picture", new_file_name, content_type, rotated_file.size, None)
                            request.FILES["pictures_set-0-picture"] = in_memory_uploaded_file

                    brabformset = BrabFormSet(request.POST, request.FILES, instance=brab)

            # --------------------------------------------------------------------------------------------

            if brabformset.is_valid():
                brab.save()
                picture = brabformset.save(commit=False)
                picture[0].visible = 1
                picture[0].main = 1
                picture[0].save()
                tag_count = self.add_tag_records(tags, request.user.id, brab.pk)
                category_count = self.add_category_records(category, request.user.id, brab.pk)
                #            add vote totals - one per each vote choice...
                for x in Vote.objects.filter(visible=1):
                    vote_total = Vote_totals(brab_id = brab.pk, vote_id = x.id, total = 0)
                    vote_total.save()
                return HttpResponseRedirect(brab.get_absolute_url())
        else:

            brabformset = BrabFormSet(request.POST, request.FILES)

        self.object = None
        context = self.get_context_data(object=self.object, brabform=brabform, brabformset=brabformset)
        return self.render_to_response(context)
Example #4
0
    def post(self, request, *args, **kwargs):

        brabform = BrabForm(data=request.POST)

        if brabform.is_valid():
            #            Fill comments.auth_user_id with request.user.id
            brabform.instance.auth_user_id = request.user.id
            #            Fill comments.brab_id with pk of the current brab
            tags = brabform.cleaned_data['tags']
            tags = self.parse_tags(tags)
            category = brabform.cleaned_data['category']

            brab = brabform.save(commit=False)
            brabformset = BrabFormSet(request.POST,
                                      request.FILES,
                                      instance=brab)

            # --------------------------------------------------------------------------------------------
            picture_string = brabformset.forms[0].data[
                'pictures_set-0-new_picture']
            # This POST variable will only be filled if client browser supports awesomecropper plugin
            # because then the HTML5 canvas content will be posted, and not file read from the disk

            number_of_turns = int(
                brabformset.forms[0].data['pictures_set-0-rotate'])

            if picture_string:
                file_to_add = CreateInMemoryUploadedFileFromBase64EncodedImage(
                    picture_string, "pictures_set-0-picture", number_of_turns)
                if file_to_add:
                    request.FILES.appendlist("pictures_set-0-picture",
                                             file_to_add)
                    brabformset = BrabFormSet(request.POST,
                                              request.FILES,
                                              instance=brab)
            else:
                try:
                    image_file_name = request.FILES[
                        u'pictures_set-0-picture'].name
                    new_image_file_present = True
                except:
                    new_image_file_present = False

                if new_image_file_present:
                    image_file_extension = image_file_name.split(".")[-1]
                    content_type = request.FILES[
                        u'pictures_set-0-picture'].content_type

                    new_file_name = md5(str(
                        localtime())).hexdigest() + image_file_extension

                    if number_of_turns == 0:
                        request.FILES[
                            u'pictures_set-0-picture'].name = new_file_name
                    else:
                        rotated_file = RotateImage(
                            request.FILES["pictures_set-0-picture"],
                            number_of_turns, image_file_extension)
                        if rotated_file:

                            in_memory_uploaded_file = InMemoryUploadedFile(
                                rotated_file, "pictures_set-0-picture",
                                new_file_name, content_type, rotated_file.size,
                                None)
                            request.FILES[
                                "pictures_set-0-picture"] = in_memory_uploaded_file

                    brabformset = BrabFormSet(request.POST,
                                              request.FILES,
                                              instance=brab)

            # --------------------------------------------------------------------------------------------

            if brabformset.is_valid():
                brab.save()
                picture = brabformset.save(commit=False)
                picture[0].visible = 1
                picture[0].main = 1
                picture[0].save()
                tag_count = self.add_tag_records(tags, request.user.id,
                                                 brab.pk)
                category_count = self.add_category_records(
                    category, request.user.id, brab.pk)
                #            add vote totals - one per each vote choice...
                for x in Vote.objects.filter(visible=1):
                    vote_total = Vote_totals(brab_id=brab.pk,
                                             vote_id=x.id,
                                             total=0)
                    vote_total.save()
                return HttpResponseRedirect(brab.get_absolute_url())
        else:

            brabformset = BrabFormSet(request.POST, request.FILES)

        self.object = None
        context = self.get_context_data(object=self.object,
                                        brabform=brabform,
                                        brabformset=brabformset)
        return self.render_to_response(context)