TaskFlow/views/views.go

346 lines
8.9 KiB
Go
Raw Normal View History

2015-11-21 15:58:23 +08:00
package views
import (
"bufio"
"github.com/thewhitetulip/Tasks/db"
"io/ioutil"
2016-01-10 21:57:41 +08:00
"log"
2015-11-21 15:58:23 +08:00
"net/http"
"os"
"strconv"
"strings"
"text/template"
2016-01-18 10:28:08 +08:00
"time"
"io"
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
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
//PopulateTemplates is used to parse all templates present in
//the templates folder
func PopulateTemplates() {
var allFiles []string
templatesDir := "./public/templates/"
files, err := ioutil.ReadDir(templatesDir)
2015-11-21 21:20:51 +08:00
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println("Error reading template dir")
2015-11-21 21:20:51 +08:00
}
2015-11-21 15:58:23 +08:00
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".html") {
allFiles = append(allFiles, templatesDir+filename)
}
}
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2015-11-22 11:51:29 +08:00
os.Exit(1)
2015-11-21 15:58:23 +08:00
}
templates, err = template.ParseFiles(allFiles...)
2015-11-21 21:20:51 +08:00
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2015-11-21 22:12:44 +08:00
os.Exit(1)
2015-11-21 21:20:51 +08:00
}
2015-11-21 15:58:23 +08:00
homeTemplate = templates.Lookup("home.html")
deletedTemplate = templates.Lookup("deleted.html")
editTemplate = templates.Lookup("edit.html")
searchTemplate = templates.Lookup("search.html")
completedTemplate = templates.Lookup("completed.html")
}
//ShowAllTasksFunc is used to handle the "/" URL which is the default ons
//TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
context := db.GetTasks("pending") //true when you want non deleted notes
2015-11-22 11:51:29 +08:00
if message != "" {
context.Message = message
}
2016-01-18 10:28:08 +08:00
context.CSRFToken = "abcd"
2015-11-22 11:51:29 +08:00
message = ""
2016-01-18 09:02:15 +08:00
expiration := time.Now().Add(365 * 24 * time.Hour)
2016-01-18 10:28:08 +08:00
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
2016-01-18 09:02:15 +08:00
http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context)
2015-11-22 11:51:29 +08:00
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
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) {
if r.Method == "GET" {
2015-11-21 21:20:51 +08:00
context := db.GetTasks("deleted") //false when you want deleted notes
2015-11-22 11:51:29 +08:00
if message != "" {
context.Message = message
message = ""
}
2015-11-21 15:58:23 +08:00
deletedTemplate.Execute(w, context)
2015-11-22 11:51:29 +08:00
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//SearchTaskFunc is used to handle the /search/ url, handles the search function
func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
query := r.Form.Get("query")
context := db.SearchTask(query)
searchTemplate.Execute(w, context)
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//AddTaskFunc is used to handle the addition of new task, "/add" URL
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
2016-01-18 10:28:08 +08:00
if r.Method == "POST" { // Will work only for POST requests, will redirect to home
2015-11-21 15:58:23 +08:00
r.ParseForm()
2016-01-18 10:28:08 +08:00
file, handler, err := r.FormFile("uploadfile")
if err != nil {
log.Println(err)
return
}
2016-01-18 09:02:15 +08:00
title := template.HTMLEscapeString(r.Form.Get("title"))
content := template.HTMLEscapeString(r.Form.Get("content"))
formToken := template.HTMLEscapeString(r.Form.Get("CSRFToken"))
2016-01-18 10:28:08 +08:00
cookie, _ := r.Cookie("csrftoken")
if formToken == cookie.Value {
if handler != nil {
r.ParseMultipartForm(32 << 20) //defined maximum size of file
defer file.Close()
f, err := os.OpenFile("./files/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
filelink := "<br> <a href=./files/"+handler.Filename+">"+ handler.Filename+"</a>"
content = content + filelink
}
2016-01-18 09:02:15 +08:00
truth := db.AddTask(title, content)
if truth != nil {
message = "Error adding task"
2016-01-18 10:28:08 +08:00
log.Println("error adding task to db")
2016-01-18 09:02:15 +08:00
} else {
message = "Task added"
2016-01-18 10:28:08 +08:00
log.Println("added task to db")
2016-01-18 09:02:15 +08:00
}
http.Redirect(w, r, "/", http.StatusFound)
2016-01-18 10:28:08 +08:00
} else {
log.Fatal("CSRF mismatch")
2015-11-21 15:58:23 +08:00
}
2016-01-18 09:02:15 +08:00
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2015-11-21 21:20:51 +08:00
context := db.GetTasks("completed") //false when you want finished notes
2015-11-21 15:58:23 +08:00
completedTemplate.Execute(w, context)
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
2015-11-21 19:38:08 +08:00
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2015-11-21 19:38:08 +08:00
} else {
task := db.GetTaskByID(id)
editTemplate.Execute(w, task)
2015-11-21 21:20:51 +08:00
}
2015-11-21 15:58:23 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url
func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2015-11-21 15:58:23 +08:00
} else {
2015-11-22 11:51:29 +08:00
err = db.CompleteTask(id)
2015-11-21 15:58:23 +08:00
if err != nil {
2015-11-22 11:51:29 +08:00
message = "Complete task failed"
} else {
message = "Task marked complete"
2015-11-21 15:58:23 +08:00
}
http.Redirect(w, r, "/", http.StatusFound)
}
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
2015-11-22 11:51:29 +08:00
//DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete
2015-11-21 15:58:23 +08:00
func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id := r.URL.Path[len("/delete/"):]
if id == "all" {
db.DeleteAll()
http.Redirect(w, r, "/", http.StatusFound)
} else {
id, err := strconv.Atoi(id)
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2015-11-21 15:58:23 +08:00
} else {
2015-11-21 22:12:44 +08:00
err = db.DeleteTask(id)
if err != nil {
2015-11-22 11:51:29 +08:00
message = "Error deleting task"
} else {
message = "Task deleted"
2015-11-21 22:12:44 +08:00
}
2015-11-22 11:51:29 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
2015-11-22 11:51:29 +08:00
//TrashTaskFunc is used to populate the trash tasks
2015-11-21 15:58:23 +08:00
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
2015-11-21 19:38:08 +08:00
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2015-11-21 19:38:08 +08:00
} else {
2015-11-21 22:12:44 +08:00
err = db.TrashTask(id)
if err != nil {
2015-11-22 11:51:29 +08:00
message = "Error trashing task"
} else {
message = "Task trashed"
2015-11-21 22:12:44 +08:00
}
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
}
2015-11-21 15:58:23 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
http.Redirect(w, r, "/trash", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
2015-11-21 19:38:08 +08:00
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
err = db.RestoreTask(id)
if err != nil {
message = "Restore failed"
} else {
message = "Task restored"
}
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/deleted/", http.StatusFound)
}
2015-11-21 15:58:23 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
2016-01-09 13:03:35 +08:00
//RestoreFromCompleteFunc restores the task from complete to pending
func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2016-01-09 13:03:35 +08:00
} else {
err = db.RestoreTaskFromComplete(id)
if err != nil {
message = "Restore failed"
} else {
message = "Task restored"
}
http.Redirect(w, r, "/pending/", http.StatusFound)
}
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
}
}
2015-11-21 15:58:23 +08:00
//UpdateTaskFunc is used to update a task, handes "/update/" URL
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
2015-11-21 21:20:51 +08:00
if r.Method == "POST" {
2015-11-21 19:38:08 +08:00
r.ParseForm()
id, err := strconv.Atoi(r.Form.Get("id"))
if err != nil {
2016-01-09 13:18:11 +08:00
log.Println(err)
2015-11-21 19:38:08 +08:00
}
title := r.Form.Get("title")
content := r.Form.Get("content")
2015-11-21 22:12:44 +08:00
err = db.UpdateTask(id, title, content)
if err != nil {
2015-11-22 11:51:29 +08:00
message = "Error updating task"
} else {
message = "Task updated"
2015-11-21 22:12:44 +08:00
}
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-22 11:51:29 +08:00
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//ServeStaticFunc is used to serve static files
//TODO: replace this with the http.FileServer
func ServeStaticFunc(w http.ResponseWriter, r *http.Request) {
path := "./public" + r.URL.Path
var contentType string
if strings.HasSuffix(path, ".css") {
contentType = "text/css"
} else if strings.HasSuffix(path, ".png") {
contentType = "image/png"
2016-01-07 22:55:56 +08:00
} else if strings.HasSuffix(path, ".js") {
2015-11-21 15:58:23 +08:00
contentType = "application/javascript"
} else {
contentType = "plain/text"
}
f, err := os.Open(path)
if err == nil {
defer f.Close()
w.Header().Add("Content Type", contentType)
br := bufio.NewReader(f)
br.WriteTo(w)
} else {
w.WriteHeader(404)
}
}