def url_surt(request, slug, surt): # Add back the slash lost by Apache removing null path segments. surt = fix_http_double_slash(surt) # Get the project by the project slug. project = get_project(slug) # Create the SURT dictionary containing url_list and single_letter. surt_dict = create_surt_dict(project, surt) # Create the alphabetical browse dictionary. browse_dict = alphabetical_browse(project) # Add Browse by if browsing surts by letter. top_domain_search = re.compile(r'^http://\(([^,]+),?').search(surt, 0) if top_domain_search: top_domain = top_domain_search.group(1) else: top_domain = None return render_to_response( 'nomination/url_surt.html', { 'surt': surt, 'project': project, 'url_list': surt_dict['url_list'], 'letter': surt_dict['letter'], 'browse_domain': top_domain, 'browse_dict': browse_dict, }, RequestContext(request, {}), )
def value_report(request, slug, field, val): val = urllib.unquote(val) # Add back the slash lost by Apache removing null path segments. val = fix_http_double_slash(val) # Get the project by the project slug. project = get_project(slug) # If there are no URLs in the queryset, Apache rewrote the url, # so we need to add a trailing slash to do a lookup properly. urls = URL.objects.filter(url_project_id=project.id, attribute=field, value=val) if urls.count() == 0: val = val + '/' urls = URL.objects.filter(url_project_id=project.id, attribute=field, value=val) report_text = '#This list of URLs was created for the ' + project.project_name + \ ' project.\n#URLs for value "' + val + '" in metadata field "' + field + '"\n' + \ '#List generated on ' + \ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%SZ") + "\n\n" for entity in urls.values_list('entity', flat=True).order_by('entity').distinct(): report_text += unicode(entity) + '\n' return HttpResponse(report_text, content_type='text/plain;')
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_fix_http_double_slash(): url = 'http:/www.example.com' expected = 'http://www.example.com' assert url_handler.fix_http_double_slash(url) == expected