def search(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): name = form.cleaned_data['username'] print(name) if not Usernames.objects.filter(username=name).exists(): u = Usernames(username=name) u.save() utilsy.create_directory(name) print('a') main_task = socialpath_main.delay(name=name) request.session['task_id'] = main_task.task_id return HttpResponseRedirect("/social") # return render(request, 'social_index.html', context={'task_id': main_task.task_id}) else: messages.warning(request, 'User already exists in database.') # <- return render(request, 'search.html', {'form': form}) else: messages.error(request, "Error") else: form = PostForm() return render(request, 'search.html', {'form': form})
def post(request): data = {} if request.method == 'POST': url = request.POST.get('url', None) form = PostForm(request.POST, request.FILES) if form.is_valid(): # create or update post = Post() if url is None else Post.objects.get(url=url) post.title = request.POST['title'] post.content = request.POST['content'] post.author = request.user # add image if uploaded featured_image = request.FILES.get('featured_image', False) if featured_image: post.featured_image = featured_image post.save() data['errors'] = False data['next'] = post.url # redirect url else: data['errors'] = form.errors return JsonResponse(data) # GET request return HttpResponseRedirect("/home")
def update_post(request, post_id): post_id = int(post_id) try: post_up = Post.objects.get(id=post_id) except Post.DoesNotExist: return redirect('social:home') post_form = PostForm(request.POST or None, instance=post_up) if post_form.is_valid(): post_form.save() return redirect('social:home') return render(request, 'social/home.html', {'post_form': post_form})
def post(self, request, format=None): form = PostForm(request.POST, request.FILES) if form.is_valid(): post = PostModel() post.title = request.POST.get('title') post.content = request.POST.get('content') post.author = request.user # add image if uploaded featured_image = request.FILES.get('featured_image', False) if featured_image: post.featured_image = featured_image post.save() serializer = PostSerializer(post) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)
def search(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] if not Users.objects.filter(name=name).exists(): u = Users(name=name) u.save() utilsy.create_directory(name) keys = utilsy.get_keys() # subprocess.Popen(['python', 'manage.py', 'process_tasks'], stdout=subprocess.PIPE, # stderr=subprocess.PIPE) # twitter_task(name, keys['keys']['twitter']['TWITTER_ACCESS_TOKEN'],keys['keys']['twitter']['TWITTER_ACCESS_TOKEN_SECRET'], keys['keys']['twitter']['TWITTER_CONSUMER_KEY'], keys['keys']['twitter']['TWITTER_CONSUMER_SECRET']) # subprocess.Popen(['python', 'manage.py', 'process_tasks'], stdout=subprocess.PIPE, # stderr=subprocess.PIPE) # subprocess.Popen(['python', 'manage.py', 'process_tasks'], stdout=subprocess.PIPE, # stderr=subprocess.PIPE) reddit_task(name, keys['keys']['reddit']['CLIENT_ID'], keys['keys']['reddit']['CLIENT_SECRET'],keys['keys']['reddit']['PASSWORD'], keys['keys']['reddit']['USER_AGENT'], keys['keys']['reddit']['USERNAME']) stackoverflow_task(name) # subprocess.Popen(['python', 'manage.py', 'process_tasks'], stdout=subprocess.PIPE, # stderr=subprocess.PIPE) facebook_task(name) instag(name, keys['keys']['instagram']['instagram_cookie']) # subprocess.Popen(['python3', 'manage.py', 'process_tasks'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) return redirect('users') else: messages.warning(request, 'User already exists in the database.') # <- return render(request, 'search.html', {'form': form}) # raise form.ValidationError('Looks like a username with that email or password already exists') else: messages.error(request, "Error") else: form = PostForm() return render(request, 'search.html', {'form': form})
def get(self, request, *args, **kwargs): if request.user.is_authenticated: context = self.get_default_context(request) context["form"] = PostForm() return render(request, 'social/post_list.html', context) else: return render(request, 'landing/index.html')
def showForm(request): response = "" # show a single form for all the media if request.method == 'POST': post = PostForm(request.POST, request.FILES) if post.is_valid(): postName = post.cleaned_data['postName'] imageUrl = post.cleaned_data['imageUrl'] postCaption = post.cleaned_data['postCaption'] postSource = post.cleaned_data['postSource'] lastPost = postData.objects.order_by('postId').last() if lastPost: lastPostId = lastPost.postId else: lastPostId = 0 # select an image if present #if imageUrl == "" and "imageFile" in request.FILES: # image = settings.MEDIA_ROOT + "" +request.FILES['imageFile'].name # imageUrl = "http://"+request.get_host()+settings.MEDIA_URL+request.FILES['imageFile'].name # with open(image, 'wb') as destination: # for chunk in request.FILES['imageFile'].chunks(): # destination.write(chunk) for source in postSource: response += postUpdate(source, postName, imageUrl, postCaption, lastPostId + 1) #postUpdate("li", postName, imageUrl, postCaption) return HttpResponse(response) else: # error occured in validation form = PostForm() return render(request, 'postForm.html', { 'form': form, 'msg': post.errors }) else: form = PostForm() return render(request, 'postForm.html', {'form': form})
def get(self, request): form = PostForm() posts = Post.objects.all().order_by('-created') users = User.objects.exclude(id=request.user.id) user = request.user try: friend = Friend.objects.get(current_user=user) friends = friend.users.all() except Friend.DoesNotExist: friends = None args = {'form': form, 'posts': posts, 'users': users, 'friends': friends, 'user': user} return render(request, self.template_name, args)
def post(self, request): form = PostForm(data=request.POST) if form.is_valid(): post = form.save(commit=False) post.user = request.user if 'post_image' in request.FILES: post.post_image = request.FILES['post_image'] post.save() form = PostForm() return redirect('social:home') return render(request, self.template_name, {'form': form})