Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import file of usernames to provide private project access #773

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions osmtm/static/js/project.edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ $(document).ready(function() {
});
});

// taken from the great GeoJSON.io
function readAsText(f, callback) {
try {
var reader = new FileReader();
reader.readAsText(f);
reader.onload = function(e) {
if (e.target && e.target.result) callback(null, e.target.result);
else callback({
message: droppedFileCouldntBeLoadedI18n
});
};
reader.onerror = function(e) {
callback({
message: droppedFileWasUnreadableI18n
});
};
} catch (e) {
callback({
message: droppedFileWasUnreadableI18n
});
}
}

function allowedUsersCrtl($scope) {
$scope.allowed_users = allowed_users;

Expand Down Expand Up @@ -82,6 +105,51 @@ function allowedUsersCrtl($scope) {
}
});
});

$('#user_import').click(function() {
$('input[name=user_import]').click();
return false;
});


$('input[name=user_import]').change(function() {
function addUserToProject(userToAdd) {
$.ajax({
url: base_url + 'project/' + project_id + '/user/' + userToAdd,
type: 'PUT',
success: function(data) {
$scope.$apply(function() {
allowed_users[data.user.id] = data.user;
$scope.allowed_users = allowed_users;
});
}
});
}

var file = $(this).val();
if ((file.substr(-3) == 'txt') || (file.substr(-3) == 'csv')) {
readAsText($(this)[0].files[0], function(err, text) {
var lines = text.split(/\r\n|\n/);
var usersToAdd = [];

for (var i=0; i<lines.length; i++) {
var lineData = lines[i].split(',');
for (var j=0; j<lineData.length; j++) {
if (lineData[j] != "") {
usersToAdd.push(lineData[j]);
}
}
}
for (var i=0; i<usersToAdd.length; i++) {
addUserToProject(usersToAdd[i]);
}
});
} else {
alert(pleaseProvideTxtOrCSVFileI18n);
}
$(this).val('');
});

}
angular.module('allowed_users', []);

Expand Down
22 changes: 22 additions & 0 deletions osmtm/templates/project.edit.mako
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<script type="text/javascript" src="${request.static_url('osmtm:static/js/lib/bootstrap-datepicker.js')}"></script>
<script>
var locale_name = "${request.locale_name}";
var drawAreaOfInterestI18n = "${_('Draw the area of interest')}";
var droppedFileCouldntBeLoadedI18n = "${_('Dropped file could not be loaded')}";
var droppedFileWasUnreadableI18n = "${_('Dropped file was unreadable')}";
var pleaseProvideTxtOrCSVFileI18n = "${_('Please provide a .txt or a .csv file')}";
</script>

<%
Expand Down Expand Up @@ -394,6 +398,24 @@ geometry = loads(str(project.area.geometry.data))
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<center><h5>${_('or')}</h5></center>
</div>
</div>
<div class="row">
<div class="col-md-5">
<form id="uploadform" method="post" enctype="multipart/form-data">
<input type="file" val="" name="user_import" class="hidden" />
<%
link = '<a id="user_import" data-role="button" class="btn btn-default" rel="tooltip" title="%s">%s</a>' \
% (_('Provide a .txt or .csv file of usernames separated by commas or lines.'), _('Import'))
text = _('${user_import_link} a <em>txt</em> or <em>csv</em> file of Tasking Manager usernames.', mapping={'user_import_link': link})
%>
${text|n}
</form>
</div>
</div>
</div>
<%
from osmtm.models import dumps
Expand Down