def categories(request, project_slug): project = Project.objects.get(slug=project_slug) categories = GroupCategory.objects.filter(parent=project) from django.template.defaultfilters import slugify if request.method == "POST": categories_pairs = request.POST.copy() del categories_pairs['csrfmiddlewaretoken'] del categories_pairs['project_id'] for key, value in categories_pairs.items(): if key == 'new' and value != '': new_category = GroupCategory( parent=project, title=value.strip(), slug=slugify(value) ) new_category.save() else: current_category = next( category for category in categories if category.id == int(key)) if current_category.title != value.strip(): current_category.title = value.strip() current_category.save() from django.contrib import messages messages.success(request, 'Categories have been saved successfully.') categories = GroupCategory.objects.filter(parent=project) context = { 'project': project, 'categories': categories, 'title': (_(settings.STRINGS['projects']['CATEGORIES']) + ' ‹ ' + project.title) } return render(request, 'project/categories.html', context)
def new_project(request): if not request.user.is_authenticated(): return redirect('/login/?source=%s' % request.path) # If a form submission was made if request.method == "POST": from forms import ProjectForm new_project_form = ProjectForm(request.POST, request.user) if new_project_form.is_valid(): try: # Try creating a new project with the form inputs new_project = new_project_form.save() # Add the creator to the project members. project_creator = Membership( user=request.user, project=new_project, confirmed=True) project_creator.save() # Create a new root collection for the project from django.template.defaultfilters import slugify title = request.POST.get('title') from oer.models import Collection root_collection = Collection( title=title + "_root", host=new_project, visibility=request.POST.get('visibility'), slug=slugify(title), creator=request.user ) root_collection.save() new_project.collection = root_collection new_project.save() # Set default cover pic if not uploaded by user if not new_project.cover_pic: _set_cover_picture(new_project, new_project_form) # Add a default category in the project. group_category = GroupCategory( parent=new_project, title='General', slug='general' ) group_category.save() Project.project_created.send( sender="Project", project=new_project, creator=request.user) return redirect( 'projects:project_home', project_slug=new_project.slug) except: print "Could not create new project" else: print new_project_form.errors print "The form had errors" context = { 'title': _(settings.STRINGS['projects']['NEW_PROJECT_TITLE']) } return render(request, 'new-project.html', context)