def modify_comment(request, goal_id, comment_id, subgoal_id=None): if subgoal_id: supgoal_id = goal_id goal_id = subgoal_id goal = get_object_or_404(AbstractGoal, pk=goal_id) comment = get_object_or_404(Comment, pk=comment_id) if request.user.is_authenticated: if (request.user == goal.owner): if request.method == "POST": if request.POST.get("comment_text"): comment.text = request.POST.get("comment_text") comment.save() if subgoal_id: return redirect_goal(supgoal_id, goal_id) else: return redirect_goal(goal_id) else: if subgoal_id: return render(request, 'commentary/modify_subcomment.html', { 'goal': goal, 'comment': comment, 'supgoal_id': supgoal_id }) else: return render(request, 'commentary/modify_comment.html', { 'goal': goal, 'comment': comment }) else: return redirect_home(request.user.username) else: return HttpResponseRedirect("/login")
def upload_img(request, goal_id, subgoal_id=None): if subgoal_id: supgoal_id = goal_id goal_id = subgoal_id goal = get_object_or_404(AbstractGoal, pk=goal_id) if not os.path.exists(join(MEDIA_ROOT, 'Archivo/' + str(goal_id))): os.makedirs(join(MEDIA_ROOT, 'Archivo/' + str(goal_id))) error = "" flag = True if request.user.is_authenticated: try: user = User.objects.get(username=request.user.username) except Exception as e: return HttpResponse("El usuario no existe") if request.method == 'POST': form = forms.ArchivoForm(request.POST, request.FILES) if form.is_valid(): tiene_url = len(form.cleaned_data['url']) > 0 tiene_file = len(request.FILES) > 0 if ((not tiene_url) and (not tiene_file)) or (tiene_url and tiene_file): error += "Debe tener url o archivo" flag = False else: for file in os.listdir( join(MEDIA_ROOT, 'Archivo/' + str(goal_id))): if file == form.cleaned_data['upload'].name: error += "Este archivo ya se encuenta en esta meta" flag = False if flag: form.save() if subgoal_id: return redirect_goal(supgoal_id, goal_id) else: return redirect_goal(goal_id) else: error += "url no valido" else: form = forms.ArchivoForm(initial={'goal': goal_id, 'owner': user}) form.fields['goal'].widget = forms.HiddenInput() form.fields['owner'].widget = forms.HiddenInput() print error if subgoal_id: return render( request, "upload/index.html", { 'goal': goal, "form": form, 'subgoal_id': subgoal_id, "mensaje_error": error }) else: return render(request, "upload/index.html", { 'goal': goal, "form": form, "mensaje_error": error }) else: return HttpResponseRedirect("/login")
def delete_comment(request, goal_id, comment_id, subgoal_id=None): comment = get_object_or_404(Comment, pk=comment_id) goal = get_object_or_404(AbstractGoal, pk=goal_id) if request.user.is_authenticated: if (request.user == goal.owner): if request.method == "GET": comment.delete() if subgoal_id: return redirect_goal(goal_id, subgoal_id) else: return redirect_goal(goal_id) else: return redirect_home(request.user.username) else: return HttpResponseRedirect("/login")
def archivo_editar(request, id_archivo, goal_id, subgoal_id=None): if subgoal_id: supgoal_id = goal_id goal_id = subgoal_id goal = get_object_or_404(AbstractGoal, pk=goal_id) archivo = models.Archivo.objects.get(id=id_archivo) if request.method == 'GET': form = forms.ArchivoForm(instance=archivo) else: form = forms.ArchivoForm(request.POST, request.FILES, instance=archivo) if form.is_valid(): form.save() if subgoal_id: return redirect_goal(supgoal_id, goal_id) else: return redirect_goal(goal_id) return render(request, 'upload/index.html', {'form': form})
def archivo_eliminar(request, id_archivo, goal_id, subgoal_id=None): if subgoal_id: supgoal_id = goal_id goal_id = subgoal_id goal = get_object_or_404(AbstractGoal, pk=goal_id) archivo = models.Archivo.objects.get(id=id_archivo) form = forms.ArchivoForm(instance=archivo) if request.method == 'POST': file_to_delete = join(MEDIA_ROOT, archivo.upload.name) if isfile(file_to_delete): os.remove(file_to_delete) archivo.delete() if subgoal_id: return redirect_goal(supgoal_id, goal_id) else: return redirect_goal(goal_id) if subgoal_id: return render(request, 'upload/archivo_sub_delete.html', { 'goal': goal, 'supgoal_id': supgoal_id }) else: return render(request, 'upload/archivo_delete.html', {'goal': goal})