Skip to content

Commit

Permalink
list links
Browse files Browse the repository at this point in the history
  • Loading branch information
flurdy committed Nov 27, 2012
1 parent d005f94 commit 5f94ff1
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 114 deletions.
24 changes: 12 additions & 12 deletions app/controllers/Secured.scala
Expand Up @@ -31,7 +31,7 @@ trait Secured {

def withCurrentRecipient(f: Recipient => Request[AnyContent] => Result) = isAuthenticated {
username => implicit request =>
Recipient.findByUsername(username).map { recipient =>
Recipient.findByUsername(username).map { recipient =>

f(recipient)(request)

Expand All @@ -45,7 +45,7 @@ trait Secured {
Recipient.findByUsername(profileUsername) match {
case Some(profileRecipient) => {
if( currentRecipient == profileRecipient || currentRecipient.isAdmin ){

f(currentRecipient)(request)

} else {
Expand All @@ -63,20 +63,20 @@ trait Secured {
case Some(wishlist) => {
if(wishlist.recipient.username == username) {

f(wishlist)(request)
f(wishlist)(request)

} else {
Logger.warn("Wishlist %d recipient is not %s".format(wishlistId,username))
Results.NotFound(views.html.error.notfound()(request.flash,findCurrentRecipient(request.session),analyticsDetails))
}
}

}
case None => Results.NotFound(views.html.error.notfound()(request.flash,findCurrentRecipient(request.session),analyticsDetails))
}
}


def isRecipientOfWishlist(username:String,wishlistId:Long)(f: => (Wishlist,Recipient) => Request[AnyContent] => Result) = withCurrentRecipient { currentRecipient => implicit request =>
def isEditorOfWishlist(username:String,wishlistId:Long)(f: => (Wishlist,Recipient) => Request[AnyContent] => Result) = withCurrentRecipient { currentRecipient => implicit request =>
Wishlist.findById(wishlistId) match {
case Some(wishlist) => {
if(wishlist.recipient.username == username) {
Expand All @@ -87,22 +87,22 @@ trait Secured {
} else {
Logger.warn("Recipient %s is not a recipient of wishlist %d".format(currentRecipient.username,wishlistId))
Results.Unauthorized(views.html.error.permissiondenied()(request.flash,Some(currentRecipient),analyticsDetails))
}
}
} else {
Logger.warn("Wishlist %d recipient is not %s".format(wishlistId,username))
Results.NotFound(views.html.error.notfound()(request.flash,Some(currentRecipient),analyticsDetails))
}
}
}
case None => Results.NotFound(views.html.error.notfound()(request.flash,Some(currentRecipient),analyticsDetails))
}
}



def isRecipientOfWish(username:String,wishlistId:Long,wishId:Long)(f: => (Wish,Wishlist,Recipient) => Request[AnyContent] => Result) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>

def isEditorOfWish(username:String,wishlistId:Long,wishId:Long)(f: => (Wish,Wishlist,Recipient) => Request[AnyContent] => Result) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
WishEntry.findByIds(wishId,wishlistId) match {
case Some(wishEntry) => {
if(wishlistId == wishEntry.wishlist.wishlistId.get){
if(wishlistId == wishEntry.wishlist.wishlistId.get){

f(wishEntry.wish,wishlist,currentRecipient)(request)

Expand All @@ -116,12 +116,12 @@ trait Secured {
}

implicit def analyticsDetails: Option[String] = Play.configuration.getString("analytics.id")


def withWish(username:String,wishlistId:Long,wishId:Long)(f: => (Wish,Wishlist) => Request[AnyContent] => Result) = withWishlist(username,wishlistId) { wishlist => implicit request =>
WishEntry.findByIds(wishId,wishlistId) match {
case Some(wishEntry) => {
if(wishlistId == wishEntry.wishlist.wishlistId.get){
if(wishlistId == wishEntry.wishlist.wishlistId.get){

f(wishEntry.wish,wishlist)(request)

Expand Down
79 changes: 42 additions & 37 deletions app/controllers/WishController.scala
Expand Up @@ -18,7 +18,7 @@ object WishController extends Controller with Secured {

val searchForm = Form {
"term" -> optional(text(maxLength = 99))
}
}

val simpleAddWishForm = Form {
"title" -> text(maxLength = 50,minLength = 2 )
Expand All @@ -33,9 +33,9 @@ object WishController extends Controller with Secured {

val updateWishlistOrderForm = Form(
"order" -> text(maxLength=500)
)
)

val addOrganiserForm = Form {
val addOrganiserForm = Form {
tuple(
"recipient" -> text(maxLength = 180,minLength = 2 ),
"wishlistid" -> number,
Expand All @@ -52,7 +52,7 @@ object WishController extends Controller with Secured {
case (recipient,wishlistid,username) => {
Wishlist.findById(wishlistid) match {
case Some(wishlist) => {
Recipient.findByUsername(username.trim) match {
Recipient.findByUsername(username.trim) match {
case Some(recipient) => !wishlist.isOrganiser(recipient)
case None => true
}
Expand Down Expand Up @@ -85,20 +85,20 @@ object WishController extends Controller with Secured {
}


def showEditWishlist(username:String,wishlistId:Long) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
def showEditWishlist(username:String,wishlistId:Long) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
val editForm = editWishlistForm.fill((wishlist.title,wishlist.description))
val organisers = wishlist.findOrganisers
Ok(views.html.wishlist.editwishlist(wishlist, editForm,organisers,addOrganiserForm))
}


def updateWishlist(username:String,wishlistId:Long) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
def updateWishlist(username:String,wishlistId:Long) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
editWishlistForm.bindFromRequest.fold(
errors => {
Logger.warn("Update failed: " + errors)
val organisers = wishlist.findOrganisers
BadRequest(views.html.wishlist.editwishlist(wishlist,errors,organisers,addOrganiserForm))
},
},
editForm => {
Logger.info("Updating wishlist: " + editForm)

Expand All @@ -111,7 +111,7 @@ object WishController extends Controller with Secured {



def deleteWishlist(username:String,wishlistId:Long) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
def deleteWishlist(username:String,wishlistId:Long) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
Logger.info("Deleting wishlist: " + wishlistId)

wishlist.delete
Expand All @@ -123,20 +123,20 @@ object WishController extends Controller with Secured {
def showWishlist(username:String,wishlistId:Long) = withWishlist(username,wishlistId) { wishlist => implicit request =>
val wishes = Wishlist.findWishesForWishlist(wishlist)
val gravatarUrl = recipientGravatarUrl(wishlist)
findCurrentRecipient match {
findCurrentRecipient match {
case Some(recipient) => {
if( recipient.canEdit(wishlist) ) {
Ok(views.html.wishlist.showeditwishlist(wishlist,wishes,simpleAddWishForm,gravatarUrl))
} else {
Ok(views.html.wishlist.showwishlist(wishlist,wishes,simpleAddWishForm,gravatarUrl))
Ok(views.html.wishlist.showwishlist(wishlist,wishes,simpleAddWishForm,gravatarUrl))
}
}
case None => Ok(views.html.wishlist.showwishlist(wishlist,wishes,simpleAddWishForm,gravatarUrl))
case None => Ok(views.html.wishlist.showwishlist(wishlist,wishes,simpleAddWishForm,gravatarUrl))
}
}


def showConfirmDeleteWishlist(username:String,wishlistId:Long) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
def showConfirmDeleteWishlist(username:String,wishlistId:Long) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
Ok(views.html.wishlist.deletewishlist(wishlist))
}

Expand All @@ -146,37 +146,37 @@ object WishController extends Controller with Secured {
errors => {
Logger.warn("Update failed: " + errors)
BadRequest
},
},
term => {
val wishlists = term match {
case None => Wishlist.findAll
case Some(searchTerm) => Wishlist.searchForWishlistsContaining(searchTerm)
}
Ok(views.html.wishlist.listwishlists(wishlists,searchForm.fill(term),editWishlistForm))
}
)
)
}

private def recipientGravatarUrl(wishlist:Wishlist) = RecipientController.gravatarUrl(wishlist.recipient)


def addWishToWishlist(username:String,wishlistId:Long) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
def addWishToWishlist(username:String,wishlistId:Long) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
simpleAddWishForm.bindFromRequest.fold(
errors => {
Logger.warn("Add failed: " + errors)
val wishes = wishlist.findWishes
BadRequest(views.html.wishlist.showwishlist(wishlist,wishes,errors,recipientGravatarUrl(wishlist)))
},
},
title => {
val wish = new Wish(title,None,currentRecipient).save
wish.addToWishlist(wishlist)
Redirect(routes.WishController.showWishlist(username,wishlistId)).flashing("messageSuccess" -> "Wish added")
}
)
}
)
}


def removeWishFromWishlist(username:String,wishlistId:Long,wishId:Long) = isRecipientOfWish(username,wishlistId,wishId) { (wish,wishlist,currentRecipient) => implicit request =>
def removeWishFromWishlist(username:String,wishlistId:Long,wishId:Long) = isEditorOfWish(username,wishlistId,wishId) { (wish,wishlist,currentRecipient) => implicit request =>

wishlist.removeWish(wish)

Expand All @@ -185,7 +185,7 @@ object WishController extends Controller with Secured {



def updateWish(username:String,wishlistId:Long,wishId:Long) = isRecipientOfWish(username,wishlistId,wishId) { (wish,wishlist,currentRecipient) => implicit request =>
def updateWish(username:String,wishlistId:Long,wishId:Long) = isEditorOfWish(username,wishlistId,wishId) { (wish,wishlist,currentRecipient) => implicit request =>
editWishForm.bindFromRequest.fold(
errors => {
Logger.warn("Update failed: " + errors)
Expand All @@ -204,27 +204,27 @@ object WishController extends Controller with Secured {



def updateWishlistOrder(username:String,wishlistId:Long) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
def updateWishlistOrder(username:String,wishlistId:Long) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>

// val wishes = Wishlist.findWishesForWishlist(wishlist)
updateWishlistOrderForm.bindFromRequest.fold(
errors => {
Logger.warn("Update order failed: " + errors)
Redirect(routes.WishController.showWishlist(username,wishlistId)).flashing("messageError" -> "Order update failed")
},
},
listOrder => {
Logger.info("Updating wishlist's order: " + wishlistId)

var ordinalCount = 1;
listOrder.split(",") map { wishId =>
listOrder.split(",") map { wishId =>
WishEntry.findByIds(wishId.toInt,wishlistId) map { wishentry =>
wishentry.copy(ordinal=Some(ordinalCount)).update
ordinalCount += 1
}
}

Redirect(routes.WishController.showWishlist(username,wishlistId)).flashing("message" -> "Wishlist updated")

}
)
}
Expand Down Expand Up @@ -259,43 +259,48 @@ object WishController extends Controller with Secured {
}


def addOrganiserToWishlist(username:String,wishlistId:Long) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
def addOrganiserToWishlist(username:String,wishlistId:Long) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
addOrganiserForm.bindFromRequest.fold(
errors => {
Logger.warn("Add failed: " + errors)
val editForm = editWishlistForm.fill((wishlist.title,wishlist.description))
val organisers = wishlist.findOrganisers
BadRequest(views.html.wishlist.editwishlist(wishlist,editForm,organisers,errors))
},
BadRequest(views.html.wishlist.editwishlist(wishlist,editForm,organisers,errors))
},
organiserUsername => {
Recipient.findByUsername(organiserUsername._3) match {
Recipient.findByUsername(organiserUsername._3) match {
case Some(organiser) => {
Logger.info("Adding organiser %s to wishlist %d".format(organiser.username,wishlistId))

wishlist.addOrganiser(organiser)
wishlist.addOrganiser(organiser)

Redirect(routes.WishController.showEditWishlist(username,wishlistId)).flashing("messageSuccess" -> "Organiser added")
}
case None => NotFound(views.html.error.notfound())
}
}
)
}
}
)
}




def removeOrganiserFromWishlist(username:String,wishlistId:Long,organiserUsername:String) = isRecipientOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
def removeOrganiserFromWishlist(username:String,wishlistId:Long,organiserUsername:String) = isEditorOfWishlist(username,wishlistId) { (wishlist,currentRecipient) => implicit request =>
Logger.info("Removing organiser %s from wishlist %d".format(organiserUsername,wishlistId))
Recipient.findByUsername(organiserUsername) match {
Recipient.findByUsername(organiserUsername) match {
case Some(organiser) => {

wishlist.removeOrganiser(organiser)

Redirect(routes.WishController.showEditWishlist(username,wishlistId)).flashing("messageRemoved" -> "Organiser removed")
}
case None => NotFound(views.html.error.notfound())
}
}
}



def addLinkToWish(username:String, wishlistId:Long, wishId:Long) = TODO
def deleteLinkFromWish(username:String, wishlistId:Long, wishId:Long, linkId:Long) = TODO

}
24 changes: 13 additions & 11 deletions app/views/wishlist/showeditwishlist.scala.html
Expand Up @@ -6,7 +6,7 @@
@nautical(wishlist.title) {
<li><a href="@routes.WishController.showWishlist(wishlist.recipient.username,wishlist.wishlistId.get)">wishlist</a></li>
}{

<div id="view-wishlist-page">


Expand All @@ -20,12 +20,12 @@


<div class="well" >

<img class="gravatar img-rounded" src="@gravatarUrl" title="Avatar of @wishlist.recipient.username from Gravatar.com"/>

<h3>@wishlist.title</h3>


<p>A wishlist by <a href="@routes.RecipientController.showProfile(wishlist.recipient.username)">@wishlist.recipient.username</a></p>

<p>@wishlist.description</p>
Expand All @@ -41,15 +41,15 @@ <h3>@wishlist.title</h3>

<h4>Wishes</h4>

<ul id="wish-list" class="unstyled sortable">
<ul id="wish-list" class="unstyled sortable">
@for(wish <- wishes){
@showwish(wishlist,wish,true)
}
}
</ul>

</div>
@for(wish <- wishes){
@wishmodal(wishlist,wish)
@wisheditmodal(wishlist,wish)
}

}
Expand Down Expand Up @@ -92,14 +92,16 @@ <h4>Wishes</h4>
$(".sortable")
.sortable({
placeholder: "ui-state-highlight",
update: function(event,ui){
var serialisedData = $('#wish-list').sortable('toArray').join().replace(/wish-row-/g,"");
update: function(event,ui){
var serialisedData = $('#wish-list').sortable('toArray').join().replace(/wish-row-/g,"");
$.post('@routes.WishController.updateWishlistOrder(wishlist.recipient.username,wishlist.wishlistId.get)', 'order='+serialisedData);
}
});
$(".sortable").disableSelection();
$(".sortable").disableSelection();
$("ul#wish-list li:odd").addClass("list-row-odd");
$("ul#wish-list li:even").addClass("list-row-even");
//$(".modal-links li:odd").addClass("list-row-odd");
//$(".modal-links li:even").addClass("list-row-even");
$("ul#wish-list li")
.mouseover(function(e){
$(this).addClass("list-row-hover");
Expand All @@ -109,7 +111,7 @@ <h4>Wishes</h4>
});
});
</script>

</div>


Expand Down

0 comments on commit 5f94ff1

Please sign in to comment.