Tasks/views/views.go

105 lines
3.1 KiB
Go
Raw Normal View History

2015-11-21 15:58:23 +08:00
package views
2016-02-01 22:34:01 +08:00
/*Holds the fetch related view handlers*/
2015-11-21 15:58:23 +08:00
import (
"net/http"
"text/template"
2016-01-18 10:28:08 +08:00
"time"
2016-02-01 21:45:04 +08:00
"github.com/thewhitetulip/Tasks/db"
2016-05-09 10:11:05 +08:00
"github.com/thewhitetulip/Tasks/sessions"
2015-11-21 15:58:23 +08:00
)
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 10:11:05 +08:00
var loginTemplate *template.Template
2015-11-22 11:51:29 +08:00
var message string //message will store the message to be shown as notification
2015-11-21 15:58:23 +08:00
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-12 01:19:32 +08:00
if r.Method == "GET" {
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
context, err := db.GetTasks(username, "pending", "")
categories := db.GetCategories(username)
2016-05-12 01:19:32 +08:00
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
} else {
if message != "" {
context.Message = message
}
2016-05-12 01:19:32 +08:00
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)
2016-02-01 21:45:04 +08:00
}
2015-11-21 15:58:23 +08:00
}
}
//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-12 01:19:32 +08:00
if r.Method == "GET" {
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
categories := db.GetCategories(username)
context, err := db.GetTasks(username, "deleted", "")
2016-05-12 01:19:32 +08:00
context.Categories = categories
if err != nil {
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
}
if message != "" {
context.Message = message
message = ""
2015-11-22 11:51:29 +08:00
}
2016-05-12 01:19:32 +08:00
deletedTemplate.Execute(w, context)
2015-11-21 15:58:23 +08:00
}
}
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
2016-05-12 01:19:32 +08:00
if r.Method == "GET" {
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
categories := db.GetCategories(username)
context, err := db.GetTasks(username, "completed", "")
2016-05-12 01:19:32 +08:00
context.Categories = categories
if err != nil {
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
2016-02-01 21:45:04 +08:00
}
2016-05-12 01:19:32 +08:00
completedTemplate.Execute(w, context)
2015-11-21 15:58:23 +08:00
}
}
2016-02-03 01:40:44 +08:00
//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-12 01:19:32 +08:00
if r.Method == "GET" && sessions.IsLoggedIn(r) {
category := r.URL.Path[len("/category/"):]
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
context, err := db.GetTasks(username, "", category)
categories := db.GetCategories(username)
2016-02-03 01:40:44 +08:00
2016-05-12 01:19:32 +08:00
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
if message != "" {
context.Message = message
2016-02-03 01:40:44 +08:00
}
2016-05-12 01:19:32 +08:00
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)
2016-02-03 01:40:44 +08:00
}
}