Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/campaign publish #275

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions docs/docsrc/examples/examples_admin/page_builder_with_campaign.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package examples_admin

import (
"context"
"fmt"
"net/http"
"strings"

"github.com/qor/oss"

"github.com/qor/oss/filesystem"

"github.com/qor5/admin/v3/pagebuilder"
"github.com/qor5/admin/v3/presets"
"github.com/qor5/admin/v3/presets/gorm2op"
Expand Down Expand Up @@ -57,6 +62,14 @@ func (b *Campaign) GetTitle() string {
return b.Title
}

func (b *Campaign) PublishUrl(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (s string) {
return "campaigns/index.html"
}

func (b *Campaign) LiveUrl(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (s string) {
return "campaigns/index.html"
}

func (p *Campaign) PrimarySlug() string {
return fmt.Sprintf("%v_%v", p.ID, p.Version.Version)
}
Expand All @@ -73,6 +86,14 @@ func (p *Campaign) PrimaryColumnValuesBySlug(slug string) map[string]string {
}
}

func (b *CampaignProduct) PublishUrl(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (s string) {
return "campaign-products/index.html"
}

func (b *CampaignProduct) LiveUrl(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (s string) {
return "campaign-products/index.html"
Copy link
Contributor

@sunfmin sunfmin Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个链接像是个列表页面?实际上是吗?

}

func (b *CampaignProduct) GetTitle() string {
return b.Name
}
Expand Down Expand Up @@ -115,7 +136,10 @@ func PageBuilderExample(b *presets.Builder, db *gorm.DB) http.Handler {
if err != nil {
panic(err)
}
puBuilder := publish.New(db, nil)
stroge := filesystem.New("/tmp/publish")
stroge.Delete("/tmp/publish/campaigns/index.html")
stroge.Delete("/tmp/publish/campaign-products/index.html")
puBuilder := publish.New(db, stroge)
if b.GetPermission() == nil {
b.Permission(
perm.New().Policies(
Expand All @@ -132,7 +156,7 @@ func PageBuilderExample(b *presets.Builder, db *gorm.DB) http.Handler {
RenderFunc(func(obj interface{}, input *pagebuilder.RenderInput, ctx *web.EventContext) HTMLComponent {
c := obj.(*MyContent)
return Div().Text(c.Text).Style("height:200px")
})
}).Cover("https://qor5.com/img/qor-logo.png")

ed := header.Model(&MyContent{}).Editing("Text", "Color")
ed.Field("Color").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package examples_admin
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/qor5/admin/v3/pagebuilder"
Expand Down Expand Up @@ -187,6 +189,52 @@ func TestPageBuilderCampaign(t *testing.T) {
}
},
},
{
Name: "Page Builder Campaign Detail Publish",
Debug: true,
ReqFunc: func() *http.Request {
pageBuilderData.TruncatePut(dbr)
req := NewMultipartBuilder().
PageURL("/campaigns/1_2024-05-20-v01?__execute_event__=publish_EventPublish&id=1_2024-05-20-v01").
BuildEventFuncRequest()

return req
},
EventResponseMatch: func(t *testing.T, er *TestEventResponse) {
body, err := os.ReadFile("/tmp/publish/campaigns/index.html")
if err != nil {
t.Error("publish failed!")
return
}
if !strings.Contains(string(body), "Hello Campaign") {
t.Errorf("publish failed! %s", string(body))
return
}
},
},
{
Name: "Page Builder Campaign Products Editor Publish",
Debug: true,
ReqFunc: func() *http.Request {
pageBuilderData.TruncatePut(dbr)
req := NewMultipartBuilder().
PageURL("/page_builder/campaign-products-editors/1_2024-05-20-v01?__execute_event__=publish_EventPublish&id=1_2024-05-20-v01").
BuildEventFuncRequest()

return req
},
EventResponseMatch: func(t *testing.T, er *TestEventResponse) {
body, err := os.ReadFile("/tmp/publish/campaign-products/index.html")
if err != nil {
t.Error("publish failed!")
return
}
if !strings.Contains(string(body), "Hello Product") {
t.Errorf("publish failed! %s", string(body))
return
}
},
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down
28 changes: 19 additions & 9 deletions pagebuilder/model.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package pagebuilder

import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"path"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -582,14 +583,7 @@
}

func (b *ModelBuilder) setName() {
b.name = b.getModelName(b.mb)
}

func (b *ModelBuilder) getModelName(mb *presets.ModelBuilder) string {
modelType := reflect.TypeOf(mb.NewModel())
modelstr := modelType.String()
modelName := modelstr[strings.LastIndex(modelstr, ".")+1:]
return inflection.Plural(strcase.ToKebab(modelName))
b.name = utils.GetObjName(b.mb.NewModel())
}

func (b *ModelBuilder) addSharedContainerToPage(pageID int, containerID, pageVersion, locale, modelName string, modelID uint) (newContainerID string, err error) {
Expand Down Expand Up @@ -1243,3 +1237,19 @@
return err
})
}

func (b *ModelBuilder) HtmlRaw(obj interface{}) (r string) {
p, ok := obj.(PrimarySlugInterface)
if !ok {
return

Check warning on line 1244 in pagebuilder/model.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/model.go#L1244

Added line #L1244 was not covered by tests
}
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", fmt.Sprintf("/?id=%s", p.PrimarySlug()), nil)
b.preview.ServeHTTP(w, req)
r = w.Body.String()
return
}

func (b *ModelBuilder) ContextValueProvider(in context.Context) context.Context {
return context.WithValue(in, b.name, b)
}
71 changes: 23 additions & 48 deletions pagebuilder/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,55 @@
"path"
"path/filepath"

"github.com/qor/oss"
"github.com/qor5/admin/v3/utils"

"github.com/qor5/admin/v3/l10n"

"github.com/qor/oss"
"github.com/qor5/admin/v3/publish"
"gorm.io/gorm"
)

type contextKeyType int

const contextKey contextKeyType = iota

func (b *ModelBuilder) ContextValueProvider(in context.Context) context.Context {
return context.WithValue(in, contextKey, b)
}

func builderFromContext(c context.Context) (b *ModelBuilder, ok bool) {
b, ok = c.Value(contextKey).(*ModelBuilder)
return
}
func (p *Page) PublishUrl(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (s string) {
var (
err error
localePath string
)
builder := ctx.Value(utils.GetObjName(p))

Check warning on line 24 in pagebuilder/publish.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/publish.go#L19-L24

Added lines #L19 - L24 were not covered by tests

func (p *Page) GetPublishActions(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (objs []*publish.PublishAction, err error) {
var b *ModelBuilder
var ok bool
if b, ok = builderFromContext(ctx); !ok || b == nil {
return
}
content, err := p.getPublishContent(b, ctx)
if err != nil {
b, ok := builder.(*ModelBuilder)
if !ok {

Check warning on line 27 in pagebuilder/publish.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/publish.go#L26-L27

Added lines #L26 - L27 were not covered by tests
return
}

var localePath string
if b.builder.l10n != nil {
localePath = l10n.LocalePathFromContext(p, ctx)
}

var category Category
category, err = p.GetCategory(db)
if err != nil {
if category, err = p.GetCategory(db); err != nil {

Check warning on line 35 in pagebuilder/publish.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/publish.go#L35

Added line #L35 was not covered by tests
return
}
objs = append(objs, &publish.PublishAction{
Url: p.getPublishUrl(localePath, category.Path),
Content: content,
IsDelete: false,
})
p.OnlineUrl = p.getPublishUrl(localePath, category.Path)
return p.getPublishUrl(localePath, category.Path)

Check warning on line 38 in pagebuilder/publish.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/publish.go#L38

Added line #L38 was not covered by tests
}

func (p *Page) LiveUrl(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (s string) {

Check warning on line 41 in pagebuilder/publish.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/publish.go#L41

Added line #L41 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法一直返回空字符串?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

忘记返回了

var liveRecord Page

builder := ctx.Value(utils.GetObjName(p))
mb, ok := builder.(ModelBuilder)
if !ok {
return

Check warning on line 47 in pagebuilder/publish.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/publish.go#L44-L47

Added lines #L44 - L47 were not covered by tests
}
{
lrdb := db.Where("id = ? AND status = ?", p.ID, publish.StatusOnline)
if b.builder.l10n != nil {
if mb.builder.l10n != nil {

Check warning on line 51 in pagebuilder/publish.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/publish.go#L51

Added line #L51 was not covered by tests
lrdb = lrdb.Where("locale_code = ?", p.LocaleCode)
}
lrdb.First(&liveRecord)
}
if liveRecord.ID == 0 {
return
}

if liveRecord.OnlineUrl != p.OnlineUrl {
objs = append(objs, &publish.PublishAction{
Url: liveRecord.OnlineUrl,
IsDelete: true,
})
}

return
}

func (p *Page) GetUnPublishActions(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (objs []*publish.PublishAction, err error) {
objs = append(objs, &publish.PublishAction{
Url: p.OnlineUrl,
IsDelete: true,
})
return
}

Expand Down
Loading
Loading