Merge pull request #5563 from rainzm/fix/list_notification

fix & feat(notify): Notification and template
This commit is contained in:
Zexi Li
2020-03-20 14:44:44 +08:00
committed by GitHub
11 changed files with 47 additions and 93 deletions
@@ -0,0 +1 @@
{{.os_type}} 镜像 {{.name}} 上传完成
@@ -0,0 +1 @@
SMS_126915025
@@ -0,0 +1 @@
镜像 {{.name}} 上传完成
@@ -0,0 +1 @@
Yunion Verify
-3
View File
@@ -23,8 +23,5 @@ import (
type NotificationDetails struct {
apis.ResourceBaseDetails
Id string `json:"id"`
Name string `json:"name"`
UserList jsonutils.JSONObject `json:"user_list"`
}
+1 -1
View File
@@ -268,7 +268,7 @@ func (self *NotifyModelDispatcher) Verify(ctx context.Context, params map[string
data := jsonutils.NewDict()
data.Set("status", jsonutils.NewString(models.VERIFICATION_VERIFIED))
data.Set("verified_at", jsonutils.NewTimeString(current))
_, err = self.Update(ctx, verifition.CID, jsonutils.JSONNull, data, nil)
_, err = self.Update(ctx, verifition.CID, jsonutils.NewDict(), data, nil)
if err != nil {
return httperrors.NewGeneralError(err)
}
+2 -3
View File
@@ -96,7 +96,7 @@ type SNotification struct {
SendAt time.Time `nullable:"false"`
SendBy string `width:"128" nullable:"false"`
// ClusterID identify message with same topic, msg, priority
ClusterID string `width:"128" charset:"ascii" primary:"true" create:"optional"`
ClusterID string `width:"128" charset:"ascii" primary:"true" create:"optional" list:"user" get:"user"`
}
type UserDetail struct {
@@ -165,7 +165,6 @@ func (self *SNotification) getMoreDetails(ctx context.Context, query jsonutils.J
userDetails = []UserDetail{userDetail}
}
out.Id = self.ClusterID
out.UserList = jsonutils.Marshal(userDetails)
return out, nil
}
@@ -289,7 +288,7 @@ func (self *SNotificationManager) ListItemFilter(ctx context.Context, q *sqlchem
q = q.Equals("uid", userCred.GetUserId())
}
q = q.GroupBy("cluster_id")
q = q.GroupBy("cluster_id").Desc("received_at")
return q, nil
}
+40 -48
View File
@@ -19,13 +19,13 @@ import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
ptem "text/template"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/sqlchemy"
@@ -34,7 +34,6 @@ import (
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/notify/options"
"yunion.io/x/onecloud/pkg/notify/rpc/apis"
"yunion.io/x/onecloud/pkg/notify/template"
)
type STemplateManager struct {
@@ -58,6 +57,7 @@ const (
TEMPLATE_TYPE_TITLE = "title"
TEMPLATE_TYPE_CONTENT = "content"
TEMPLATE_TYPE_REMOTE = "remote"
CONTACTTYPE_ALL = "all"
)
type STemplate struct {
@@ -75,58 +75,50 @@ func (tm *STemplateManager) GetEmailUrl() string {
return options.Options.VerifyEmailUrl
}
var initTemlateList []STemplate
var templatePath = "/opt/yunion/share/template"
func (tm *STemplateManager) defaultTemplate() []STemplate {
if len(initTemlateList) != 0 {
return initTemlateList
}
func (tm *STemplateManager) defaultTemplate() ([]STemplate, error) {
templates := make([]STemplate, 0, 4)
initTemlateList = []STemplate{
{
ContactType: "email",
Topic: "IMAGE_ACTIVED",
TemplateType: TEMPLATE_TYPE_TITLE,
Content: template.IMAGE_ACTIVED_TITLE,
},
{
ContactType: "email",
Topic: "IMAGE_ACTIVED",
TemplateType: TEMPLATE_TYPE_CONTENT,
Content: template.IMAGE_ACTIVED_CONTENT,
},
}
content, err := ioutil.ReadFile(template.EMAIL_VERIFY_CONTENT_PATH)
if err == nil {
initTemlateList = append(initTemlateList,
STemplate{
ContactType: "email",
Topic: "VERIFY",
TemplateType: TEMPLATE_TYPE_CONTENT,
for _, templateType := range []string{"title", "content", "remote"} {
contactType, topic := CONTACTTYPE_ALL, ""
titleTemplatePath := fmt.Sprintf("%s/%s", templatePath, templateType)
files, err := ioutil.ReadDir(titleTemplatePath)
if err != nil {
return templates, errors.Wrapf(err, "Read Dir '%s'", titleTemplatePath)
}
for _, file := range files {
if file.IsDir() {
continue
}
spliteName := strings.Split(file.Name(), ".")
topic = spliteName[0]
if len(spliteName) > 1 {
contactType = spliteName[1]
}
fullPath := filepath.Join(titleTemplatePath, file.Name())
content, err := ioutil.ReadFile(fullPath)
if err != nil {
return templates, err
}
templates = append(templates, STemplate{
ContactType: contactType,
Topic: topic,
TemplateType: templateType,
Content: string(content),
},
STemplate{
ContactType: "email",
Topic: "VERIFY",
TemplateType: TEMPLATE_TYPE_TITLE,
Content: template.EMAIL_VERIFY_TITLE,
},
)
} else {
if os.IsNotExist(err) {
log.Errorf("The path of email verify template is invalid")
} else {
log.Errorf("open %s error: %s", template.EMAIL_VERIFY_CONTENT_PATH, err.Error())
})
}
}
return initTemlateList
return templates, nil
}
func (tm *STemplateManager) InitializeData() error {
for _, template := range tm.defaultTemplate() {
q := tm.Query().Equals("contact_type", template.ContactType).Equals("topic", template.Topic).Equals("template_type",
template.TemplateType)
templates, err := tm.defaultTemplate()
if err != nil {
return err
}
for _, template := range templates {
q := tm.Query().Equals("contact_type", template.ContactType).Equals("topic", template.Topic).Equals("template_type", template.TemplateType)
count, _ := q.CountWithError()
if count > 0 {
continue
@@ -144,7 +136,7 @@ func (tm *STemplateManager) InitializeData() error {
func (tm *STemplateManager) NotifyFilter(contactType, topic, msg string) (params apis.SendParams, err error) {
params.Topic = topic
templates := make([]STemplate, 0, 3)
q := tm.Query().Equals("contact_type", contactType).Equals("topic", strings.ToUpper(topic))
q := tm.Query().Equals("topic", strings.ToUpper(topic)).In("contact_type", []string{CONTACTTYPE_ALL, contactType})
err = db.FetchModelObjects(tm, q, &templates)
if errors.Cause(err) == sql.ErrNoRows || len(templates) == 0 {
// no such template, return as is
-15
View File
@@ -1,15 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package template // import "yunion.io/x/onecloud/pkg/notify/template"
-23
View File
@@ -1,23 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package template
const (
EMAIL_VERIFY_CONTENT_PATH = "/opt/yunion/share/notify/email_verify_template"
EMAIL_VERIFY_TITLE = "Yunion Verify"
IMAGE_ACTIVED_TITLE = "镜像 {{.name}} 上传完成"
IMAGE_ACTIVED_CONTENT = "{{.os_type}} 镜像 {{.name}} 上传完成"
)