示例#1
0
 def create_folder(user_id, name, parent_id=None):
     user = User.objects.filter(id=user_id).first()
     parent = None
     if not user:
         return None
     if parent_id:
         parent = Folder.objects.filter(id=parent_id).first()
         if not parent:
             return None
     existing_folder = Folder.objects.filter(
         name=name, owner=user, parent=parent).first()
     if existing_folder:
         return None
     new_folder = Folder(name=name, owner=user, parent=parent)
     new_folder.save()
     return FolderRepo.to_json(new_folder)
示例#2
0
def folder_add(request):
    context = {
        "profile": Profile.objects.get(user=request.user),
        "bot_api_key": BotKey.objects.get(user=request.user).key
    }
    if request.method == "POST":
        folder_form = FolderAddForm(request.POST)
        if folder_form.is_valid():
            try:
                folder = Folder.objects.get(name=folder_form.data['name'],
                                            user=request.user)
                messages.error(
                    request,
                    f"Вы уже создали подборку \"{folder_form.data['name']}\". Выберите другое название."
                )
                context["form"] = FolderAddForm(request.POST)
                return render(request, "folder/add.html", context)
            except Folder.DoesNotExist:
                ok = check_blacklist(folder_form.data['name'])
                if ok is not None:
                    messages.error(
                        request,
                        f"Название подборки не должно содержать \"{ok}\"")
                    return render(request, "folder/add.html", context)
                folder = Folder(name=folder_form.data['name'],
                                user=request.user)
                private = "Публичная"
                if not request.POST.get("public", None):
                    folder.public = False
                    private = "Приватная"
                folder.save()
                messages.success(
                    request,
                    f"{private} подборка \"{folder_form.data['name']}\" создана"
                )
                return redirect(reverse('link_add'))
        else:
            messages.error(request, f"Неправильные данные")
            context["form"] = FolderAddForm(request.POST)
    else:
        context["form"] = FolderAddForm()
    return render(request, "folder/add.html", context)