def test_returns_expected(self): project = factories.ProjectWithMetadataFactory() nominator = factories.NominatorFactory() attribute_name = project.metadata.first().name value = 'some_value' form_data = { 'nominator_email': nominator.nominator_email, 'scope': '1', 'url_value': 'http://www.example.com', attribute_name: value } expected = [ 'You have successfully nominated {0}'.format(form_data['url_value']), 'You have successfully added the {0} "{1}" for {2}'.format( attribute_name, value, form_data['url_value']) ] assert url_handler.add_metadata(project, form_data) == expected
def url_listing(request, slug, url_entity): # Add back the slash lost by Apache removing null path segments. url_entity = fix_http_double_slash(url_entity) url_exists = True posted_data = None # get the project by the project slug project = get_project(slug) # get list of institutions to populate autocomplete institutions = get_look_ahead(project) # create metadata/values set metadata_vals = get_metadata(project) # get the list of URLs try: find_url = URL.objects.filter(entity__iexact=url_entity, url_project=project) except: url_exists = False if url_exists and len(find_url) > 0: form_errors = None summary_list = None if request.method == 'POST': posted_data = request.POST.copy() scope_form = ScopeForm(posted_data) form_errors = scope_form.errors some_errors = {} # check validity of dates for met in project.project_metadata_set.all().filter(form_type='date'): if met.metadata.name in posted_data: if not posted_data[met.metadata.name]: # added this 'if' when iipc wanted not to require any fields # on url_listing edit form continue cleandate = validate_date(posted_data[met.metadata.name]) if cleandate: cleandate = str(cleandate) # check if this fits valid date range. # The following code presumes (too much) if there are # values associated with this metadata date field, the # first date will be the default value for the form. If # two dates are given, in addition to the first date # being the form default, it will be the start of a # range, while the second value will be the end of that # range. If three values are given, the first value is, # as always, default for the form, and the next 2 dates # serve as the start and end points of the valid range. datevals = met.metadata.values.all().order_by('metadata_values') numvals = len(datevals) if numvals > 1: if numvals == 2: rangestart = datevals[0].value rangeend = datevals[1].value elif numvals > 2: rangestart = datevals[1].value rangeend = datevals[2].value if not (cleandate >= rangestart and \ cleandate <= rangeend): some_errors[met.metadata.name] = \ unicode('The date you entered is outside the allowed range.') else: # store the valid date posted_data[met.metadata.name] = cleandate else: posted_data[met.metadata.name] = cleandate else: some_errors[met.metadata.name] = \ unicode('Enter a valid date format.') # check validity of standard metadata if scope_form.is_valid(): # check if nominator is required by project nominator_fields = ['nominator_name', 'nominator_email', 'nominator_institution'] if project.registration_required: for nominator_field in nominator_fields: if not scope_form.cleaned_data[nominator_field].strip(): some_errors[nominator_field] = unicode( 'This field is required.') else: if scope_form.cleaned_data['nominator_name'].strip() or\ scope_form.cleaned_data['nominator_institution'].strip() or\ scope_form.cleaned_data['nominator_email'].strip(): for nominator_field in nominator_fields: if not scope_form.cleaned_data[nominator_field].strip(): some_errors[nominator_field] = unicode( 'You must ' +\ 'provide name, institution, and email ' +\ 'to affiliate your name or institution ' +\ 'with nominations. Leave all "Information ' +\ 'About You" fields blank to remain ' +\ 'anonymous.') else: #supply anonymous information scope_form.cleaned_data['nominator_email'] = 'anonymous' scope_form.cleaned_data['nominator_name'] = 'Anonymous' scope_form.cleaned_data['nominator_institution'] = \ 'Anonymous' if not some_errors: #combine cleaned form class data with project specific data posted_data.update(scope_form.cleaned_data) # handle multivalue metadata and user supplied values posted_data = handle_metadata(request, posted_data) posted_data['url_value'] = url_entity summary_list = add_metadata(project, posted_data) # clear out posted data, so it is not sent back to form posted_data = None else: form_errors.update(some_errors) else: #Create the scope form scope_form = ScopeForm() #Create a dictionary from the URLs information pulled from all the URLs entries url_list = URL.objects.filter(entity__iexact=url_entity, url_project=project).order_by('attribute') url_data = create_url_list(project, url_list) #Grab all related URLs try: related_url_list = URL.objects.filter(url_project=project, attribute__iexact='surt', \ value__istartswith=url_data['surt']).order_by('value').exclude(entity__iexact=url_entity) except: related_url_list = None # in case of a user input error, send back data to repopulate form json_data = None if posted_data: json_data = json.dumps(posted_data.lists()) # send form types for use to repopulate form form_types = {} for pm in project.project_metadata_set.all(): form_types[pm.metadata.name] = str(pm.form_type) return render_to_response( 'nomination/url_listing.html', { 'project': project, 'url_data': url_data, 'related_url_list': related_url_list, 'scope_form': scope_form, 'form_errors': form_errors, 'summary_list': summary_list, 'metadata_vals': metadata_vals, 'json_data' : json_data, 'form_types' : json.dumps(form_types), 'institutions': institutions, }, RequestContext(request, {}), ) else: default_data = {'url_value': url_entity,} form = URLForm(default_data) return render_to_response( 'nomination/url_add.html', { 'project': project, 'form': form, 'url_not_found': True, 'metadata_vals': metadata_vals, 'form_errors': None, 'summary_list': None, 'json_data' : None, 'form_types' : None, 'institutions': institutions, 'url_entity': url_entity, }, RequestContext(request, {}), )
def test_nominator_not_found(self): project = factories.ProjectFactory() form_data = {'nominator_email': '*****@*****.**'} with pytest.raises(http.Http404): url_handler.add_metadata(project, form_data)