def resource_create_step1_view(request): if request.user.is_anonymous: return render(request, 'orb/login_required.html', { 'message': _('You need to be logged in to add a resource.'), }) if request.method == 'POST': form = ResourceStep1Form(request.POST, request.FILES, request=request) resource_form_set_choices(form) if form.is_valid(): # save resource resource = Resource(status=Resource.PENDING, create_user=request.user, update_user=request.user) resource.title = form.cleaned_data.get("title") resource.description = form.cleaned_data.get("description") if form.cleaned_data.get("study_time_number") and form.cleaned_data.get("study_time_unit"): resource.study_time_number = form.cleaned_data.get( "study_time_number") resource.study_time_unit = form.cleaned_data.get( "study_time_unit") if 'image' in request.FILES: resource.image = request.FILES["image"] resource.attribution = form.cleaned_data.get("attribution") resource.save() # add organisation(s)/geography and other tags resource_add_free_text_tags( resource, form.cleaned_data.get('organisations'), request.user, 'organisation') resource_add_free_text_tags( resource, form.cleaned_data.get('geography'), request.user, 'geography') resource_add_free_text_tags( resource, form.cleaned_data.get('languages'), request.user, 'language') resource_add_free_text_tags( resource, form.cleaned_data.get('other_tags'), request.user, 'other') # add tags resource_add_tags(request, form, resource) # see if email needs to be sent resource_workflow.send(sender=resource, resource=resource, request=request, status=Resource.PENDING, notes="") resource_submitted.send(sender=resource, resource=resource, request=request) # redirect to step 2 # Redirect after POST return HttpResponseRedirect(reverse('orb_resource_create2', args=[resource.id])) else: if request.user.userprofile.organisation: user_org = request.user.userprofile.organisation.name initial = {'organisations': user_org, } else: initial = {} form = ResourceStep1Form(initial=initial, request=request) resource_form_set_choices(form) return render(request, 'orb/resource/create_step1.html', {'form': form})
def resource_edit_view(request, resource_id): resource = get_object_or_404(Resource, pk=resource_id) if not resource_can_edit(resource, request.user): raise Http404() if request.method == 'POST': form = ResourceStep1Form(data=request.POST, files=request.FILES) resource_form_set_choices(form) if form.is_valid(): resource.update_user = request.user resource.title = form.cleaned_data.get("title") resource.description = form.cleaned_data.get("description") if form.cleaned_data.get( "study_time_number") and form.cleaned_data.get( "study_time_unit"): resource.study_time_number = form.cleaned_data.get( "study_time_number") resource.study_time_unit = form.cleaned_data.get( "study_time_unit") resource.attribution = form.cleaned_data.get("attribution") resource.save() # update image image = form.cleaned_data.get("image") if image == False: resource.image = None resource.save() if request.FILES.has_key('image'): resource.image = request.FILES["image"] resource.save() # update tags - remove all current tags first ResourceTag.objects.filter(resource=resource).delete() resource_add_tags(request, form, resource) resource_add_free_text_tags(resource, form.cleaned_data.get('organisations'), request.user, 'organisation') resource_add_free_text_tags(resource, form.cleaned_data.get('geography'), request.user, 'geography') resource_add_free_text_tags(resource, form.cleaned_data.get('languages'), request.user, 'language') resource_add_free_text_tags(resource, form.cleaned_data.get('other_tags'), request.user, 'other') # All successful - now redirect # Redirect after POST return HttpResponseRedirect( reverse('orb_resource_edit2', args=[resource.id])) else: initial = request.POST.copy() initial['image'] = resource.image files = ResourceFile.objects.filter(resource=resource)[:1] if files: initial['file'] = files[0].file form = ResourceStep1Form(initial=initial, data=request.POST, files=request.FILES) resource_form_set_choices(form) else: data = {} data['title'] = resource.title organisations = Tag.objects.filter( category__slug='organisation', resourcetag__resource=resource).values_list('name', flat=True) data['organisations'] = ', '.join(organisations) data['description'] = resource.description data['image'] = resource.image data['study_time_number'] = resource.study_time_number data['study_time_unit'] = resource.study_time_unit data['attribution'] = resource.attribution files = ResourceFile.objects.filter(resource=resource)[:1] if files: data['file'] = files[0].file urls = ResourceURL.objects.filter(resource=resource)[:1] if urls: data['url'] = urls[0].url health_topic = Tag.objects.filter( category__top_level=True, resourcetag__resource=resource).values_list('id', flat=True) data['health_topic'] = health_topic resource_type = Tag.objects.filter( category__slug='type', resourcetag__resource=resource).values_list('id', flat=True) data['resource_type'] = resource_type audience = Tag.objects.filter( category__slug='audience', resourcetag__resource=resource).values_list('id', flat=True) data['audience'] = audience geography = Tag.objects.filter( category__slug='geography', resourcetag__resource=resource).values_list('name', flat=True) data['geography'] = ', '.join(geography) languages = Tag.objects.filter( category__slug='language', resourcetag__resource=resource).values_list('name', flat=True) data['languages'] = ', '.join(languages) device = Tag.objects.filter( category__slug='device', resourcetag__resource=resource).values_list('id', flat=True) data['device'] = device license = Tag.objects.filter( category__slug='license', resourcetag__resource=resource).values_list('id', flat=True) if license: data['license'] = license[0] other_tags = Tag.objects.filter(resourcetag__resource=resource, category__slug='other').values_list( 'name', flat=True) data['other_tags'] = ', '.join(other_tags) form = ResourceStep1Form(initial=data) resource_form_set_choices(form) return render(request, 'orb/resource/edit.html', {'form': form})