Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        if 'SF' in request.POST:
            Follower_to_followee.objects.filter(follower_id = request.user.id, \
                followee_id = self.object.auth_user_id).update(deleted = 1)
            return HttpResponseRedirect(self.object.get_absolute_url())
        if 'F' in request.POST:
            try:
                follower_to_followee = Follower_to_followee.objects.get(follower_id = request.user.id, \
                                                    followee_id = self.object.auth_user_id)
                follower_to_followee.deleted = 0

            except:
                follower_to_followee = Follower_to_followee(follower_id = request.user.id,\
                    followee_id = self.object.auth_user_id )


            follower_to_followee.save()
            return HttpResponseRedirect(self.object.get_absolute_url())
        if 'V' in request.POST:
            comment_form = CommentForm(prefix="C")
            picture_form = PictureForm(prefix="P")
            voting_form = VotingForm(data=request.POST, prefix="V")

            if voting_form.is_valid():
                chosen_vote_id = int(voting_form.cleaned_data['vote_choice'])
                existing_vote = Vote_to_brab.objects.filter(auth_user_id = request.user.id, brab_id=self.object.pk, )
                if not existing_vote:
                    vote_link = Vote_to_brab(auth_user_id = request.user.id, brab_id = self.object.pk, vote_id = chosen_vote_id)
                    vote_link.save()
                    existing_vote_total = Vote_totals.objects.filter(brab_id=self.object.pk, vote_id=chosen_vote_id)
                    if not existing_vote_total:
                        vote_total = Vote_totals(brab_id = self.object.pk, vote_id = chosen_vote_id, total = 1)
                    else:
                        vote_total = Vote_totals.objects.get(brab_id=self.object.pk, vote_id=chosen_vote_id)
                        vote_total.total = vote_total.total + 1

                    vote_total.save()
                else:
                    existing_vote = Vote_to_brab.objects.get(auth_user_id = request.user.id, brab_id=self.object.pk, )
                    old_vote_id = existing_vote.vote_id
                    existing_vote.vote_id = chosen_vote_id
                    existing_vote.save()

                    existing_old_vote_total = Vote_totals.objects.filter(brab_id=self.object.pk, vote_id=old_vote_id, )
                    existing_new_vote_total = Vote_totals.objects.filter(brab_id=self.object.pk, vote_id=chosen_vote_id, )
                    if existing_old_vote_total:
                        vote_total = Vote_totals.objects.get(brab_id=self.object.pk, vote_id=old_vote_id)
                        vote_total.total = vote_total.total - 1
                        vote_total.save()

                    if existing_new_vote_total:
                        vote_total = Vote_totals.objects.get(brab_id=self.object.pk, vote_id=chosen_vote_id)
                        vote_total.total = vote_total.total + 1
                    else:
                        vote_total = Vote_totals(brab_id = self.object.pk, vote_id = chosen_vote_id, total = 1)

                    vote_total.save()


            return HttpResponseRedirect(self.object.get_absolute_url())

        if 'C' in request.POST:
            comment_form = CommentForm(data=request.POST, prefix="C")
            picture_form = PictureForm(prefix="P")
            voting_form = VotingForm(prefix="V")
            if comment_form.is_valid():
    #            Fill comments.auth_user_id with request.user.id
                comment_form.instance.auth_user_id = request.user.id
    #            Fill comments.brab_id with pk of the current brab
                comment_form.instance.brab_id = self.object.pk
                comment_form.save()
                return HttpResponseRedirect(self.object.get_absolute_url())

        if 'P' in request.POST:
            comment_form = CommentForm(prefix="C")
            picture_form = PictureForm(data=request.POST, prefix="P", files=request.FILES )
            voting_form = VotingForm(prefix="V")
            if picture_form.is_valid():

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

               if not self.object.pictures_set.count():
                    picture_form.instance.main = 1
               elif picture_form.instance.main:

                    for picture in self.object.pictures_set.all():
                        picture.main = 0
                        picture.save()

                #            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()
               return HttpResponseRedirect(self.object.get_absolute_url())

        context = self.get_context_data(object=self.object, C_form=comment_form, P_form=picture_form, V_form=voting_form)
        return self.render_to_response(context)
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        if 'SF' in request.POST:
            Follower_to_followee.objects.filter(follower_id = request.user.id, \
                followee_id = self.object.auth_user_id).update(deleted = 1)
            return HttpResponseRedirect(self.object.get_absolute_url())
        if 'F' in request.POST:
            try:
                follower_to_followee = Follower_to_followee.objects.get(follower_id = request.user.id, \
                                                    followee_id = self.object.auth_user_id)
                follower_to_followee.deleted = 0

            except:
                follower_to_followee = Follower_to_followee(follower_id = request.user.id,\
                    followee_id = self.object.auth_user_id )

            follower_to_followee.save()
            return HttpResponseRedirect(self.object.get_absolute_url())
        if 'V' in request.POST:
            comment_form = CommentForm(prefix="C")
            picture_form = PictureForm(prefix="P")
            voting_form = VotingForm(data=request.POST, prefix="V")

            if voting_form.is_valid():
                chosen_vote_id = int(voting_form.cleaned_data['vote_choice'])
                existing_vote = Vote_to_brab.objects.filter(
                    auth_user_id=request.user.id,
                    brab_id=self.object.pk,
                )
                if not existing_vote:
                    vote_link = Vote_to_brab(auth_user_id=request.user.id,
                                             brab_id=self.object.pk,
                                             vote_id=chosen_vote_id)
                    vote_link.save()
                    existing_vote_total = Vote_totals.objects.filter(
                        brab_id=self.object.pk, vote_id=chosen_vote_id)
                    if not existing_vote_total:
                        vote_total = Vote_totals(brab_id=self.object.pk,
                                                 vote_id=chosen_vote_id,
                                                 total=1)
                    else:
                        vote_total = Vote_totals.objects.get(
                            brab_id=self.object.pk, vote_id=chosen_vote_id)
                        vote_total.total = vote_total.total + 1

                    vote_total.save()
                else:
                    existing_vote = Vote_to_brab.objects.get(
                        auth_user_id=request.user.id,
                        brab_id=self.object.pk,
                    )
                    old_vote_id = existing_vote.vote_id
                    existing_vote.vote_id = chosen_vote_id
                    existing_vote.save()

                    existing_old_vote_total = Vote_totals.objects.filter(
                        brab_id=self.object.pk,
                        vote_id=old_vote_id,
                    )
                    existing_new_vote_total = Vote_totals.objects.filter(
                        brab_id=self.object.pk,
                        vote_id=chosen_vote_id,
                    )
                    if existing_old_vote_total:
                        vote_total = Vote_totals.objects.get(
                            brab_id=self.object.pk, vote_id=old_vote_id)
                        vote_total.total = vote_total.total - 1
                        vote_total.save()

                    if existing_new_vote_total:
                        vote_total = Vote_totals.objects.get(
                            brab_id=self.object.pk, vote_id=chosen_vote_id)
                        vote_total.total = vote_total.total + 1
                    else:
                        vote_total = Vote_totals(brab_id=self.object.pk,
                                                 vote_id=chosen_vote_id,
                                                 total=1)

                    vote_total.save()

            return HttpResponseRedirect(self.object.get_absolute_url())

        if 'C' in request.POST:
            comment_form = CommentForm(data=request.POST, prefix="C")
            picture_form = PictureForm(prefix="P")
            voting_form = VotingForm(prefix="V")
            if comment_form.is_valid():
                #            Fill comments.auth_user_id with request.user.id
                comment_form.instance.auth_user_id = request.user.id
                #            Fill comments.brab_id with pk of the current brab
                comment_form.instance.brab_id = self.object.pk
                comment_form.save()
                return HttpResponseRedirect(self.object.get_absolute_url())

        if 'P' in request.POST:
            comment_form = CommentForm(prefix="C")
            picture_form = PictureForm(data=request.POST,
                                       prefix="P",
                                       files=request.FILES)
            voting_form = VotingForm(prefix="V")
            if picture_form.is_valid():

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

                if not self.object.pictures_set.count():
                    picture_form.instance.main = 1
                elif picture_form.instance.main:

                    for picture in self.object.pictures_set.all():
                        picture.main = 0
                        picture.save()

                #            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()
                return HttpResponseRedirect(self.object.get_absolute_url())

        context = self.get_context_data(object=self.object,
                                        C_form=comment_form,
                                        P_form=picture_form,
                                        V_form=voting_form)
        return self.render_to_response(context)
Ejemplo n.º 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)
Ejemplo n.º 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)