Tasks/views/addViews.go

219 lines
6.0 KiB
Go
Raw Permalink Normal View History

2016-01-31 14:20:01 +08:00
package views
2016-02-01 22:34:19 +08:00
/*
Holds the insert task related view handlers, includes the one for file upload
*/
2016-01-31 14:20:01 +08:00
import (
"crypto/md5"
2016-01-31 22:22:00 +08:00
"fmt"
2016-01-31 14:20:01 +08:00
"io"
2016-01-31 22:22:00 +08:00
"log"
"net/http"
"os"
"strconv"
2016-02-06 14:58:00 +08:00
"strings"
2016-01-31 22:22:00 +08:00
"text/template"
"time"
2016-02-01 21:45:04 +08:00
"github.com/thewhitetulip/Tasks/db"
2016-05-14 15:26:24 +08:00
"github.com/thewhitetulip/Tasks/sessions"
2016-02-06 18:02:46 +08:00
"github.com/thewhitetulip/Tasks/utils"
2016-01-31 14:20:01 +08:00
)
// UploadedFileHandler is used to handle the uploaded file related requests
func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
2016-09-22 00:40:46 +08:00
if r.Method != "GET" {
http.Redirect(w, r, "/", http.StatusBadRequest)
return
2016-01-31 14:20:01 +08:00
}
2016-09-22 00:40:46 +08:00
token := r.URL.Path[len("/files/"):]
//file, err := db.GetFileName(token)
//if err != nil {
log.Println("serving file ./files/" + token)
http.ServeFile(w, r, "./files/"+token)
//}
2016-01-31 14:20:01 +08:00
}
//AddTaskFunc is used to handle the addition of new task, "/add" URL
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
2016-09-22 00:40:46 +08:00
if r.Method != "POST" { // Will work only for POST requests, will redirect to home
http.Redirect(w, r, "/", http.StatusBadRequest)
return
}
var filelink string // will store the html when we have files to be uploaded, appened to the note content
r.ParseForm()
file, handler, err := r.FormFile("uploadfile")
if err != nil && handler != nil {
//Case executed when file is uploaded and yet an error occurs
log.Println(err)
message = "Error uploading file"
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
2016-01-31 14:20:01 +08:00
2016-09-22 00:40:46 +08:00
taskPriority, priorityErr := strconv.Atoi(r.FormValue("priority"))
2016-02-02 23:38:35 +08:00
2016-09-22 00:40:46 +08:00
if priorityErr != nil {
log.Print(priorityErr)
message = "Bad task priority"
}
priorityList := []int{1, 2, 3}
found := false
for _, priority := range priorityList {
if taskPriority == priority {
found = true
2016-05-12 01:19:32 +08:00
}
2016-09-22 00:40:46 +08:00
}
//If someone gives us incorrect priority number, we give the priority
//to that task as 1 i.e. Low
if !found {
taskPriority = 1
}
var hidden int
hideTimeline := r.FormValue("hide")
if hideTimeline != "" {
hidden = 1
} else {
hidden = 0
}
// dueDate := r.FormValue("dueDate")
category := r.FormValue("category")
title := template.HTMLEscapeString(r.Form.Get("title"))
content := template.HTMLEscapeString(r.Form.Get("content"))
formToken := template.HTMLEscapeString(r.Form.Get("CSRFToken"))
cookie, _ := r.Cookie("csrftoken")
if formToken == cookie.Value {
username := sessions.GetCurrentUserName(r)
if handler != nil {
// this will be executed whenever a file is uploaded
r.ParseMultipartForm(32 << 20) //defined maximum size of file
defer file.Close()
htmlFilename := strings.Replace(handler.Filename, " ", "-", -1)
2016-09-22 00:40:46 +08:00
randomFileName := md5.New()
io.WriteString(randomFileName, strconv.FormatInt(time.Now().Unix(), 10))
io.WriteString(randomFileName, htmlFilename)
2016-09-22 00:40:46 +08:00
token := fmt.Sprintf("%x", randomFileName.Sum(nil))
f, err := os.OpenFile("./files/"+htmlFilename, os.O_WRONLY|os.O_CREATE, 0666)
2016-09-22 00:40:46 +08:00
if err != nil {
log.Println(err)
return
}
2016-09-22 00:40:46 +08:00
defer f.Close()
io.Copy(f, file)
if strings.HasSuffix(htmlFilename, ".png") || strings.HasSuffix(htmlFilename, ".jpg") {
filelink = "<br> <img src='/files/" + htmlFilename + "'/>"
2016-09-22 00:40:46 +08:00
} else {
filelink = "<br> <a href=/files/" + htmlFilename + ">" + htmlFilename + "</a>"
2016-01-31 14:20:01 +08:00
}
2016-09-22 00:40:46 +08:00
content = content + filelink
fileTruth := db.AddFile(htmlFilename, token, username)
2016-09-22 00:40:46 +08:00
if fileTruth != nil {
message = "Error adding filename in db"
2016-05-12 01:19:32 +08:00
log.Println("error adding task to db")
}
2016-09-22 00:40:46 +08:00
}
//taskTruth := db.AddTask(title, content, category, taskPriority, username, dueDate)
taskTruth := db.AddTask(title, content, category, taskPriority, username, hidden)
if taskTruth != nil {
message = "Error adding task"
log.Println("error adding task to db")
2016-05-12 01:19:32 +08:00
http.Redirect(w, r, "/", http.StatusInternalServerError)
2016-09-22 00:40:46 +08:00
} else {
message = "Task added"
log.Println("added task to db")
http.Redirect(w, r, "/", http.StatusFound)
}
2016-09-22 00:40:46 +08:00
} else {
log.Println("CSRF mismatch")
message = "Server Error"
http.Redirect(w, r, "/", http.StatusInternalServerError)
2016-01-31 14:20:01 +08:00
}
2016-05-12 01:19:32 +08:00
2016-01-31 14:20:01 +08:00
}
2016-02-03 01:40:44 +08:00
//AddCategoryFunc used to add new categories to the database
func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
2016-09-22 00:40:46 +08:00
if r.Method != "POST" { // We respond only to POST requests, redirect to home for others
http.Redirect(w, r, "/", http.StatusBadRequest)
return
}
2016-05-12 01:19:32 +08:00
r.ParseForm()
category := r.Form.Get("category")
if strings.Trim(category, " ") != "" {
2016-05-14 15:26:24 +08:00
username := sessions.GetCurrentUserName(r)
log.Println("adding category")
err := db.AddCategory(username, category)
if err != nil {
2016-05-12 01:19:32 +08:00
message = "Error adding category"
http.Redirect(w, r, "/", http.StatusBadRequest)
} else {
message = "Added category"
http.Redirect(w, r, "/", http.StatusFound)
2016-02-03 01:40:44 +08:00
}
}
}
2016-02-05 02:36:00 +08:00
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
2016-08-30 23:30:26 +08:00
if r.Method != "GET" {
http.Redirect(w, r, "/", http.StatusBadRequest)
return
}
2016-08-30 23:30:26 +08:00
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil {
log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
return
2016-09-22 00:40:46 +08:00
}
redirectURL := utils.GetRedirectUrl(r.Referer())
username := sessions.GetCurrentUserName(r)
task, err := db.GetTaskByID(username, id)
categories := db.GetCategories(username)
task.Categories = categories
task.Referer = redirectURL
2016-08-30 23:30:26 +08:00
2016-09-22 00:40:46 +08:00
if err != nil {
task.Message = "Error fetching Tasks"
2016-02-05 02:36:00 +08:00
}
2016-09-22 00:40:46 +08:00
editTemplate.Execute(w, task)
2016-02-05 02:36:00 +08:00
}
//AddCommentFunc will be used
func AddCommentFunc(w http.ResponseWriter, r *http.Request) {
2016-08-30 23:30:26 +08:00
if r.Method != "POST" {
log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
return
}
r.ParseForm()
text := r.Form.Get("commentText")
id := r.Form.Get("taskID")
2016-08-30 23:30:26 +08:00
idInt, err := strconv.Atoi(id)
2016-08-30 23:30:26 +08:00
if (err != nil) || (text == "") {
log.Println("unable to convert into integer")
message = "Error adding comment"
} else {
username := sessions.GetCurrentUserName(r)
err = db.AddComments(username, idInt, text)
2016-08-30 23:30:26 +08:00
if err != nil {
log.Println("unable to insert into db")
message = "Comment not added"
} else {
message = "Comment added"
2016-05-12 01:19:32 +08:00
}
2016-08-30 23:30:26 +08:00
}
2016-08-30 23:30:26 +08:00
http.Redirect(w, r, "/", http.StatusFound)
}