summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <Kjetil Ørbekk orbekk@pvv.ntnu.no>2010-09-03 14:20:40 -0400
committerKjetil Orbekk <Kjetil Ørbekk orbekk@pvv.ntnu.no>2010-09-03 14:20:40 -0400
commitac9d7b3012496f411f7ba6c19d31c9496b44327f (patch)
tree28742e4192fd72cc10c024a730d00e5fbea5b1b7
parentd5ab045ec80a10b341db2ac5a591d84741ea846a (diff)
Add approve functionality.
-rw-r--r--html/approve.jsp81
-rw-r--r--src/lq/QuoteUtils.java50
-rw-r--r--src/lq/UserUtil.java33
3 files changed, 164 insertions, 0 deletions
diff --git a/html/approve.jsp b/html/approve.jsp
new file mode 100644
index 0000000..b0455dc
--- /dev/null
+++ b/html/approve.jsp
@@ -0,0 +1,81 @@
+<%@ page contentType="text/html; charset=UTF-8"
+ import="java.util.List"
+%>
+<html>
+<head>
+<meta name="robots" content="noindex, nofollow" />
+<title>Quote approval</title>
+<style type="text/css">
+body {font-family: monospace;}
+hr {
+ border-style: solid;
+ border-color: black;
+ border-width: 1px;
+}
+</style>
+</head>
+<body bgcolor="#FFFFFF" text="#000000" link="#000000" vlink="#000000">
+
+<%
+if (lq.UserUtil.isAuthenticated()) {
+ out.println("Logget inn som: " + lq.UserUtil.getAuthenticatedEmail() + "<br>");
+
+ if (!lq.Strings.nullOrEmpty(request.getParameter("id"))) {
+ String action = request.getParameter("action");
+ if (action != null && (action.equals("approve") || action.equals("reject"))) {
+ try {
+ Long id = Long.parseLong(request.getParameter("id"));
+ if (action.equals("approve")) {
+ lq.QuoteUtils.approveQuote(id);
+ out.println("Godkjente quote #" + id);
+ }
+ else {
+ lq.QuoteUtils.rejectQuote(id);
+ out.println("Avviste quote #" + id);
+ }
+ }
+ catch (NumberFormatException e) {
+ out.println("lmao for en gimp hax<br>");
+ }
+ }
+ else {
+ out.println("Feilkode π/3");
+ }
+ out.println("<p><p>");
+ }
+
+ List<lq.Quote> pendingQuotes = lq.QuoteUtils.getQuotesPendingApproval();
+
+ for (lq.Quote quote : pendingQuotes) {
+ String nick = lq.Strings.escape(quote.getAuthor());
+ String timestamp = lq.DateUtil.timestampFormat.format(quote.getTimestamp());
+ String date = lq.DateUtil.dateFormat.format(quote.getQuoteDate());
+ String content = lq.Strings.escape(quote.getContent());
+
+ out.println("<br><pre>");
+ out.println("Fra " + quote.getIp() + ", " + timestamp);
+ out.println("Nick: " + nick);
+ out.println("Date: " + date);
+ out.println();
+ out.println(content);
+ out.println("</pre>");
+ out.println("<a href=\"approve.jsp?"
+ + "id=" + quote.getId() + "&"
+ + "action=reject\">avvis</a>");
+ out.println(", ");
+ out.println("<a href=\"approve.jsp?"
+ + "id=" + quote.getId() + "&"
+ + "action=approve\">godkjenn</a>");
+ out.println("<hr>");
+ }
+
+ String logoutUrl = lq.UserUtil.getLogoutUrl(request.getRequestURI());
+ out.println("<a href=\"" + logoutUrl + "\">Logg ut</a>");
+}
+else {
+ String loginUrl = lq.UserUtil.getLoginUrl(request.getRequestURI());
+ out.println("<a href=\"" + loginUrl + "\">Logg inn</a>");
+}
+%>
+</body>
+</html>
diff --git a/src/lq/QuoteUtils.java b/src/lq/QuoteUtils.java
index aaa14b9..ab5cdd1 100644
--- a/src/lq/QuoteUtils.java
+++ b/src/lq/QuoteUtils.java
@@ -21,6 +21,20 @@ public class QuoteUtils {
}
}
+ public static List<Quote> getQuotesPendingApproval() {
+ PersistenceManager pm = PMF.get().getPersistenceManager();
+ try {
+ Query quoteQuery = pm.newQuery(Quote.class);
+ quoteQuery.setFilter("approved == null");
+ List<Quote> quotes = (List<Quote>) quoteQuery.execute();
+ pm.retrieveAll(quotes);
+ return quotes;
+ }
+ finally {
+ pm.close();
+ }
+ }
+
public static List<Quote> getQuotesOrderedByIdDesc() {
List<Quote> quotes = getQuotes();
Collections.sort(quotes,
@@ -53,4 +67,40 @@ public class QuoteUtils {
});
return quotes;
}
+
+ public static void approveQuote(Long id) {
+ PersistenceManager pm = PMF.get().getPersistenceManager();
+ try {
+ Query quoteQuery = pm.newQuery(Quote.class);
+ quoteQuery.setFilter("id == idParam");
+ quoteQuery.declareParameters("Long idParam");
+ List<Quote> quotes = (List<Quote>) quoteQuery.execute(id);
+
+ for (Quote quote : quotes) {
+ quote.setApproved(true);
+ pm.makePersistent(quote);
+ }
+ }
+ finally {
+ pm.close();
+ }
+ }
+
+ public static void rejectQuote(Long id) {
+ PersistenceManager pm = PMF.get().getPersistenceManager();
+ try {
+ Query quoteQuery = pm.newQuery(Quote.class);
+ quoteQuery.setFilter("id == idParam");
+ quoteQuery.declareParameters("Long idParam");
+ List<Quote> quotes = (List<Quote>) quoteQuery.execute(id);
+
+ for (Quote quote : quotes) {
+ quote.setApproved(false);
+ pm.makePersistent(quote);
+ }
+ }
+ finally {
+ pm.close();
+ }
+ }
}
diff --git a/src/lq/UserUtil.java b/src/lq/UserUtil.java
new file mode 100644
index 0000000..4fd9452
--- /dev/null
+++ b/src/lq/UserUtil.java
@@ -0,0 +1,33 @@
+package lq;
+
+import javax.servlet.http.HttpServletRequest;
+import com.google.appengine.api.users.User;
+import com.google.appengine.api.users.UserService;
+import com.google.appengine.api.users.UserServiceFactory;
+
+public class UserUtil {
+ public static final String getAuthenticatedEmail() {
+ UserService userService = UserServiceFactory.getUserService();
+ User user = userService.getCurrentUser();
+ if (user == null) {
+ return null;
+ }
+ else {
+ return user.getEmail();
+ }
+ }
+
+ public static final boolean isAuthenticated() {
+ return getAuthenticatedEmail() != null;
+ }
+
+ public static final String getLoginUrl(String requestUrl) {
+ UserService userService = UserServiceFactory.getUserService();
+ return userService.createLoginURL(requestUrl);
+ }
+
+ public static final String getLogoutUrl(String requestUrl) {
+ UserService userService = UserServiceFactory.getUserService();
+ return userService.createLogoutURL(requestUrl);
+ }
+}