2015-11-21 13:28:23 +05:30
|
|
|
package views
|
|
|
|
|
2016-02-01 20:04:01 +05:30
|
|
|
/*Holds the fetch related view handlers*/
|
|
|
|
|
2015-11-21 13:28:23 +05:30
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"text/template"
|
2016-01-18 07:58:08 +05:30
|
|
|
"time"
|
2016-02-01 19:15:04 +05:30
|
|
|
|
|
|
|
"github.com/thewhitetulip/Tasks/db"
|
2016-05-09 07:41:05 +05:30
|
|
|
"github.com/thewhitetulip/Tasks/sessions"
|
2015-11-21 13:28:23 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var homeTemplate *template.Template
|
|
|
|
var deletedTemplate *template.Template
|
|
|
|
var completedTemplate *template.Template
|
|
|
|
var editTemplate *template.Template
|
|
|
|
var searchTemplate *template.Template
|
|
|
|
var templates *template.Template
|
2016-05-09 07:41:05 +05:30
|
|
|
var loginTemplate *template.Template
|
|
|
|
|
2015-11-22 09:21:29 +05:30
|
|
|
var message string //message will store the message to be shown as notification
|
2015-11-21 13:28:23 +05:30
|
|
|
var err error
|
|
|
|
|
|
|
|
//ShowAllTasksFunc is used to handle the "/" URL which is the default ons
|
|
|
|
//TODO add http404 error
|
|
|
|
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-09 07:41:05 +05:30
|
|
|
if r.Method == "GET" && sessions.IsLoggedIn(r) {
|
2016-02-02 23:10:44 +05:30
|
|
|
context, err := db.GetTasks("pending", "")
|
|
|
|
categories := db.GetCategories()
|
2016-02-01 19:15:04 +05:30
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
}
|
2015-11-22 09:21:29 +05:30
|
|
|
if message != "" {
|
|
|
|
context.Message = message
|
|
|
|
}
|
2016-01-18 07:58:08 +05:30
|
|
|
context.CSRFToken = "abcd"
|
2016-02-02 23:10:44 +05:30
|
|
|
context.Categories = categories
|
2015-11-22 09:21:29 +05:30
|
|
|
message = ""
|
2016-01-18 06:32:15 +05:30
|
|
|
expiration := time.Now().Add(365 * 24 * time.Hour)
|
2016-01-18 07:58:08 +05:30
|
|
|
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
|
2016-01-18 06:32:15 +05:30
|
|
|
http.SetCookie(w, &cookie)
|
|
|
|
homeTemplate.Execute(w, context)
|
2015-11-22 09:21:29 +05:30
|
|
|
} else {
|
|
|
|
message = "Method not allowed"
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2015-11-21 13:28:23 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
|
|
|
|
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-09 07:41:05 +05:30
|
|
|
if r.Method == "GET" && sessions.IsLoggedIn(r) {
|
2016-02-02 23:10:44 +05:30
|
|
|
context, err := db.GetTasks("deleted", "")
|
2016-02-06 12:39:38 +05:30
|
|
|
categories := db.GetCategories()
|
|
|
|
context.Categories = categories
|
2016-02-01 19:15:04 +05:30
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
|
|
|
|
}
|
2015-11-22 09:21:29 +05:30
|
|
|
if message != "" {
|
|
|
|
context.Message = message
|
|
|
|
message = ""
|
|
|
|
}
|
2015-11-21 13:28:23 +05:30
|
|
|
deletedTemplate.Execute(w, context)
|
2015-11-22 09:21:29 +05:30
|
|
|
} else {
|
|
|
|
message = "Method not allowed"
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2015-11-21 13:28:23 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
|
|
|
|
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-09 07:41:05 +05:30
|
|
|
if r.Method == "GET" && sessions.IsLoggedIn(r) {
|
2016-02-02 23:10:44 +05:30
|
|
|
context, err := db.GetTasks("completed", "")
|
2016-02-06 12:39:38 +05:30
|
|
|
categories := db.GetCategories()
|
|
|
|
context.Categories = categories
|
2016-02-01 19:15:04 +05:30
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
|
|
|
|
}
|
2015-11-21 13:28:23 +05:30
|
|
|
completedTemplate.Execute(w, context)
|
2015-11-21 17:08:08 +05:30
|
|
|
} else {
|
2015-11-22 09:21:29 +05:30
|
|
|
message = "Method not allowed"
|
2015-11-21 17:08:08 +05:30
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2015-11-21 13:28:23 +05:30
|
|
|
}
|
|
|
|
}
|
2016-02-02 23:10:44 +05:30
|
|
|
|
|
|
|
//ShowCategoryFunc will populate the /category/<id> URL which shows all the tasks related
|
|
|
|
// to that particular category
|
|
|
|
func ShowCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-09 07:41:05 +05:30
|
|
|
if r.Method == "GET" && sessions.IsLoggedIn(r) {
|
2016-02-02 23:10:44 +05:30
|
|
|
category := r.URL.Path[len("/category/"):]
|
|
|
|
context, err := db.GetTasks("", category)
|
|
|
|
categories := db.GetCategories()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
if message != "" {
|
|
|
|
context.Message = message
|
|
|
|
}
|
|
|
|
context.CSRFToken = "abcd"
|
|
|
|
context.Categories = categories
|
|
|
|
message = ""
|
|
|
|
expiration := time.Now().Add(365 * 24 * time.Hour)
|
|
|
|
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
|
|
|
|
http.SetCookie(w, &cookie)
|
|
|
|
homeTemplate.Execute(w, context)
|
|
|
|
} else {
|
|
|
|
message = "Method not allowed"
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|