Permalink
Browse files

Allow users to change their sorting folder.

  • Loading branch information...
1 parent 1cf29f9 commit 15da9ae33b98e735920e08679edb60dac78d2731 @mustpax committed Sep 8, 2012
Showing with 49 additions and 1 deletion.
  1. +41 −0 app/controllers/Accounts.java
  2. +7 −1 app/views/Accounts/settings.html
  3. +1 −0 conf/routes
@@ -4,8 +4,15 @@
import models.User;
import notifiers.Mails;
import play.Logger;
+import play.data.validation.Required;
+import play.data.validation.Validation;
import play.mvc.Controller;
import play.mvc.With;
+import rules.RuleUtils;
+
+import common.api.ApiClient;
+import common.api.ApiClientFactory;
+import dropbox.client.InvalidTokenException;
@With(Login.class)
public class Accounts extends Controller {
@@ -23,6 +30,40 @@ public static void settingsPost(boolean periodicSort) {
settings();
}
+ /**
+ * Update sortingFolder for user.
+ */
+ public static void sortingFolder(@Required String folder) {
+ User u = Login.getUser();
+ folder = RuleUtils.normalize(folder, false);
+
+ try {
+ ApiClient api = ApiClientFactory.create(u);
+ boolean createdFolder = false;
+ if (! api.exists(folder)) {
+ Logger.info("Folder does not exist attempting to create %s", folder);
+ if (api.mkdir(folder)) {
+ Logger.info("Successfully created folder %s", folder);
+ createdFolder = true;
+ } else {
+ Logger.error("Failed to create folder '%s'", folder);
+ flash.error("Error: folder %s is missing and we couldn't create it.", folder);
+ settings();
+ }
+ }
+
+ u.sortingFolder = folder;
+ String createdFolderMsg = createdFolder ? " This folder didn't exist, so we created it for you." : "";
+ flash.success("%s is now your sorting folder.%s", folder, createdFolderMsg);
+ u.save();
+ Logger.info("Updated sorting folder to %s", folder);
+ settings();
+ } catch (InvalidTokenException e) {
+ Logger.error(e, "Bad token when trying to update sorting folder to '%s' for user %s", folder, u);
+ Login.logout();
+ }
+ }
+
public static void settings() {
User user = Login.getUser();
render(user);
@@ -2,7 +2,6 @@
#{set title:'Settings'/}
#{set settings:'active'/}
-
<div class="row">
<div class="span8 offset2">
<dl class="dl-horizontal account-info ">
@@ -27,6 +26,13 @@
#{/form}
#{/else}
</dd>
+ <dt>Sorting folder</dt>
+ <dd>
+ #{form @sortingFolder(), method:'POST', class:'form-inline' }
+ <input type="text" name="folder" value="${user.sortingFolder}">
+ <input type="submit" class="btn" value="Update">
+ #{/form}
+ </dd>
<dt>Last sync</dt>
#{if user.lastSync}
<dd>#{if user.lastSync}${user.lastSync.since()}#{/if}#{else}<em>None yet</em>#{/else}</dd>
View
@@ -12,6 +12,7 @@ GET /account/settings Accounts.settings
POST /account/settings Accounts.settingsPost
GET /account/delete Accounts.delete
POST /account/delete Accounts.deletePost
+POST /account/sortingFolder Accounts.sortingFolder
# Footer
GET /team Footer.team

0 comments on commit 15da9ae

Please sign in to comment.