Exemplo n.º 1
0
def create(request):
    if request.method == 'POST':
        if request.POST["title"] and request.POST['URL'] and request.POST['summary'] and request.POST['body'] and\
           request.FILES['image']:
            post = Post()
            post.title = request.POST["title"]
            if request.POST['URL'].startswith(
                    'http://') or request.POST['URL'].startswith('https://'):
                post.URL = request.POST['URL']
            else:
                post.URL = 'http://' + request.POST['URL']
            post.summary = request.POST['summary']
            post.body = request.POST['body']
            post.image = request.FILES['image']
            post.pub_date = timezone.datetime.now()
            post.total_votes = 0
            post.SentBy = request.user
            post.save()
            return redirect('home')
        else:
            return render(request, 'create.html',
                          {'error': 'You need to fill all areas!'})
    else:
        return render(
            request,
            'create.html',
        )
Exemplo n.º 2
0
def upload():

    if 'username' in session:
        form = UploadForm()
        if request.method == 'POST':
            if form.validate_on_submit():
                # create new record
                new_post = Post()
                new_post.poster = User.objects.get(
                    username=session['username']).id
                new_post.title = form.title.data
                new_post.tags = [x for x in form.tags.data]

                if form.link.data:
                    new_post.link = form.link.data

                if request.files.get('meme'):
                    filename = secure_filename(form.meme.data.filename)
                    hashed_fname = sha256(filename.encode('utf-8')).hexdigest()
                    file_path = os.path.join('upload_folder', hashed_fname)
                    form.meme.data.save(file_path)
                    new_post.image = hashed_fname

                new_post.save()

                return redirect(url_for('general_app.index'))

        return render_template('post/upload.html', form=form)
    else:
        return abort(403)
Exemplo n.º 3
0
def make_post(request, id_):
    if request.method == 'POST':
        title = str(request.POST.get('title', False))
        image = request.FILES.get('image', False)
        video = str(request.POST.get('video', False))
        audio = str(request.POST.get('audio', False))
        description = str(request.POST.get('content', False))
        for_coop = int(request.POST.get('for_cooperative', False))
        count = 0
        attachments = []
        while request.FILES.get('attachment' + str(count), False) is not False:
            attachments.append(
                request.FILES.get('attachment' + str(count), False))
            count = count + 1
        if title and description:
            if id_ != 'new':
                new_post = Post.objects.get(id=id_)
            else:
                new_post = Post()
            new_post.author_id = request.user.id
            new_post.title = title
            new_post.author_status = author_status(request.user.id)
            new_post.date_posted = timezone.datetime.now()
            new_post.content = description
            if image:
                new_post.image = image
            if video:
                new_post.video = video
            if audio:
                new_post.audio = audio
            if for_coop == 1:
                new_post.for_cooperative = True
                member = Member.objects.get(user_id=request.user.id)
                new_post.cooperative_name = member.coop_detail().name
            new_post.save()
            for val in attachments:
                new_attachment = Attachment()
                new_attachment.post = new_post
                new_attachment.file = val
                new_attachment.save()

            return render(request, 'post/make_post.html', {
                'message': 'Your post has been uploaded',
                'status': 'success'
            })
        else:
            return render(request, 'post/make_post.html', {
                'message': 'All fields must be filled',
                'status': 'danger'
            })

    return render(request, 'post/make_post.html')