From 48f47310204df4cb1bdb5ee8db761ab942b20527 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 17 Oct 2016 14:30:30 -0700 Subject: adding initial updates to provide better time recording for post editing & creation --- system/api/handlers.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'system') diff --git a/system/api/handlers.go b/system/api/handlers.go index 5fb79bc..0c9139f 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -32,6 +32,8 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { // num := q.Get("num") // page := q.Get("page") + // TODO: inplement time-based ?after=time.Time, ?before=time.Time between=time.Time|time.Time + if t == "" { res.WriteHeader(http.StatusBadRequest) return -- cgit v1.2.3 From e82ae68634df85b06ff816525aa64a72ac9d8771 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 17 Oct 2016 15:04:29 -0700 Subject: adding partial time helper and hard coding date & time inputs --- system/admin/static/common/js/util.js | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'system') diff --git a/system/admin/static/common/js/util.js b/system/admin/static/common/js/util.js index d45d9e3..1e798d6 100644 --- a/system/admin/static/common/js/util.js +++ b/system/admin/static/common/js/util.js @@ -20,4 +20,15 @@ function replaceBadChars(text) { s = s.replace(/[\u02DC\u00A0]/g, " "); return s; +} + + +// Returns a local partial time based on unix timestamp +function getPartialTime(date) { + var parts = []; + parts.push(date.getHours()) + parts.push(date.getMinutes()) + parts.push(date.getSeconds()) + + return parts.join(":"); } \ No newline at end of file -- cgit v1.2.3 From 24fadac198c7a37ee5341ef1bcf8764eb6ce9314 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 17 Oct 2016 15:33:13 -0700 Subject: adding util for date and time parsing, adding js to set inputs to time and date values based on timestamp --- system/admin/static/common/js/util.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'system') diff --git a/system/admin/static/common/js/util.js b/system/admin/static/common/js/util.js index 1e798d6..40c9828 100644 --- a/system/admin/static/common/js/util.js +++ b/system/admin/static/common/js/util.js @@ -23,12 +23,34 @@ function replaceBadChars(text) { } -// Returns a local partial time based on unix timestamp -function getPartialTime(date) { +// Returns a local partial time based on unix timestamp (i.e. HH:MM:SS) +function getPartialTime(unix) { + var date = new Date(unix); var parts = []; - parts.push(date.getHours()) - parts.push(date.getMinutes()) - parts.push(date.getSeconds()) + parts.push(date.getHours()); + parts.push(date.getMinutes()); + parts.push(date.getSeconds()); return parts.join(":"); +} + +// Returns a local partial date based on unix timestamp (YYYY-MM-DD) +function getPartialDate(unix) { + var date = new Date(unix); + var parts = []; + parts.push(date.getFullYear()); + + var month = date.getMonth()+1; + if (month < 10) { + month = "0" + String(month); + } + parts.push(month); + + var day = date.getDate(); + if (day < 10) { + day = "0" + String(day); + } + parts.push(day); + + return parts.join("-"); } \ No newline at end of file -- cgit v1.2.3 From 884d364476565b1f94b1bdf3e85f92d7d8081f6c Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 17 Oct 2016 16:43:51 -0700 Subject: modifying form to support more browsers w/o input[type=time] --- system/admin/static/common/js/util.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'system') diff --git a/system/admin/static/common/js/util.js b/system/admin/static/common/js/util.js index 40c9828..cf47af4 100644 --- a/system/admin/static/common/js/util.js +++ b/system/admin/static/common/js/util.js @@ -23,34 +23,42 @@ function replaceBadChars(text) { } -// Returns a local partial time based on unix timestamp (i.e. HH:MM:SS) +// Returns a local partial time object based on unix timestamp function getPartialTime(unix) { var date = new Date(unix); - var parts = []; - parts.push(date.getHours()); - parts.push(date.getMinutes()); - parts.push(date.getSeconds()); + var t = {}; + var hours = date.getHours(); + if (hours < 10) { + hours = "0" + String(hours); + } + t.hh = hours; + + var minutes = date.getMinutes(); + if (minutes < 10) { + minutes = "0" + String(minutes); + } + t.mm = minutes; - return parts.join(":"); + return t; } -// Returns a local partial date based on unix timestamp (YYYY-MM-DD) +// Returns a local partial date object based on unix timestamp function getPartialDate(unix) { var date = new Date(unix); - var parts = []; - parts.push(date.getFullYear()); + var d = {}; + d.yyyy = date.getFullYear(); var month = date.getMonth()+1; if (month < 10) { month = "0" + String(month); } - parts.push(month); + d.mm = month; var day = date.getDate(); if (day < 10) { day = "0" + String(day); } - parts.push(day); + d.dd = day; - return parts.join("-"); + return d; } \ No newline at end of file -- cgit v1.2.3 From 6c6061400d641ddb347c208dc10c7e73bcebff6e Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 17 Oct 2016 17:02:55 -0700 Subject: updates to time and date format --- system/admin/static/common/js/util.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'system') diff --git a/system/admin/static/common/js/util.js b/system/admin/static/common/js/util.js index cf47af4..7bd821d 100644 --- a/system/admin/static/common/js/util.js +++ b/system/admin/static/common/js/util.js @@ -46,13 +46,10 @@ function getPartialTime(unix) { function getPartialDate(unix) { var date = new Date(unix); var d = {}; + d.yyyy = date.getFullYear(); - var month = date.getMonth()+1; - if (month < 10) { - month = "0" + String(month); - } - d.mm = month; + d.mm = date.getMonth()+1; var day = date.getDate(); if (day < 10) { -- cgit v1.2.3 From cc8efd51e4ffb9bbe0fa9fef4936cf31f7ff21c8 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 17 Oct 2016 17:14:40 -0700 Subject: updates to time utils --- system/admin/static/common/js/util.js | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'system') diff --git a/system/admin/static/common/js/util.js b/system/admin/static/common/js/util.js index 7bd821d..7f4c8ab 100644 --- a/system/admin/static/common/js/util.js +++ b/system/admin/static/common/js/util.js @@ -31,7 +31,16 @@ function getPartialTime(unix) { if (hours < 10) { hours = "0" + String(hours); } + t.hh = hours; + if (hours > 12) { + t.hh = hours - 12; + t.pd = "PM"; + } else if (hours === 12) { + t.pd = "PM"; + } else if (hours < 12) { + t.pd = "AM"; + } var minutes = date.getMinutes(); if (minutes < 10) { -- cgit v1.2.3 From 2a67b80c61a49b26c273a58d67c2cef9e7b549e7 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 17 Oct 2016 19:24:26 -0700 Subject: updating handler code to support new timestamp and updated fields --- system/admin/handlers.go | 15 ++++++-------- system/admin/upload.go | 54 ++++++++++++++---------------------------------- 2 files changed, 21 insertions(+), 48 deletions(-) (limited to 'system') diff --git a/system/admin/handlers.go b/system/admin/handlers.go index d492335..edf351d 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -435,21 +435,18 @@ func editHandler(res http.ResponseWriter, req *http.Request) { cid := req.FormValue("id") t := req.FormValue("type") ts := req.FormValue("timestamp") + up := req.FormValue("updated") // create a timestamp if one was not set - date := make(map[string]int) if ts == "" { - now := time.Now() - date["year"] = now.Year() - date["month"] = int(now.Month()) - date["day"] = now.Day() - - // create timestamp format 'yyyy-mm-dd' and set in PostForm for - // db insertion - ts = fmt.Sprintf("%d-%02d-%02d", date["year"], date["month"], date["day"]) + ts := fmt.Sprintf("%d", time.Now().Unix()*1000) req.PostForm.Set("timestamp", ts) } + if up == "" { + req.PostForm.Set("updated", ts) + } + urlPaths, err := storeFileUploads(req) if err != nil { fmt.Println(err) diff --git a/system/admin/upload.go b/system/admin/upload.go index a2cde4c..7f2a4fa 100644 --- a/system/admin/upload.go +++ b/system/admin/upload.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "strconv" - "strings" "time" ) @@ -17,45 +16,16 @@ func storeFileUploads(req *http.Request) (map[string]string, error) { return nil, fmt.Errorf("%s", err) } - ts := req.FormValue("timestamp") + ts := req.FormValue("timestamp") // timestamp in milliseconds since unix epoch - // To use for FormValue name:urlPath - urlPaths := make(map[string]string) - - // get ts values individually to use as directory names when storing - // uploaded images - date := make(map[string]int) if ts == "" { - now := time.Now() - date["year"] = now.Year() - date["month"] = int(now.Month()) - date["day"] = now.Day() - - // create timestamp format 'yyyy-mm-dd' and set in PostForm for - // db insertion - ts = fmt.Sprintf("%d-%02d-%02d", date["year"], date["month"], date["day"]) - req.PostForm.Set("timestamp", ts) - } else { - tsParts := strings.Split(ts, "-") - year, err := strconv.Atoi(tsParts[0]) - if err != nil { - return nil, fmt.Errorf("%s", err) - } + ts = fmt.Sprintf("%d", time.Now().Unix()*1000) // Unix() returns seconds since unix epoch + } - month, err := strconv.Atoi(tsParts[1]) - if err != nil { - return nil, fmt.Errorf("%s", err) - } + req.Form.Set("timestamp", ts) - day, err := strconv.Atoi(tsParts[2]) - if err != nil { - return nil, fmt.Errorf("%s", err) - } - - date["year"] = year - date["month"] = month - date["day"] = day - } + // To use for FormValue name:urlPath + urlPaths := make(map[string]string) // get or create upload directory to save files from request pwd, err := os.Getwd() @@ -64,11 +34,17 @@ func storeFileUploads(req *http.Request) (map[string]string, error) { return nil, err } - tsParts := strings.Split(ts, "-") + i, err := strconv.ParseInt(ts, 10, 64) + if err != nil { + return nil, err + } + + tm := time.Unix(int64(i/1000), int64(i%1000)) + urlPathPrefix := "api" uploadDirName := "uploads" - uploadDir := filepath.Join(pwd, uploadDirName, tsParts[0], tsParts[1]) + uploadDir := filepath.Join(pwd, uploadDirName, fmt.Sprintf("%d", tm.Year()), fmt.Sprintf("%d", tm.Month())) err = os.MkdirAll(uploadDir, os.ModeDir|os.ModePerm) // loop over all files and save them to disk @@ -104,7 +80,7 @@ func storeFileUploads(req *http.Request) (map[string]string, error) { } // add name:urlPath to req.PostForm to be inserted into db - urlPath := fmt.Sprintf("/%s/%s/%s/%s/%s", urlPathPrefix, uploadDirName, tsParts[0], tsParts[1], filename) + urlPath := fmt.Sprintf("/%s/%s/%d/%d/%s", urlPathPrefix, uploadDirName, tm.Year(), tm.Month(), filename) urlPaths[name] = urlPath } -- cgit v1.2.3