Comments (12)
I think we should do this using a separate controller and table in the database, so we can store "temporary users" somewhere for data review / before we actually submit to save to the "real" database, similar to what the valiant @ebmaher has done with the cart not-yet-finalized reservations.
from reservations.
Okay, nevermind Erin you didn't go the controller route, this can also work well with a separate controller action in Users, which I'll then work on.
from reservations.
Notes:
modify to work with ALL the data columns, in case LDAP undefined / unavailablegive preference to CSV filegive a clearer example of CSV in the view prior to file upload
from reservations.
option when you submit to overwrite on netid conflict (a sort of 'update all the fields' button)option on success page to reimport conflict users (which are saved in a hash, so would be trivial to resubmit and save)delete all just-imported option (in case you REALLY screwed up)adam said nvm about thischange the deactivate buttons to hard-delete buttons, since that won't break anything and probs would not have imported if you're just going to deactivate / not want to wholly delete, and in case there was a problem with just one recordactually I think we don't want this
from reservations.
Code review:
In general, make your variable names more clear. For example, there are lots of cases where you use an instance variable called 'user' where it's really a user_id. This makes code much easier to read. Another example, @user_formatted should
be @formatted_user most likely.
Also, in the import controller action, I feel like there are a few cases where you're using instance variables (ie @user) where you can just use local variables (user), i.e. line 142.
Other stuff:
app/controllers/application_controller.rb Line 186:
Can this be simplified using Rails' 'to_sentence' method?
app/models/user.rb Line 129:
Can be simplified to:
https://gist.github.com/3183717
app/controllers/users_controller.rb
Line 116 - 120 can be shortened to:
@overwrite = (params[:overwrite] == '1'))
Line 123: Using unless in this way is confusing. I feel like 'if !condition' is more readable:
if !file.nil?
...stuff
or even better, I think this should work:
if file
#do stuff
Other stuff:
CSV import should be limited to admins I think.
You're only enforcing this at the view level. The import controller action should check for admin status as well, and redirect if it's not present.
I feel like we can refactor the two User class methods a bit. Let's talk about this together.
from reservations.
renamed unclear variablesmade variables local when appropriateto_sentence might simplify, might not; need to test to see how much shorter or cleaner it would be.user.rb ln129 not really simplifiableunless I extracted it to elsewheresimplified @overwrite and if filerestricted to admin
Idea for rest of refactoring:
Controller: get file
Model: Parse file, with status codes, already having done LDAP if necessary. Read first line headers as symbols for hash
# see if CSV lib can parse to hash; otherwise put in module
[{:login => ' ', :first_name => ' '}] # for example
Model: Create all of the users which can be created.
{:success => [[user_object,'Successfully created.'],[user_object,'Successfully updated.']],
:fail => [[user_hash,''],[user_hash,'update']} # the error messages
Controller: Prep for display
View: Display
from reservations.
(had accidentally closed)
from reservations.
http://stackoverflow.com/questions/2609891/get-in-that-db-parsing-csv-using-ruby?rq=1 <-- this one works
from reservations.
@adambray I've still got some very-regrettable code duplication in the users model (lines 156-169 and 196-209), but I don't see a particularly elegant way around it. I could extract that code block down out of the big (if update_existing)..else statement, and then move the sub-else's below it; but then I'd have to duplicate the update_existing-if and user declaration statements. :-(
Otherwise I think this is a much cleaner implementation. :-)
from reservations.
@adambray I've implemented the changes we talked about on Friday ('location' variable => 'filepath'; safe import defaults in case undefined [for 'do not update existing' and 'user_type = normal']; and modularized the duplicate code in user.rb as far as possible [still some dup in error handling, but not really avoidable]).
This implementation still relies on the first line giving the header / column information, though. When you have time, I'd be interested to hear your idea for handling when the first line does NOT give proper header information. Right now I have an (uncommitted) test in the users controller action:
imported_users.first.keys.each do |key|
unless current_user.attributes.symbolize_keys.keys.include?(key)
flash[:error] = 'Unable to import CSV file. Please ensure the first line of the file includes proper header information (login,first_name,...) as indicated below.'
redirect_to :back and return
end
end
from reservations.
re above: what I don't like about that solution is that it (1) calls current_user too many times (fixed on my local) and (2) will fail if you have an extra column of data that the controller has no use for (I tested by adding the extraneous column 'potato', for example).
from reservations.
@adambray That test (code posted two days ago in this issue; except before loop declare user_attributes_keys = current_user.attributes.symbolize_keys.keys [an array] to avoid unnecessary calls to current_user in the loop) could work well even in the second case (extraneous columns), if the flash[:error] were updated to indicate please no extraneous columns.
That is, unless we come up with a better idea; although I would like to see some form of CSV import in v3.1, so, perhaps this code (with or without this test) is good enough for the time being? Of course am open to suggestions. :-)
from reservations.
Related Issues (20)
- Reservation request approval doesn't quite persist HOT 1
- Importing users to ban them broken HOT 1
- Sensitive information on Equipment Model pages is available to the public HOT 1
- Cancelling an overdue reservation does not reset the overdue count on the model
- Recurring blackout conflict logic broken HOT 1
- Latest security fixes
- Link in flash when viewing as a different role is escaped out
- Update Loofah
- CSV USer Importer Bug HOT 1
- Update rack to Address Security Vulnerability
- Swap out Capybara-Webkit for Headless Chrome Driver
- Editing blackouts is broken HOT 1
- Date format
- Email return receipt HOT 1
- Error at checkout HOT 1
- View as guest shows equipment model requirements (but not visible to guests)
- Add Dockerfile HOT 2
- NoMethodError in ReportsController#subreport
- rake app:setup undefined method `noecho' HOT 1
- LDAP User Import Error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reservations.