def url_add(request, slug): #get the project by the project slug project = get_project(slug) #handle the post form_errors = None # get list of institutions to populate autocomplete institutions = get_look_ahead(project) some_errors = {} summary_list = [] req_fields = project.project_metadata_set.filter(required=True) date_fields = project.project_metadata_set.filter(form_type='date') posted_data = None url_entity = None if request.method == 'POST': posted_data = request.POST.copy() form = URLForm(posted_data) form_errors = form.errors # check for required fields of project specific metadata for met in req_fields: if met.metadata.name not in posted_data or \ len(posted_data[met.metadata.name]) == 0 or \ posted_data[met.metadata.name].isspace(): some_errors[met.metadata.name] = \ unicode('This field is required.') # check validity of dates for met in date_fields: if met not in req_fields and not posted_data[met.metadata.name]: pass elif met.metadata.name in posted_data: 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('Please enter a valid date format.') # check validity of standard metadata # 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 posted_data[nominator_field].strip(): some_errors[nominator_field] = unicode( 'This field is required.') else: if posted_data['nominator_name'].strip() or\ posted_data['nominator_institution'].strip() or\ posted_data['nominator_email'].strip(): for nominator_field in nominator_fields: if not posted_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.') if form.is_valid(): if not form.cleaned_data['url_value'].strip() == 'http://': # summary_list = add_url(project, form.cleaned_data) if not some_errors: # combine form class data with project specific data posted_data.update(form.cleaned_data) if not posted_data['nominator_email'].strip(): # set anonymous user posted_data['nominator_email'] = 'anonymous' posted_data['nominator_name'] = 'Anonymous' posted_data['nominator_institution'] = 'Anonymous' # handle multivalue metadata and user supplied values posted_data = handle_metadata(request, posted_data) summary_list = add_url(project, posted_data) # send url value to provide metadata link url_entity = posted_data['url_value'] # clear out posted data, so it is not sent back to form posted_data = None else: form_errors.update(some_errors) else: form_errors = 'Please specify the URL' else: form_errors.update(some_errors) form = URLForm() # create metadata/values set metadata_vals = get_metadata(project) if not len(summary_list) > 0: summary_list = None # in case of an 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_add.html', { 'project': project, 'form': 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, 'url_entity': url_entity, }, RequestContext(request, {}), )
def test_returns_none_with_invalid_date(self): date_str = '2006, Oct 25' assert url_handler.validate_date(date_str) is None
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_returns_valid_date(self, date_str): assert isinstance(url_handler.validate_date(date_str), datetime.date)