Exemplo n.º 1
0
def post_edit(request, url_name, category_name, post_pk):
    if request.user.is_anonymous:
        return redirect_with_next('core:accounts:login',
                                  'core:board:post_detail',
                                  params={
                                      'to': [url_name],
                                      'next':
                                      [url_name, category_name, post_pk]
                                  })
    univ = get_object_or_404(Univ, url_name=url_name)
    post = Post.objects.get(pk=post_pk)
    if request.user != post.author:
        return redirect('core:board:post_detail', url_name, category_name,
                        post_pk)

    form = PostForm(request.POST or None, request=request, instance=post)
    if request.method == 'POST':
        if form.is_valid():
            post = form.save(commit=False)
            if post.ctgy.is_anonymous:
                post.is_anonymous = True
            post.save()
            for image in request.FILES.getlist('images'):
                Image.objects.create(post=post, image=image)
            return redirect('core:board:post_detail', url_name, category_name,
                            post_pk)
    return render(request, 'board/post_new.html', {
        'form': form,
        'univ': univ,
        'url_name': url_name
    })
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = PostForm(obj=self.object, data=request.POST)

        if form.is_valid():
            form.save()
            return redirect(self.object)

        context = self.get_context_data(object=self.object, form=form)
        return self.render_to_response(context)
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = PostForm(obj=self.object, data=request.POST)

        if form.is_valid():
            form.save()
            return redirect(self.object)

        context = self.get_context_data(object=self.object, form=form)
        return self.render_to_response(context)
Exemplo n.º 4
0
def board_detail(request, post_id):
    user_name = request.session['userss']
    user_id = request.session['user_id']
    post = get_object_or_404(Post, pk=post_id)
    chat = Chat.objects.all()
    if request.method == 'POST':
        post_form = PostForm(request.POST, instance=post)
        print("디테일 포스트로 들어옴 ")

    else:
        post_form = PostForm(instance=post)
        for i in post_form.fields:  # 수정이 되지 않도록 처리
            post_form.fields[i].disabled = True
        comment_form = CommentForm()
        print("디테일 엘스 들어옴 ")

    return render(request, 'bbs_detail.html', {'post_form': post_form, 'post': post, 'comment_form': comment_form, 'userss': user_name, 'user_id': user_id, 'chat': chat})
Exemplo n.º 5
0
def board_update(request, post_id):

    user_name = request.session['userss']
    user_id = request.session['user_id']


    chat = Chat.objects.all()

    post = get_object_or_404(Post, pk=post_id)
    context={'chat': chat,'userss': user_name,'user_id': user_id}
    if request.method == 'POST':
        post_form = PostForm(request.POST, instance=post)

        if post_form.is_valid(): # 입력한 데이터에 문제가 없다면
            post_form.save() # 포스트 폼에 저장한다
            context['post_form']=post_form
            posts = Post.objects.all()
            context['bbspage'] = posts
            return render(request,'bbs.html', context)
            # 네임스페이스가 posts이고 urlpattern에서 name이 list인
            # url로 리다이렉션

    else:
        post_form = PostForm(instance=post)

        # 수정 시 빈 칸이 아니라 instance에 post 데이터를 가져오는 칸을 만들어줌


        return render(request, 'bbs_create.html', {'post_form': post_form, 'userss': user_name, 'user_id': user_id, 'chat': chat})
Exemplo n.º 6
0
def board_send_comment(request, post_id):
        chat = Chat.objects.all()
        user_name = request.session['userss']
        user_id = request.session['user_id']
        print("Comment 들어왔다")
        post = get_object_or_404(Post, pk=post_id)
        comment = Comment(comment=post)
        # comment = Comment(pk=post_id)

        comment_list = Comment.objects.filter(comment=post_id).order_by('-created_date')
        # print(comment_list)
        post_form = PostForm(request.POST, instance=post)


        if request.method == "POST":
            print("post 들어왔다 ")
            form = CommentForm(request.POST, instance=comment)

            if form.is_valid():
                comment = form.save(commit=False)
                comment.post = post
                comment.author = user_name
                comment.save()
                page = request.GET.get('page', '1')
                paginator = Paginator(comment_list, 5)
                print("page")
                commentpage = paginator.get_page(page)
                context = {'comment_list': comment_list,'post_form': post_form, 'post': post, 'comment_form': form, 'page': page, 'commentpage': commentpage, 'userss': user_name, 'user_id': user_id}

                return render(request, 'bbs_detail.html',context)


        else:
            form = CommentForm(request.GET)
            page = request.GET.get('page','1')
            paginator = Paginator(comment_list, 5)
            commentpage = paginator.get_page(page)

            context = {'comment_list': comment_list,'post_form': post_form, 'post': post, 'comment_form': form, 'page': page, 'commentpage': commentpage, 'userss': user_name, 'user_id': user_id, 'chat': chat}


            if form.is_valid():
                comment = form.save(commit=False)
                comment.post = post
                comment.author = user_name
                comment.save()
                page = request.GET.get('page', '1')
                paginator = Paginator(comment_list, 5)
                commentpage = paginator.get_page(page)

                context = {'comment_list': comment_list,'post_form': post_form, 'post': post, 'comment_form': form, 'page': page, 'commentpage': commentpage, 'userss': user_name, 'user_id': user_id, 'chat': chat}

                return render(request, 'bbs_detail.html', context)

        print("pComment 그린다 ")
        return render(request, 'bbs_detail.html', context)
Exemplo n.º 7
0
def post_create(request, url_name):
    if request.user.is_anonymous:
        return redirect_with_next('core:accounts:login',
                                  'core:board:post_create',
                                  params={
                                      'to': [url_name],
                                      'next': [url_name]
                                  })
    try:
        can_use(request, url_name, ck_univ_url=True, ck_anon=True)
        univ = get_object_or_404(Univ, url_name=url_name)
        form = PostForm(request.POST or None, request=request)
        if request.method == 'POST':
            if form.is_valid():
                post = form.save(commit=False)
                if post.ctgy.is_anonymous:
                    post.is_anonymous = True
                post.save()
                for image in request.FILES.getlist('images'):
                    Image.objects.create(post=post, image=image)
                return redirect(
                    reverse('core:board:main_board', args=[url_name]) +
                    '?state=new')
        return render(request, 'board/post_new.html', {
            'form': form,
            'univ': univ,
            'url_name': url_name,
        })

    except Exception as e:
        if str(e) == 'anon':
            return redirect_with_next("core:accounts:login",
                                      "core:board:main_board",
                                      params={
                                          "to": [url_name],
                                          "next": [url_name]
                                      })
        if str(e) == 'others':
            return redirect("core:board:main_board",
                            url_name=request.user.univ.url_name)

        # return HttpResponseBadRequest(content="Bad request: " + str(e))
        raise SuspiciousOperation
Exemplo n.º 8
0
def edit_post(request, pk):
    post = Post.objects.get(pk=pk)

    if post.approved or request.user != post.author:
        return redirect('/board/')

    tags = Tag.objects.all()
    role = get_role(request.user)
    posttags = post.tags.split(",")
    menu_type = 'general'

    form = PostForm(instance=post)
    if request.method == 'POST':
        form = PostForm(instance=post, data=request.POST, files=request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            if role == "coordinator" or role == "admin":
                post.status = 1
            else:
                post.status = 3
            post.save()

            short_title = (post.title[:25] +
                           '..') if len(post.title) > 25 else post.title
            details = (post.content[:100] +
                       '..') if len(post.content) > 100 else post.content
            description = u'Board updated - {}'.format(short_title)

            notification = create_notification_from(post)
            notification.type = 4
            notification.creator = request.user
            notification.description = description
            notification.details = details
            notification.save()

            coordinators_ids = (ConsultantProfile.objects.prefetch_related(
                'consultant').get_coordinator_by_market(
                    post.market).is_active().values_list('consultant__id',
                                                         flat=True))

            bulk_receiver_creation(notification, coordinators_ids)

            if role == "coordinator" or role == "admin":
                messages.success(request, 'Post successfully edited.')
            else:
                messages.success(
                    request,
                    'Post successfully edited. An administrator will review it shortly.'
                )

            return redirect('board:board')
        else:
            messages.error(
                request,
                'There was a problem editing the post. Please correct the errors and try again.'
            )

    edit = True

    return render(request, 'board/create.html', locals())
Exemplo n.º 9
0
def reply_topic(request, pk, topic_pk):
    topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.topic = topic
            post.created_by = request.user
            post.save()

            topic.last_updated = timezone.now()
            topic.save()

            topic_url = reverse('topic_posts',
                                kwargs={
                                    'pk': pk,
                                    'topic_pk': topic_pk
                                })
            topic_post_url = '{url}?page={page}#{id}'.format(
                url=topic_url, id=post.pk, page=topic.get_page_count())

            return redirect(topic_post_url)
    else:
        form = PostForm()
    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 10
0
def new_post(request, board_slug):
    board = Board.objects.get(slug=board_slug)
    if request.method == 'POST':
        post_form = PostForm(request.POST)
        attachment_form = AttachmentForm(request.POST, request.FILES)
        if post_form.is_valid() and attachment_form.is_valid(
        ) and request.user.is_authenticated:
            post = post_form.save(commit=False)
            post.board = board
            post.ip = get_ip(request)
            try:
                post.account = request.user
            except Post.DoesNotExist:
                post.account = None
            post.save()
            attachment = attachment_form.save(commit=False)
            attachment.post = post
            attachment.save()
            return redirect(board)
    else:
        post_form = PostForm()
        attachment_form = AttachmentForm()

    return render(request, 'new_post.html', {
        'board': board,
        'post_form': post_form,
        'attachment_form': attachment_form
    })
Exemplo n.º 11
0
def post_edit(request, category_name, pk):
    current_user = UserProfile.objects.get(user=request.user)
    current_post = Post.objects.get(id=pk)

    try:
        cat = Category.objects.get(name=category_name)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            if cat:
                post = form.save(commit=False)
                current_post.title = post.title
                current_post.content = post.content
                current_post.save()
                return HttpResponseRedirect('/donkey/category/{}/'.format(cat))
        else:
            print(form.errors)
    else:
        form = PostForm()

    context_dict = {'form': form, 'category': cat, 'post': current_post}

    return render(request, 'donkey/edit_post.html', context_dict)
Exemplo n.º 12
0
def new_post(request):

    if not request.user.is_authenticated:
        return redirect('index')
    form = PostForm()
    my_user = request.user.myuser
    context = dict(
        current_user = my_user,
        username = request.user.username,
        form = form
    )

    return render(request, 'new_post.html',context)
Exemplo n.º 13
0
def new_post(request, board_slug):
    board = Board.objects.get(slug=board_slug)
    if request.method == 'POST':
        post_form = PostForm(request.POST)
        attachment_form = AttachmentForm(request.POST, request.FILES)
        if post_form.is_valid() and attachment_form.is_valid() and request.user.is_authenticated:
            post = post_form.save(commit=False)
            post.board = board
            post.ip = get_ip(request)
            try:
                post.account = request.user
            except Post.DoesNotExist:
                post.account = None
            post.save()
            attachment = attachment_form.save(commit=False)
            attachment.post = post
            attachment.save()
            return redirect(board)
    else:
        post_form = PostForm()
        attachment_form = AttachmentForm()

    return render(request, 'new_post.html', {'board': board, 'post_form': post_form, 'attachment_form': attachment_form})
Exemplo n.º 14
0
def create_post(request):
    role = get_role(request.user)

    tags = Tag.objects.all()
    form = PostForm()
    if request.method == 'POST':
        form = PostForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            market = get_user_market_or_main_market(request.user)

            post = form.save(commit=False)
            post.author = request.user
            post.market = market
            if role == "coordinator" or role == "admin":
                post.status = 1

            post.save()

            short_title = (post.title[:25] +
                           '..') if len(post.title) > 25 else post.title
            details = (post.content[:100] +
                       '..') if len(post.content) > 100 else post.content
            description = u'New board created - {}'.format(short_title)

            notification = create_notification_from(post)
            notification.type = 3
            notification.creator = request.user
            notification.description = description
            notification.details = details
            notification.save()

            coordinators_ids = (
                ConsultantProfile.objects.prefetch_related('consultant').
                get_coordinator_by_market(market).is_active().values_list(
                    'consultant__id', flat=True))

            bulk_receiver_creation(notification, coordinators_ids)

            if role == "coordinator" or role == "admin":
                messages.success(request, 'Post successfully created.')
            else:
                messages.success(
                    request,
                    'Post successfully created. An administrator will review it shortly.'
                )
            return redirect('/board/')
        else:
            messages.error(
                request,
                'There was a problem creating the post. Please correct the errors and try again.'
            )

    if role == 'corporate':
        menu_type = 'general'

    return render(request, 'board/create.html', locals())
Exemplo n.º 15
0
    def post(self, req):
        """
        Adds the post to the database and return a response correspondingly.

        Either message/photo must exist for the POST request to be valid.
        This will be added to the database along with a created_at timestamp.
        
        The user will post form data with the following attached:
        Fields:
            name -> string: the author of the post, optional.
            message -> string: the message the user wish to convey.
            photo -> Images: the photos the user uploads.
        Returns:
            A response of either status `204` for success or `422` for invalid data.
        """
        form = PostForm(req.POST, req.FILES)

        if (form.is_valid()):
            #TODO: save form data to database

            # 204 is an empty response with no content, meaning that the operation was a success
            return HttpResponse(status=204)
        # 422 meaning the data is valid but does not match business model
        return HttpResponse(status=422)
Exemplo n.º 16
0
def notice_create_view(request, pk):
    building = Building.objects.get(pk=pk)
    template_name = 'buildinginfo/notice_form.html'
    board_name = 'notice'
    postForm = PostForm()
    postForm.fields["building"].initial = building
    ImageFormSet = modelformset_factory(Images, form=ImageForm, extra=3)
    # 'extra' means the number of photos that you can upload   ^
    formset = ImageFormSet(queryset=Images.objects.none())

    context = {}
    context['post_form'] = postForm
    context['formset'] = formset
    context['action'] = reverse('board:post_create') + "?board=" + board_name
    context['building_object'] = building
    return render(request, template_name, context)
Exemplo n.º 17
0
def detail_post(request, pk):
    if request.method == 'GET':

        post = Post.objects.get(pk=pk)
        tags = post.tags.split(",")

        form = PostForm()

        context = {
            'form': form,
            'tags': [],
            'post': post,
            'protocol': 'http',
        }

        return render(request, 'board/detail.html', locals())
Exemplo n.º 18
0
def new_post(request):
    if request.method == "POST":
        form = PostForm(request.POST)

        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.generate()
            return redirect('board:post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'board/form.html', {'form': form})
Exemplo n.º 19
0
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if post.author == User.objects.get(username=request.user.get_username()):
        if request.method == "POST":
            form = PostForm(request.POST, instance=post)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.regdate = timezone.now()
                post.generate()
                return redirect('board:post_detail', pk=post.pk)
        else:
            form = PostForm(instance=post)
        return render(request, 'board/form.html', {'form': form})
    else:
        return render(request, 'board/warning.html')
Exemplo n.º 20
0
def board_create(request):
    print('board_create')
    chat = Chat.objects.all()
    user_name = request.session['userss']
    user_id = request.session['user_id']

    companyNamedict = GetStockCode.Get_CSV_Maching_dict()
    companyName = request.POST.get('machingstock')
    print(companyName)
    xmlDict = GetStockCode.Get_XML_maching_dict()
    match_code = ''


    if companyName is not None:
        match_code = xmlDict[companyName]
        print(match_code)


    if request.method == 'POST':
        post_form = PostForm(request.POST)


        print('포스트로 들오온')
        print(post_form.errors)

        if post_form.is_valid():
            print('폼이상')
            if match_code != '':
                print(post_form)
                # commit=False : db에 넣지말고 객체를 가져와라->post에 넣어줌
                post = post_form.save(commit=False)
                post.maching_code = match_code
                post.stock_name = companyName
                post.author = user_name
                post.save()
            #post_form.save()
            return redirect('board:bbs_main')

    else:
        post_form = PostForm()
        print('get으로 들어온다 ')

    return render(request, 'bbs_create.html', {'post_form': post_form, 'companydict': companyNamedict, 'userss': user_name, 'user_id': user_id, 'chat': chat })
Exemplo n.º 21
0
 def test_post_form(self):
     form = PostForm()
     self.assertIn('<select name="user" required id="id_user">',
                   form.as_p())
Exemplo n.º 22
0
 def get(self, request, *args, **kwargs):
     self.object = self.get_object()
     form = PostForm(obj=self.object, initial={'user': request.user})
     context = self.get_context_data(object=self.object, form=form)
     return self.render_to_response(context)
Exemplo n.º 23
0
def edit_post(request, post_id):
    origin_post = Post.objects.get(id=post_id)
    edited_post = Post.objects.get(id=post_id)
    try:
        origin_attachment = Attachment.objects.get(post=origin_post)
        origin_attachment_name = origin_attachment.attachment.name
    except Attachment.DoesNotExist:
        origin_attachment = None
        origin_attachment_name = ''

    if request.method == 'POST':
        post_form = PostForm(request.POST, instance=edited_post)
        attachment_form = AttachmentForm(request.POST, request.FILES)
        if origin_post.title != request.POST['title'] or origin_post.content != request.POST['content'] or \
                origin_attachment_name != request.FILES.get('attachment', ''):
            if post_form.is_valid() and attachment_form.is_valid():
                edited_post.save()

                edited_post_history = EditedPostHistory.objects.create(
                    post=origin_post,
                    title=origin_post.title,
                    content=origin_post.content,
                    ip=origin_post.ip)
                edited_post_history.save()

                # add attachment
                if not origin_attachment and request.FILES.get(
                        'attachment', '') != '':
                    new_attachment = attachment_form.save(commit=False)
                    new_attachment.post = edited_post
                    new_attachment.save()

                # modify attachment
                elif origin_attachment and request.FILES.get('attachment', '') != '' \
                        and origin_attachment_name != request.FILES.get('attachment', ''):
                    origin_attachment.post = None
                    origin_attachment.editedPostHistory = edited_post_history
                    origin_attachment.save()

                    new_attachment = attachment_form.save(commit=False)
                    new_attachment.post = edited_post
                    new_attachment.save()

                # remove attachment
                elif request.POST.get('attachment-clear', '') == 'on':
                    origin_attachment.post = None
                    origin_attachment.editedPostHistory = edited_post_history
                    origin_attachment.save()

                return redirect(edited_post)
        else:
            error_message = "변경 사항이 없습니다"
            return render(
                request, 'edit_post.html', {
                    'post': origin_post,
                    'post_form': post_form,
                    'attachment_form': attachment_form,
                    'error_alert': error_message
                })
    else:
        post_form = PostForm(initial={
            'title': origin_post.title,
            'content': origin_post.content
        })
        if origin_attachment:
            attachment_form = AttachmentForm(
                initial={'attachment': origin_attachment.attachment})
        else:
            attachment_form = AttachmentForm()

    return render(
        request, 'edit_post.html', {
            'post': origin_post,
            'post_form': post_form,
            'attachment_form': attachment_form
        })
Exemplo n.º 24
0
def edit_post(request, post_id):
    origin_post = Post.objects.get(id=post_id)
    edited_post = Post.objects.get(id=post_id)
    try:
        origin_attachment = Attachment.objects.get(post=origin_post)
        origin_attachment_name = origin_attachment.attachment.name
    except Attachment.DoesNotExist:
        origin_attachment = None
        origin_attachment_name = ''

    if request.method == 'POST':
        post_form = PostForm(request.POST, instance=edited_post)
        attachment_form = AttachmentForm(request.POST, request.FILES)
        if origin_post.title != request.POST['title'] or origin_post.content != request.POST['content'] or \
                origin_attachment_name != request.FILES.get('attachment', ''):
            if post_form.is_valid() and attachment_form.is_valid():
                edited_post.save()

                edited_post_history = EditedPostHistory.objects.create(
                    post=origin_post,
                    title=origin_post.title,
                    content=origin_post.content,
                    ip=origin_post.ip
                )
                edited_post_history.save()

                # add attachment
                if not origin_attachment and request.FILES.get('attachment', '') != '':
                    new_attachment = attachment_form.save(commit=False)
                    new_attachment.post = edited_post
                    new_attachment.save()

                # modify attachment
                elif origin_attachment and request.FILES.get('attachment', '') != '' \
                        and origin_attachment_name != request.FILES.get('attachment', ''):
                    origin_attachment.post = None
                    origin_attachment.editedPostHistory = edited_post_history
                    origin_attachment.save()

                    new_attachment = attachment_form.save(commit=False)
                    new_attachment.post = edited_post
                    new_attachment.save()

                # remove attachment
                elif request.POST.get('attachment-clear', '') == 'on':
                    origin_attachment.post = None
                    origin_attachment.editedPostHistory = edited_post_history
                    origin_attachment.save()

                return redirect(edited_post)
        else:
            error_message = "변경 사항이 없습니다"
            return render(request, 'edit_post.html', {
                'post': origin_post,
                'post_form': post_form,
                'attachment_form': attachment_form,
                'error_alert': error_message
            })
    else:
        post_form = PostForm(initial={'title': origin_post.title, 'content': origin_post.content})
        if origin_attachment:
            attachment_form = AttachmentForm(initial={'attachment': origin_attachment.attachment})
        else:
            attachment_form = AttachmentForm()

    return render(request, 'edit_post.html', {'post': origin_post, 'post_form': post_form, 'attachment_form': attachment_form})