def create_post(request):
    print request.FILES.get('file')
    print type(request.FILES.get('file'))
    image = request.FILES.get('file')

    content = request.POST.get('post-input')
    published = datetime.now()
    is_markdown = json.loads(request.POST.get('is-markdown-post'))
    if is_markdown:
        contentType = "text/x-markdown"
        content = CommonMark.commonmark(content)
    else:
        contentType = "text/plain"



    

    visibility = request.POST.get('visibility')
    c_username = request.user.username
    user_object = User.objects.get(username=c_username)
    author_object = Author.objects.get(email=user_object)
    author_name = author_object.displayName

    post_id = uuid.uuid4()
    DITTO_HOST = 'http://' + request.get_host() + '/api/posts/' + str(post_id)
    title = request.POST.get('title')
    description = request.POST.get('description')

    categories = request.POST.get('categories')

    c = categories.split(' ')

    categories_json = json.dumps(c)

    new_post = Post(published=published, author=author_object, content=content, contentType=contentType,
                    visibility=visibility, source=DITTO_HOST, origin=DITTO_HOST, categories=categories, title=title,
                    description=description, id = post_id)
    new_post.save()
    if image:
        print image.content_type
        #image.name = str(uuid.uuid4())
        print image.name
        print "before creating"
        new_image = Img(actual_image = image)
        new_image.parent_post = new_post

        print "before saving"
        new_image.save()
        print "after saving"

        new_post.content = new_post.content + ' <br>   <img src="http://ditto-test.herokuapp.com/ditto/media/images/'+image.name+'" >'
        #new_post.content = new_post.content + ' <br>   <img src="http://localhost:8000/ditto/media/images/'+image.name+'" >'
        new_post.save()

    return HttpResponse(request.POST.get('post_body'))
Esempio n. 2
0
def upload_img(request):
    if not request.FILES:
        return render(
            request, 'feed/index.html', {
                'error_message': "You Should Choose A File Before Upload",
                'posts': Post.objects.filter(share=True).order_by('-pub_date'),
            })
    if request.method == 'POST':
        img = request.FILES['photo']
        post = Post(name=img.name, photo=img)
        post.save()
        request.session['photo_pk'] = post.pk
        context = {'post': post, 'crop_form': Crop_Form}
        return render(request, 'peditor/editor.html', context=context)
Esempio n. 3
0
def create_post(request):
    if request.method == 'POST':
        post_text = request.POST.get('post_text')

        post_author = SocialUser.objects.get(user=request.user)
        post = Post(author=post_author, content=post_text)
        post.save()

        message = 'Create post successful!'
        post_id = post.id
        content = post.content
        date_created = post.date_created_formatted
        date_edited = post.date_edited
        author = post.author.user.get_full_name()

        return JsonResponse({'message': message, 'post_id': post_id, 'content': content, 'date_created': date_created,
                             'date_edited': date_edited, 'author': author})
    else:
        return JsonResponse({'message': "You didn't send with POST method, better luck next time!"})
Esempio n. 4
0
            admin_posts = json.load(f)
            for p in admin_posts:
                if not User.objects.filter(username=p['author']).exists():
                    User.objects.create_user(username=p['author'],
                                             password='******')
                    # Put user in list
                    for i in friends['ends'][::-1]:
                        if user[i] is None:
                            user[i] = p['author']
                            break
                post = Post()
                post.author = User.objects.get(username=p['author'])
                post.text = p['text']
                if 'isPublic' in p:
                    post.isPublic = p['isPublic']
                post.save()

        print('Make random users and their random posts')
        with open('private/random_users.json') as f_u, open('private/random_posts.json', encoding='utf-8') as f_posts, \
                open('private/passwords.json') as f_pass:
            users = json.load(f_u)
            posts = json.load(f_posts)
            passwords = json.load(f_pass)
            for u in users:
                # Put user in list and setup password
                for i in range(100):
                    if user[i] is None:
                        user[i] = u
                        if i in friends['ends']:
                            User.objects.create_user(username=u, password='******')
                        else: