def autoSave(request, resourceType, title, goals, materials, lesson): dajax = Dajax() try: d = request.user.draft except: d = Draft(user=request.user) d.resourceType = resourceType d.title = title d.lesson = lesson d.goals = goals d.materials = materials d.save() #dajax.assign('#id_save', 'innerHTML', 'Saved!') return dajax.json()
def submit_resource(request): context = RequestContext(request) context = base(context) if not request.user.is_authenticated(): return render_to_response('login.html', {}, context) if request.method == 'POST': form = SubmitResourceForm(request.POST, error_class=DivErrorList) if 'save' in request.POST: try: d = request.user.draft except: d = Draft(user=request.user) d.resourceType = request.POST['resourceType'] d.title = request.POST['title'] d.lesson = request.POST['lesson'] d.goals = request.POST['goals'] d.materials = request.POST['materials'] d.save() form = SubmitResourceForm(initial={ 'resourceType': d.resourceType, 'title': d.title, 'goals': d.goals, 'materials': d.materials, 'lesson': d.lesson }, error_class=DivErrorList) return render_to_response( 'submit_resource.html', { 'message': 'Your changes have been saved', 'submit_resource_form': form }, context) if form.is_valid(): try: resourceType = form.cleaned_data['resourceType'] if resourceType == "Activity": activity = Activity() activity.title = form.cleaned_data['title'] activity.lesson = form.cleaned_data['lesson'] activity.goals = form.cleaned_data['goals'] activity.materials = form.cleaned_data['materials'] activity.author = request.user activity.save() activity.chapters.add(form.cleaned_data['chapter']) if request.FILES: attachments = request.FILES.getlist('attachment') try: if attachments: for attach in attachments: file = File.objects.create(title=attach.name, doc=attach) activity.files.add(file) activity.save() except: activity.save() else: project = Project() project.title = form.cleaned_data['title'] project.instructions = form.cleaned_data['lesson'] project.goals = form.cleaned_data['goals'] project.materials = form.cleaned_data['materials'] project.author = request.user project.save() project.chapters.add(form.cleaned_data['chapter']) if request.FILES: attachments = request.FILES.getlist('attachment') try: if attachments: for attach in attachments: file = File.objects.create(title=attach.name, doc=attach) project.files.add(file) project.save() except: project.save() form = SubmitResourceForm(error_class=DivErrorList) return render_to_response( 'submit_resource.html', { 'message': 'Thank you for your submission!', 'submit_resource_form': form }, context) except Exception as exp: form = SubmitResourceForm(request.POST, error_class=DivErrorList) return render_to_response('submit_resource.html', { 'message': exp, 'submit_resource_form': form }, context) else: form = SubmitResourceForm(request.POST, error_class=DivErrorList) return render_to_response( 'submit_resource.html', { 'message': 'Form data is invalid! Please try again', 'submit_resource_form': form }, context) else: try: d = request.user.draft form = SubmitResourceForm(initial={ 'resourceType': d.resourceType, 'title': d.title, 'goals': d.goals, 'materials': d.materials, 'lesson': d.lesson }, error_class=DivErrorList) except: form = SubmitResourceForm(error_class=DivErrorList) return render_to_response( 'submit_resource.html', { 'message': 'We loaded your last saved resource', 'submit_resource_form': form }, context)