Example #1
0
    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        comment_form = CommentForm(prefix="C")
        picture_form = PictureForm(prefix="P")

        #        Find if this user have voted already
        existing_vote = Vote_to_brab.objects.filter(
            auth_user_id=request.user.id,
            brab_id=self.object.pk,
        )
        if existing_vote:
            voting_form = VotingForm(prefix="V",
                                     initial={
                                         'vote_choice':
                                         existing_vote._result_cache[0].vote_id
                                     })
            current_vote = existing_vote._result_cache[0].vote_id
        else:
            voting_form = VotingForm(prefix="V")
            current_vote = 0


#        Create a data structure (list of dictionaries) with information on available vote choices
#        which one is currently selected by the user, totals of vote for each choice to pass to template in context
        votes_data = []
        current_vote_totals = Vote_totals.objects.filter(
            brab_id=self.object.pk)
        for x in Vote.objects.filter(visible=1):
            if x.id == current_vote:
                vote_selected = 1
            else:
                vote_selected = 0
            try:
                #                vote_total=Vote_totals.objects.get(brab_id=self.object.pk,vote_id=x.id).total
                vote_total = current_vote_totals.get(vote_id=x.id).total
            except:
                vote_total = 0
            vote_data = {
                'id': x.id,
                'name': x.name,
                'past': x.past,
                'selected': vote_selected,
                'total': vote_total
            }
            votes_data.append(vote_data)

            fq = Follower_to_followee.objects.filter(
                follower_id=request.user.id,
                followee_id=self.object.auth_user_id,
                deleted=0)
            if fq:
                followed_by_logged_in_user = 1
            else:
                followed_by_logged_in_user = 0
        context = self.get_context_data(object=self.object, C_form=comment_form, P_form=picture_form, V_form=voting_form,\
            current_vote =  current_vote, votes_data = votes_data, followed_by_logged_in_user=followed_by_logged_in_user)

        return self.render_to_response(context)
Example #2
0
    def get(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>'
            )

        if self.request.path == "/deletebrab/" + str(self.object.pk) + '/':
            self.object.deleted = 1
            self.object.save()
            # return HttpResponse('<div align="center"><h1>Brab #'+str(self.object.pk)+' deleted!</div>')
            return redirect('/mybrabs/')


#        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")

        #        The section below did not work out (actually it did exactly what intended, but
        #        then there were a bunch of difficulties in displaying pre-filled formset in template, etc.)

        #        brabformset = BrabFormSet(instance=self.object)
        ##        Fill inline brabformset picture forms with data about attached pictures
        #        picture_set = self.object.pictures_set.all()
        ##        The statement above fires the query
        #        test_stru = zip(brabformset.forms, picture_set)
        #        for subform, data in test_stru:
        #            subform.initial = data

        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):
        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)
Example #4
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 #5
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)
Example #6
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)