properly show category depth, start loading news for template

This commit is contained in:
Dan Ballard 2015-05-12 08:32:47 -07:00
parent 770bf08fc3
commit 277718e907
5 changed files with 57 additions and 8 deletions

View File

@ -1,5 +1,14 @@
# transmet # transmet
Quick fast dirty link store that can dump them to an html template Quick fast personal link store that exports to a HTML template for quick posting to a blog
Usecase: storing interesting news articles you come across during a week with at the moment notes/commentary and
then exporting in a currated organized format for immediate blog posting at your convience
## Note
As this is a personal project, some of the niceities like user managment and category managment
(that are one time tasks) are left to be done in SQL. I needed a tool to store links and export
to html so that's what I've focused on.
# Install # Install
@ -14,6 +23,8 @@ CREATE EXTENSION pgcrypto;
go get bitbucket.org/liamstask/goose/cmd/goose go get bitbucket.org/liamstask/goose/cmd/goose
edit db/dbconf.yaml
goose up goose up
## Build and run ## Build and run
@ -22,3 +33,13 @@ go build
sudo cp transmet.conf /etc/init sudo cp transmet.conf /etc/init
sudo service transmet start sudo service transmet start
## Setup environment
### Adding a user
INSERT INTO users (username, password) VALUES('USERNAME', crypt('PASSWORD', gen_salt('bf')));
### Adding Categories
INSERT INTO categories (name, parent_id) VALUES ('NAME', [null or PARENT_ID]);

View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"fmt" "fmt"
"strings"
) )
type Category struct { type Category struct {
@ -44,4 +45,19 @@ func LoadCategories(db *sql.DB) {
CategoriesTree = append(CategoriesTree, category) CategoriesTree = append(CategoriesTree, category)
} }
} }
}
// get the number of parents a category has
func (category *Category) Depth() int {
depth := 0
current := category
for current.Parent.Valid {
current = CategoriesFlat[int(current.Parent.Int64)]
depth ++
}
return depth
}
func (category *Category) ToString() string {
return strings.Repeat("- ", category.Depth()) + category.Name
} }

View File

@ -17,6 +17,12 @@ type News struct {
Expoerted bool Expoerted bool
} }
type NewsContainer struct {
Name string
News []News
Children map[int]*NewsContainer
}
func (news *News) Insert(db *sql.DB) error { func (news *News) Insert(db *sql.DB) error {
_, err := db.Exec("INSERT INTO news (url, title, category_id, notes) VALUES($1, $2, $3, $4)", news.Url, news.Title, news.Category_id, news.Notes ); _, err := db.Exec("INSERT INTO news (url, title, category_id, notes) VALUES($1, $2, $3, $4)", news.Url, news.Title, news.Category_id, news.Notes );
if err != nil { if err != nil {
@ -26,15 +32,21 @@ func (news *News) Insert(db *sql.DB) error {
return nil return nil
} }
func Unexported(db *sql.DB) ([]News, error) { func Unexported(db *sql.DB) (map[int]*NewsContainer, error) {
res, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported=false") rows, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported=false")
if err != nil { if err != nil {
fmt.Println("DB errpr reading unexported news: ", err) fmt.Println("DB errpr reading unexported news: ", err)
return nil, err return nil, err
} }
news := []News{} newsTree := map[int]*NewsContainer{}
newsFlat := map[int]*NewsContainer{}
for rows.Next() {
news := News{}
var url sql.NullString
err := rows.Scan(&news.Url, &news.Title, &news.Category_id, &news.Date, &news.Notes)
}
return newsTree, nil
return news, nil
} }

View File

@ -135,7 +135,7 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
var url = r.URL.Query().Get("url") var url = r.URL.Query().Get("url")
reHttp := regexp.MustCompile("^http://") reHttp := regexp.MustCompile("^http://")
if ! reHttp.Match([]byte(url)) { if url != "" && ! reHttp.Match([]byte(url)) {
url = "http://" + url url = "http://" + url
} }
resp, err := http.Get(url) resp, err := http.Get(url)

View File

@ -21,7 +21,7 @@
{{define "option-category"}} {{define "option-category"}}
<option value="{{.Id}}">{{if .Parent.Valid}}-{{end}}{{.Name}}</option> <option value="{{.Id}}">{{.ToString}}</option>
{{range $child := .Children}} {{range $child := .Children}}
{{template "option-category" $child}} {{template "option-category" $child}}
{{end}} {{end}}