def locations_import_preview(request, id, template_name='locations/import-map-fields.html'): """ This will generate a form based on the uploaded CSV for field mapping. A preview will be generated based on the mapping given. """ locport = get_object_or_404(LocationImport, pk=id) if request.method == 'POST': form = ImportMapForm(request.POST, locport=locport) if form.is_valid(): # Show the user a preview based on the mapping cleaned_data = form.cleaned_data #file_path = os.path.join(settings.MEDIA_ROOT, locport.get_file().file.name) file_path = locport.get_file().file.name locations, stats = parse_locs_from_csv(file_path, cleaned_data) # return the form to use it for the confirm view template_name = 'locations/import-preview.html' return render_to_response(template_name, { 'locations': locations, 'stats': stats, 'locport': locport, 'form': form, 'now': datetime.now(), }, context_instance=RequestContext(request)) else: form = ImportMapForm(locport=locport) return render_to_response(template_name, { 'form': form, 'locport': locport, 'now': datetime.now(), }, context_instance=RequestContext(request))
def run(self, user, file_path, fields, **kwargs): location_fields = [f.name for f in Location._meta.fields] #get parsed membership dicts imported = [] locs, stats = parse_locs_from_csv(file_path, fields) for m in locs: if not m['skipped']: # Create Location object obj_dict = {} for key in m.keys(): if key in location_fields: if isinstance(m[key], basestring): m[key] = m[key].strip() obj_dict[key] = m[key] # Add other fields obj_dict['location_name'] = m['locationname'] obj_dict['creator'] = user obj_dict['creator_username'] = user.username obj_dict['owner'] = user obj_dict['owner_username'] = user.username # Import Locations as publicly viewable obj_dict['allow_anonymous_view'] = True obj_dict['allow_user_view'] = False obj_dict['allow_member_view'] = False obj_dict['allow_user_edit'] = False obj_dict['allow_member_edit'] = False new_location = Location.objects.create(**obj_dict) # append to imported list imported.append(new_location) return imported, stats
def run(self, user, file_path, fields, **kwargs): location_fields = [f.name for f in Location._meta.fields] # get parsed membership dicts imported = [] locs, stats = parse_locs_from_csv(file_path, fields) for m in locs: if not m["skipped"]: # Create Location object obj_dict = {} for key in m.keys(): if key in location_fields: obj_dict[key] = m[key] # Add other fields obj_dict["location_name"] = m["locationname"] obj_dict["creator"] = user obj_dict["creator_username"] = user.username obj_dict["owner"] = user obj_dict["owner_username"] = user.username # TODO: Set permissions properly obj_dict["allow_user_view"] = False obj_dict["allow_member_view"] = False obj_dict["allow_anonymous_edit"] = False obj_dict["allow_user_edit"] = False obj_dict["allow_member_edit"] = False new_location = Location.objects.create(**obj_dict) # append to imported list imported.append(new_location) return imported, stats