예제 #1
0
def create_view(request):
    if request.method == 'POST':
        if request.user.is_authenticated():
            title = request.POST['title']
            body = request.POST['body']
            if title and body:
                post = Post()
                post.title = title
                post.body = language_filter(body)
                post.pub_date = timezone.datetime.now()
                post.author = request.user
                post.save()
                return JsonResponse(
                    {
                        "status": "success",
                        "message": "post created",
                        "post_id": post.pk
                    },
                    status=201)
            else:
                return JsonResponse(
                    {
                        "status": "failure",
                        "message": "title and post body cannot be empty"
                    },
                    status=406)
        else:
            return JsonResponse(
                {
                    "status": "failure",
                    "message": "user need to login"
                },
                status=403)
    else:
        return render(request, 'posts/create-postd.html')
예제 #2
0
def newpost(request):
    if request.method == 'POST':
        if request.POST['title']:
            if request.POST['body'] or request.FILES:
                post = Post()
                post.title = request.POST['title']

                if request.POST['url']:
                    if request.POST['url'].startswith(
                            'https://') or request.POST['url'].startswith(
                                'http://'):
                        post.url = request.POST['url']
                    else:
                        posts.url = "http://" + request.POST['url']

                if request.POST['body']:
                    post.body = request.POST['body']

                if request.FILES:
                    if request.FILES['image']:
                        post.image = request.FILES['image']

                post.author = request.user

                post.save()
                return redirect('home')
            else:
                return render(
                    request, 'posts/newpost.html',
                    {'error': "Il faut au moins du texte ou une image"})
    else:
        return render(request, 'posts/newpost.html')
예제 #3
0
def parse_posts():
    json_data = get_json(POSTS_URL)

    for item in json_data:
        try:
            post = Post()
            post.id = item['id']
            post.title = item['title']
            post.body = item['body']
            post.user = User.objects.get(pk=item['userId'])
            post.save()
        except ObjectDoesNotExist:
            pass
def import_archive():
    with open("comments.json", "r") as read_file:
        data = json.load(read_file)

    with open("users.json", "r") as read_file:
        data1 = json.load(read_file)

    with open("posts.json", "r") as read_file:
        data2 = json.load(read_file)

    with open("address.json", "r") as read_file:
        data3 = json.load(read_file)

    for i in range(len(data['comments'])):
        comments = Comment()
        comments.body = data['comments'][i]["body"]
        comments.email = data['comments'][i]["email"]
        comments.postId = data['comments'][i]["postId"]
        comments.id = data['comments'][i]["id"]
        comments.name = data['comments'][i]["name"]
        comments.save()

    for i in range(len(data2['posts'])):
        posts = Post()
        posts.body = data2['posts'][i]["body"]
        posts.userId = data2['posts'][i]["userId"]
        posts.title = data2['posts'][i]["title"]
        posts.id = data2['posts'][i]["id"]
        posts.save()

    for i in range(len(data1['users'])):
        users = Profile()
        users.username = data1['users'][i]["username"]
        users.address = data1['address'][i]["id"]
        users.email = data1['users'][i]["email"]

        users.save()

        for post in data2['posts']:
            profile = Profile.objects.get(id=post['userId'])
            Comment.objects.create(id=comment['id'],
                                   name=comment['name'],
                                   email=comment['email'],
                                   body=comment['body'],
                                   post=post)