Browse Source

Add ability to count gitxts numbers and managed

feature/frontv2
squeaky otter 5 years ago
parent
commit
02d600621e
Signed by: dashie GPG Key ID: C2D57B325840B755
  1. 2
      conf/locale/locale_en-US.ini
  2. 2
      conf/locale/locale_fr-FR.ini
  3. 110
      models/counter.go
  4. 7
      models/gitxt.go
  5. 2
      models/models.go
  6. 14
      routers/gitxt/gitxt.go
  7. 9
      stuff/template/template.go
  8. 2
      templates/base/footer.tmpl

2
conf/locale/locale_en-US.ini

@ -201,6 +201,8 @@ version = version
page = page
template = template
sources = Sources
gitxts = Gitxts
managed = managed
[header]
toggle_nav = Toggle navigation

2
conf/locale/locale_fr-FR.ini

@ -201,6 +201,8 @@ version = version
page = page
template = gabarit
sources = Sources
gitxts = Gitxts
managed = gérés
[header]
toggle_nav = Toggle navigation

110
models/counter.go

@ -0,0 +1,110 @@
package models
import (
"fmt"
)
// Counter struct
type Counter struct {
ID int64 `xorm:"pk autoincr"`
Name string `xorm:"UNIQUE NOT NULL"`
Count int64
}
// Don't change them !
const (
current_gitxts = "current_gitxts"
managed_gitxts = "total_gitxts"
)
/* Counter for Gitxts [current number] */
func GetCounterGitxts() (*Counter, error) {
c := &Counter{Name: current_gitxts}
has, err := x.Get(c)
if err != nil || !has {
c.Count = 0
return c, fmt.Errorf("no counter available")
}
return c, nil
}
// Update an user
func updateCounterGitxts(e Engine, count int64) error {
c := &Counter{
Name: current_gitxts,
Count: count,
}
// Try to get the Counter and create if not existent
cc, err := GetCounterGitxts()
if cc.Count <= 0 && err != nil {
// create
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
if _, err = sess.Insert(c); err != nil {
return err
}
return sess.Commit()
}
cc.Count = count
// Update it
_, err = e.Id(cc.ID).AllCols().Update(cc)
return err
}
// UpdateUser with datas
func UpdateCounterGitxts(u int64) error {
return updateCounterGitxts(x, u)
}
/* Counter for Gitxts [total managed number] */
func GetCounterGitxtsManaged() (*Counter, error) {
c := &Counter{Name: managed_gitxts}
has, err := x.Get(c)
if err != nil || !has {
c.Count = 0
return c, fmt.Errorf("no counter available")
}
return c, nil
}
// Update an user
func updateCounterGitxtsManaged(e Engine, count int64) error {
c := &Counter{
Name: managed_gitxts,
Count: count,
}
// Try to get the Counter and create if not existent
cc, err := GetCounterGitxtsManaged()
if cc.Count <= 0 && err != nil {
// create
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
if _, err = sess.Insert(c); err != nil {
return err
}
return sess.Commit()
}
cc.Count = count
// Update it
_, err = e.Id(cc.ID).AllCols().Update(cc)
return err
}
// UpdateUser with datas
func UpdateCounterGitxtsManaged(c int64) error {
return updateCounterGitxtsManaged(x, c)
}

7
models/gitxt.go

@ -222,6 +222,13 @@ func DeleteExpiredRepositories() {
if err != nil {
log.Warn("Error removing repository %d/%d: %v", tc.userID, tc.repoID, err)
} else {
counter, _ := GetCounterGitxts()
if counter.Count <= 0 {
// This should not happens but well, anyway
UpdateCounterGitxts(counter.Count)
} else {
UpdateCounterGitxts(counter.Count - 1)
}
log.Trace("Deleted repository %s", tc.hash)
}

2
models/models.go

@ -51,7 +51,7 @@ var (
func init() {
tables = append(tables,
new(User), new(SSHKey), new(Gitxt))
new(User), new(SSHKey), new(Gitxt), new(Counter))
gonicNames := []string{"SSL"}
for _, name := range gonicNames {

14
routers/gitxt/gitxt.go

@ -233,8 +233,12 @@ func NewPost(ctx *context.Context, f form.Gitxt) {
return
}
// 5. Return render to gitxt view page
counter, _ := models.GetCounterGitxts()
counterManaged, _ := models.GetCounterGitxtsManaged()
models.UpdateCounterGitxts(counter.Count + 1)
models.UpdateCounterGitxtsManaged(counterManaged.Count + 1)
// 5. Return render to gitxt view page
log.Trace("Pushed repository %s to database as %i", repositoryName, u.ID)
ctx.Redirect(setting.AppSubURL + "/" + repositoryUser + "/" + repositoryName)
}
@ -442,6 +446,14 @@ func DeletePost(ctx *context.Context, f form.GitxtDelete) {
return
}
counter, _ := models.GetCounterGitxts()
if counter.Count <= 0 {
// This should not happens but well, anyway
models.UpdateCounterGitxts(counter.Count)
} else {
models.UpdateCounterGitxts(counter.Count - 1)
}
ctx.JSONSuccess(map[string]interface{}{
"error": nil,
"redirect": setting.AppSubURL + "/",

9
stuff/template/template.go

@ -14,6 +14,7 @@ import (
"dev.sigpipe.me/dashie/git.txt/stuff/markup"
"dev.sigpipe.me/dashie/git.txt/stuff/template/highlight"
"container/list"
"dev.sigpipe.me/dashie/git.txt/models"
)
// NewFuncMap initialize the Functions Map
@ -97,6 +98,14 @@ func NewFuncMap() []template.FuncMap {
"IsPdf": func(mime string) bool {
return strings.EqualFold(mime, "application/pdf")
},
"CounterGitxt": func() int64 {
c, _ := models.GetCounterGitxts()
return c.Count
},
"CounterGitxtManaged": func() int64 {
c, _ := models.GetCounterGitxtsManaged()
return c.Count
},
}}
}

2
templates/base/footer.tmpl

@ -7,6 +7,8 @@
<div>
<div class="left">
© 2017 git.txt - {{.i18n.Tr "footer.version"}}: {{AppVer}} - {{.i18n.Tr "footer.page"}}: <strong>{{LoadTimes .PageStartTime}}</strong> - {{.i18n.Tr "footer.template"}}: <strong>{{call .TmplLoadTimes}}</strong>
<br/>
{{.i18n.Tr "footer.gitxts"}}: {{CounterGitxt}}, {{.i18n.Tr "footer.managed"}}: {{CounterGitxtManaged}}
</div>
<div class="right">
<a target="_blank" href="https://dev.sigpipe.me/dashie/git.txt">{{.i18n.Tr "footer.sources"}}</a>