Various tweaks

This commit is contained in:
Noah 2018-04-11 18:59:30 -07:00
parent 709e1f9577
commit b7d1661fcd
7 changed files with 216 additions and 169 deletions

View File

@ -50,7 +50,7 @@ def main(tumblr, root, scheme):
}
log.info("Blog title: %s", SiteSettings["site"]["title"])
log.info("Description: %s", SiteSettings["site"]["description"])
write_db(root, "app/settings", SiteSettings)
# write_db(root, "app/settings", SiteSettings)
posts_total = r["posts-total"] # n
log.info("Total Posts: %d", posts_total)
@ -63,6 +63,9 @@ def main(tumblr, root, scheme):
POST_ID = 0
# Unique Tumblr post IDs so no duplicates.
tumblr_posts = set()
while posts_start >= 0:
log.info("GET PAGE start=%d num=%d\n", posts_start, per_page)
r = api_get(API_ROOT, start=posts_start, num=per_page)
@ -80,6 +83,10 @@ def main(tumblr, root, scheme):
timestamp = datetime.fromtimestamp(post["unix-timestamp"])
timestamp = timestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
if post["id"] in tumblr_posts:
continue
tumblr_posts.add(post["id"])
slug = post.get("slug")
if not slug:
slug = re.sub(r'.*{}/'.format(tumblr), '', post.get("url-with-slug"))
@ -209,12 +216,15 @@ def _download_images(root, images):
for i, url in enumerate(images):
log.info("DOWNLOAD: %s", url)
filename = url.rsplit("/", 1)[-1]
r = requests.get(url)
if r.ok:
filename = "static/photos/"+filename
if os.path.isfile(os.path.join(root, filename)):
result.append("/"+filename)
continue
r = requests.get(url)
if r.ok:
with open(os.path.join(root, filename), "wb") as fh:
fh.write(r.content)
result.append("/"+filename)

View File

@ -10,16 +10,24 @@ import (
"github.com/gorilla/mux"
"github.com/kirsle/blog/internal/forms"
"github.com/kirsle/blog/internal/log"
"github.com/kirsle/blog/internal/mail"
"github.com/kirsle/blog/internal/markdown"
"github.com/kirsle/blog/models/settings"
"github.com/kirsle/blog/internal/render"
"github.com/kirsle/blog/internal/responses"
"github.com/kirsle/blog/models/settings"
)
// Register attaches the contact URL to the app.
func Register(r *mux.Router) {
r.HandleFunc("/contact", func(w http.ResponseWriter, r *http.Request) {
// Allow ?next= to redirect to other local pages.
nextURL := r.FormValue("next")
if nextURL != "" && nextURL[0] != '/' {
log.Error("/contact?next=: URL must be a local page beginning with /")
nextURL = ""
}
form := &forms.Contact{}
v := map[string]interface{}{
"Form": form,
@ -42,6 +50,15 @@ func Register(r *mux.Router) {
if r.Method == http.MethodPost {
form.ParseForm(r)
if err = form.Validate(); err != nil {
// If they're not from the /contact front-end, redirect them
// with the flash.
if len(nextURL) > 0 {
responses.FlashAndRedirect(w, r, nextURL, err.Error())
return
}
// Otherwise flash and let the /contact page render to retain
// their form fields so far.
responses.Flash(w, r, err.Error())
} else {
go mail.SendEmail(mail.Email{
@ -72,8 +89,14 @@ func Register(r *mux.Router) {
))
fh.Close()
}
if len(nextURL) > 0 {
responses.FlashAndRedirect(w, r, nextURL, "Your message has been sent.")
} else {
responses.FlashAndRedirect(w, r, "/contact", "Your message has been sent.")
}
return
}
}
render.Template(w, r, "contact", v)

View File

@ -6,10 +6,10 @@ import (
"time"
"github.com/gorilla/feeds"
"github.com/kirsle/blog/internal/responses"
"github.com/kirsle/blog/models/posts"
"github.com/kirsle/blog/models/settings"
"github.com/kirsle/blog/models/users"
"github.com/kirsle/blog/internal/responses"
)
func feedHandler(w http.ResponseWriter, r *http.Request) {
@ -46,7 +46,7 @@ func feedHandler(w http.ResponseWriter, r *http.Request) {
Description: post.Body + suffix,
Created: p.Created,
})
if i >= 5 {
if i == 9 { // 10 -1
break
}
}

View File

@ -8,11 +8,11 @@ import (
"strconv"
"github.com/kirsle/blog/internal/log"
"github.com/kirsle/blog/internal/render"
"github.com/kirsle/blog/internal/types"
"github.com/kirsle/blog/models/comments"
"github.com/kirsle/blog/models/posts"
"github.com/kirsle/blog/models/users"
"github.com/kirsle/blog/internal/render"
"github.com/kirsle/blog/internal/types"
)
// partialIndex renders and returns the blog index partial.
@ -28,10 +28,16 @@ func partialIndex(r *http.Request, tag, privacy string) template.HTML {
if page <= 0 {
page = 1
}
perPage := 5 // TODO: configurable
perPage := 10 // TODO: configurable
offset := (page - 1) * perPage
stop := offset + perPage
// Calculate page total.
var pages = int(len(pool) / perPage)
if pages == 0 {
pages = 1
}
// Handle pagination.
var previousPage, nextPage int
if page > 1 {
@ -48,7 +54,7 @@ func partialIndex(r *http.Request, tag, privacy string) template.HTML {
var view []PostMeta
for i := offset; i < stop; i++ {
if i >= len(pool) {
continue
break
}
post, err := posts.Load(pool[i].ID)
if err != nil {
@ -81,6 +87,8 @@ func partialIndex(r *http.Request, tag, privacy string) template.HTML {
v := map[string]interface{}{
"PreviousPage": previousPage,
"NextPage": nextPage,
"Page": page,
"Pages": pages,
"View": view,
}
render.Template(&output, r, "blog/index.partial", v)

View File

@ -10,10 +10,10 @@ import (
"github.com/kirsle/blog/internal/log"
"github.com/kirsle/blog/internal/middleware"
"github.com/kirsle/blog/internal/middleware/auth"
"github.com/kirsle/blog/models/settings"
"github.com/kirsle/blog/models/users"
"github.com/kirsle/blog/internal/sessions"
"github.com/kirsle/blog/internal/types"
"github.com/kirsle/blog/models/settings"
"github.com/kirsle/blog/models/users"
)
// Vars is an interface to implement by the templates to pass their own custom
@ -23,6 +23,7 @@ type vars struct {
// Global, "constant" template variables.
SetupNeeded bool
Title string
Description string
Path string
TemplatePath string // actual template file on disk
LoggedIn bool
@ -62,6 +63,7 @@ func Template(w io.Writer, r *http.Request, path string, data interface{}) error
Request: r,
RequestTime: r.Context().Value(types.StartTimeKey).(time.Time),
Title: s.Site.Title,
Description: s.Site.Description,
Path: r.URL.Path,
Data: data,

View File

@ -50,7 +50,7 @@
<div class="bluez-header">
<div class="container">
<h1 class="bluez-title">{{ .Title }}</h1>
<p class="lead bluez-description">Just another web blog.</p>
<p class="lead bluez-description">{{ .Description }}</p>
</div>
</div>

View File

@ -1,5 +1,7 @@
{{ define "title" }}Website Settings{{ end }}
{{ define "content" }}
<div class="card">
<div class="card-body">
<form action="/admin/settings" method="POST">
<input type="hidden" name="_csrf" value="{{ .CSRF }}">
@ -166,4 +168,6 @@
{{ end }}
</form>
</div>
</div>
{{ end }}