Ejemplo n.º 1
0
def postcreate(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('index')
    form = PostForm()
    return render(request, 'create.html', {'form': form})
Ejemplo n.º 2
0
def postedit(request, pk, template_name='edit.html'):
    post = get_object_or_404(Post, pk=pk)
    form = PostForm(request.POST or None, instance=post)

    if form.is_valid():
        form.save()
        return redirect('index')
    return render(request, template_name, {'form': form})
Ejemplo n.º 3
0
def post_form_view(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            form.save()
            title = form.cleaned_data['title']
            return HttpResponse('%s successfully  saved!' % title)
    else:
        form = PostForm()
        context = {'form': form, 'title': 'Post Form'}
    return render(request, 'base.html', context)
Ejemplo n.º 4
0
def post_create(request):
    title = 'Create'
    form = PostForm(request.POST or None, request.FILES or None)
    author = get_author(request.user)
    if request.method == 'POST':
        if form.is_valid():
            form.instance.author = author
            form.save()
            return redirect(
                reverse("post-detail", kwargs={'pk': form.instance.pk}))
    context = {'title': title, "form": form}
    return render(request, "post_create.html", context)
Ejemplo n.º 5
0
	def test_form_creates_model(self):
		Syntax.objects.create(syntax_name="syntax_name1")
		post_obj_data = {
			u'title': u'post_1', 
			u'code': u'code_1', 
			u'ttl_option': u'minutes=10',
			u'syntax': "1"
		}
		post_form = PostForm(data=post_obj_data)
		post_form.save()
		post_obj = Post.objects.get(title=post_obj_data['title'])

		self.assertTrue(isinstance(post_obj, Post))
		self.assertEqual(post_obj.title, post_obj_data['title'])
Ejemplo n.º 6
0
def index(request):
    if request.method == "POST":
        post_pk = request.POST.get("post_id", 0)
        post_obj = Post.objects.filter(pk=post_pk).first()
        form = PostForm(request.POST, instance=post_obj)
        if form.is_valid():
            post = form.save()
            return HttpResponseRedirect(
                reverse('post-view', args=(post.slug, )))
        else:
            form.add_error(
                None,
                forms.ValidationError(
                    _(u"Кожне поле повинно бути заповненим!")))

    elif request.method == "GET":
        post_pk = request.GET.get("post_id", 0)
        post_obj = Post.objects.filter(pk=post_pk).first()

        form = PostForm(instance=post_obj)

    context = {
        "latest_posts": Post.objects.all().order_by('-created')[:5],
        "form": form,
        "post_on_edit": (post_pk if request.GET.get("mark") else 0)
    }
    return render(request, "app/index.html", context)
Ejemplo n.º 7
0
 def get(self, request, *args, **kwargs):
     show_followed = 'all'
     if 'index' in request.session:
         show_followed = request.session['index']
     if show_followed == 'followed':
         # posts = Post.objects.all()
         posts = request.user.followed_posts()
     else:
         posts = Post.objects.all()
     if request.method == 'POST':
         form = PostForm(request.POST)
         if form.is_valid():
             post = form.save(commit=False)
             post.author = request.user
             post.timestamp = datetime.utcnow()
             post.save()
             return redirect('index')
     else:
         form = PostForm()
     context = {
         'posts': posts,
         'form': form,
         'show_followed': show_followed,
     }
     return render(request, "index.html", context)
Ejemplo n.º 8
0
def thread(request, thread_id):
    ''' A view for rendering the threads '''
    thread_id = int(thread_id)
    try:
        current_thread = Thread.objects.get(id=thread_id)
    except Thread.DoesNotExist:
        return HttpResponse("404 Thread does not exist")
    context_dict = {
        'thread': current_thread,
        'thread_report_form': ThreadReportForm(),
        'post_report_form': PostReportForm()
    }
    if request.method == 'POST':
        post_form = PostForm(request.POST)
        if post_form.is_valid():
            post = post_form.save(commit=False)
            post.thread = current_thread
            post.poster = request.META['REMOTE_ADDR']
            post.save()
            current_thread.last_post_time = post.time_posted
            current_thread.post_count += 1
            current_thread.save()
            print(str(current_thread.last_post_time))
        else:
            print(post_form.errors)
    context_dict['post_form'] = PostForm()
    context_dict['posts'] = Post.objects.filter(thread=current_thread)
    context_dict['num_posts'] = len(context_dict['posts'])
    return render(request, 'app/thread.html', context_dict)
Ejemplo n.º 9
0
def edit_post(request, post_id):
    instance = get_object_or_404(Post, id=post_id)
    post_form = PostForm(request.POST or None,
                         request.FILES or None,
                         instance=instance)

    if request.method == "POST" and instance.creator.user.username == request.user.username:
        files = request.FILES.getlist('images')

        if post_form.is_valid():
            instance = post_form.save(commit=False)
            instance.save()

            for f in files:
                post_image = PostImage(image=f)
                post_image.save()
                instance.images.add(post_image)

            return redirect('{}/{}'.format(reverse('profile'), '1'))

    context = {
        'post_form': post_form,
        'post_id': post_id,
    }
    return render(request, 'edit_post.html', context)
Ejemplo n.º 10
0
def thread(request, thread_id):
	''' A view for rendering the threads '''
	thread_id = int(thread_id)
	try:
		current_thread = Thread.objects.get(id=thread_id)
	except Thread.DoesNotExist:
		return HttpResponse("404 Thread does not exist")
	context_dict = {
		'thread': current_thread,
		'thread_report_form': ThreadReportForm(),
		'post_report_form': PostReportForm()
	}
	if request.method == 'POST':
		post_form = PostForm(request.POST)
		if post_form.is_valid():
			post = post_form.save(commit = False)
			post.thread = current_thread
			post.poster = request.META['REMOTE_ADDR']
			post.save()
			current_thread.last_post_time = post.time_posted
			current_thread.post_count += 1
			current_thread.save()
			print(str(current_thread.last_post_time))
		else:
			print(post_form.errors)
	context_dict['post_form'] = PostForm()
	context_dict['posts'] = Post.objects.filter(thread = current_thread)
	context_dict['num_posts'] = len(context_dict['posts'])
	return render(request, 'app/thread.html', context_dict)
Ejemplo n.º 11
0
def thread_detail(request, thread_id):
    thread = get_object_or_404(Thread, id=thread_id)

    if request.method == 'POST':
        # Everyone logged in reply in a thread
        if not request.user.is_authenticated:
            return login_required()

        form = PostForm(request.POST)

        if form.is_valid():
            post = form.save(commit=False)
            post.thread = thread
            post.author = request.user
            post.save()

            return HttpResponseRedirect(
                reverse('thread_detail', args=[thread_id]))

    else:  # GET
        form = PostForm()

    breadcrumbs = _category_breadcrumb(thread.category, last_active=True)

    return render(request, 'app/thread/detail.html', {
        'thread': thread,
        'reply_form': form,
        'breadcrumbs': breadcrumbs
    })
Ejemplo n.º 12
0
def index(request):  
    if request.method == "POST":
        post_pk = request.POST.get("post_id", 0)
        post_obj = Post.objects.filter(pk=post_pk).first()
        form = PostForm(request.POST, instance=post_obj)   
        if form.is_valid():
            post = form.save()
            return HttpResponseRedirect(reverse('post-view', 
                                                args=(post.slug,)))
        else:  
            form.add_error(None, forms.ValidationError(
                                _(u"Кожне поле повинно бути заповненим!")))

    elif request.method == "GET":
        post_pk = request.GET.get("post_id", 0)
        post_obj = Post.objects.filter(pk=post_pk).first()
        
        form = PostForm(instance=post_obj)

    context = {
        "latest_posts": Post.objects.all().order_by('-created')[:5],
        "form": form,
        "post_on_edit": (post_pk 
                         if request.GET.get("mark") 
                         else 0)
    }
    return render(request, "app/index.html", context)
Ejemplo n.º 13
0
def post_form_view(request):
    form = PostForm()
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
            #title = form.cleaned_data['title']

            print(form.cleaned_data['title'])
            print(form.cleaned_data['description'])
            #print(form.cleaned_data['wiki'])

            #return HttpResponse('%s successfully  saved!' % title)
            return HttpResponse('successfully  saved!')
    else:
        context = {'form': form, 'title': 'Post Form'}
    return render(request, 'custom_form.html', context)
Ejemplo n.º 14
0
def home_view(request):
    if request.method == 'GET':
        post_form = PostForm()
    elif request.method == 'POST':
        post_form = PostForm(request.POST)
        if post_form.is_valid():
            obj = post_form.save(commit=False)
            obj.user_fk = request.user
            obj.save()
    return render(request, 'app/home.html', context={'post_form': post_form})
Ejemplo n.º 15
0
def post(request):
    current_user = request.user
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.user = current_user
            post.save()

    posts = Post.objects.filter(user=request.user.id)

    return render(request, 'pers_profile.html', {"form": form, "posts": posts})
Ejemplo n.º 16
0
def submit_post(request):
    """Create a form to submit post.
    """
    context = RequestContext(request)

    if request.POST:
        postform = PostForm(data=request.POST)
        if postform.is_valid():
            postform.save(commit=True)
            return HttpResponseRedirect('/app/blog')
        else:
            print postform.errors
    else:
        postform = PostForm()
        print postform

    context_dict = {
        'postform': postform,
    }

    return render_to_response("app/submitpost.html", context_dict, context)
Ejemplo n.º 17
0
def post_edit(request, pk):
	post = get_object_or_404(Post, pk=pk)
	if request.method == "POST":
		form = PostForm(request.POST, instance=post)
		if form.is_valid():
			post = form.save(commit=False)
			post.author = request.user
			post.save()
			return redirect('app.views.post_detail', pk=post.pk)
	else:
		form = PostForm(instance=post)
	return render(request, 'app/post_edit.html', {'form': form})
Ejemplo n.º 18
0
def post_form_view(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save()
            messages.success(request, '%s successfully saved.' % post.title)
            return redirect('test_markdownify')
    else:
        form = PostForm()
        context = {'form': form, 'title': 'Post Form'}
    theme = getattr(settings, 'MARTOR_THEME', 'bootstrap')
    return render(request, '%s/form.html' % theme, context)
Ejemplo n.º 19
0
def post_form_view(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save()
            messages.success(request, "%s successfully saved." % post.title)
            return redirect("test_markdownify")
    else:
        form = PostForm()
        context = {"form": form, "title": "Post Form"}
    theme = getattr(settings, "MARTOR_THEME", "bootstrap")
    return render(request, "%s/form.html" % theme, context)
Ejemplo n.º 20
0
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('index')
    else:
        form = PostForm()
    return render(request, 'post_edit.html', {'form': form})
Ejemplo n.º 21
0
def dbPost(request):
    args = {}
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save( commit = False)
            post.feature_id = request.POST.get("feature_id", "")
            specId = request.POST.get("species", "")            
            citeId = request.POST.get("citation", "")            
            post.username = request.POST.get("username", "")
            post.dt = request.POST.get("dt", "")            
            post.species_id = Species.objects.get(species_id_html = specId)
            post.traits = request.POST.get("traits", "")            
            if request.POST.get("mean","") == "":
                post.mean = None
            else:
                post.mean = float(request.POST.get("mean",""))
            
            if request.POST.get("range0","") == "":
                r1 = None
            else: 
                r1 = float(request.POST.get("range0",""))
            if request.POST.get("range1","") == "":
                r2 = None
            else:
                r2 = float(request.POST.get("range1",""))
                                            
            post.range = [r1,r2]
            
            
            post.uncertainty = request.POST.get("uncertainty","")
            post.units = request.POST.get("units","")
            post.cite = Citation.objects.get(citation_name = citeId)
            
            post.save()

            cnt = CitationNumerictraitSpecies()
            cnt.relation_id = CitationNumerictraitSpecies.objects.all().order_by('-relation_id').values_list()[0][0] + 1
            cnt.feature = NumericTraits.objects.get(feature_id = request.POST.get("feature_id", ""))
            cnt.cite = Citation.objects.get(citation_name = citeId)
            cnt.species = Species.objects.get(species_id_html = specId)
            cnt.save()
        
            return HttpResponse('')        
        else:
            print form.errors                       
           
    else:
        form = PostFormOther()
    
        args['form'] = form
        return render(request, 'app/lifehistory.html', args)
Ejemplo n.º 22
0
def post_new(request):
	if request.method == "POST":
		form = PostForm(request.POST)
		if form.is_valid():
			post = form.save(commit=False)
			post.author = request.user
			if 'picture' in request.FILES:
				profile.picture = request.FILES['picture']
			post.save()
			return redirect('app.views.post_detail', pk=post.pk)
	else:
		form = PostForm()
	return render(request, 'app/post_edit.html', {'form': form})
Ejemplo n.º 23
0
def post_form(request,pk):

    user = get_object_or_404(User,pk=pk)
    if request.method=='POST':
        form = PostForm(data=request.POST)
        if form.is_valid():
            post_filled_form = form.save(commit=False)
            post_filled_form.user = user
            post_filled_form.save()
            return redirect('app:post_detail',pk=user.pk)
    else:
        form = PostForm()

    return render(request, 'app/post_form.html', {'post_filled_form': form})
Ejemplo n.º 24
0
def post_edit(request, post_id):
    post = get_object_or_404(Post, id=post_id)

    if request.method == 'POST':
        # Only an author can edit their post
        if not request.user.is_authenticated or request.user.id != post.author_id:
            raise PermissionDenied

        form = PostForm(request.POST, instance=post)

        if form.is_valid():
            form.save()

            return HttpResponseRedirect(
                reverse('thread_detail', args=[post.thread_id]))

    else:  # GET
        form = PostForm(instance=post)

    return render(request, 'app/post/edit.html', {
        'post': post,
        'post_form': form
    })
Ejemplo n.º 25
0
 def post(self, request, *args, **kwargs):
     user = User.objects.get(pk=request.session['user']['id'])
     form = PostForm(request.POST)
     if form.is_valid():
         post = form.save(commit=False)
         post.author = user
         post.save()
         return JsonResponse({
             'status': 'success',
         })
     else:
         return JsonResponse({
             'status': 'failed',
         })
Ejemplo n.º 26
0
def thread_create(request, category_id):
    try:
        category = Category.objects.get(id=category_id)
    except Category.DoesNotExist:
        category = None

    if request.method == 'POST':
        # Everyone logged in can create a new thread
        if not request.user.is_authenticated:
            return login_required()

        thread_form = ThreadForm(request.POST)
        post_form = PostForm(request.POST)

        if thread_form.is_valid() and post_form.is_valid():
            with transaction.atomic():
                post = post_form.save(commit=False)
                post.author_id = request.user.id
                post.thread_id = 0
                post.save()

                thread = thread_form.save(commit=False)
                thread.original_post_id = post.id
                thread.save()

                post.thread_id = thread.id
                post.save()

            return HttpResponseRedirect(
                reverse('thread_detail', args=[thread.id]))

    else:
        thread_form = ThreadForm(initial={'category': category})
        post_form = PostForm()

    return render(request, 'app/thread/create.html', {
        'thread_form': thread_form,
        'post_form': post_form
    })
Ejemplo n.º 27
0
def create_post(request):
    post_form = PostForm(request.POST or None, request.FILES or None)

    if request.method == "POST":
        files = request.FILES.getlist('images')

        if post_form.is_valid():
            p = post_form.save(commit=False)
            p.creator = ExtendedUser.objects.get(user_id=request.user.id)
            p.is_shown = True
            p.save()

            for f in files:
                post_image = PostImage(image=f)
                post_image.save()
                p.images.add(post_image)

            return redirect(reverse('index'))

    context = {
        'post_form': post_form,
    }
    return render(request, 'createPost.html', context)