Пример #1
0
func (self *Backend) getBlobRefCollectionSelectors() map[*mongo.Collection][]string {
	return nil

	if self.imageRefCollectionSelectors == nil {
		colSel := make(map[*mongo.Collection][]string)
		for _, collection := range mongo.Collections {
			var doc BlobDoc // todo needs to be document type of collection
			collection.InitDocument(&doc)
			model.SetAllSliceLengths(&doc, 1)
			model.Visit(&doc, model.FieldOnlyVisitor(
				func(data *model.MetaData) error {
					if _, ok := data.Value.Addr().Interface().(*media.BlobRef); ok {
						if _, ok := colSel[collection]; !ok {
							colSel[collection] = nil
						}
						colSel[collection] = append(colSel[collection], data.WildcardSelector())
					}
					return nil
				},
			))
		}
		self.blobRefCollectionSelectors = colSel
	}
	return self.blobRefCollectionSelectors
}
Пример #2
0
func InitRefs(document interface{}) {
	model.Visit(document, model.FieldOnlyVisitor(
		func(data *model.MetaData) error {
			if ref, ok := data.Value.Addr().Interface().(*Ref); ok && ref.CollectionName == "" {
				ref.CollectionName, ok = data.Attrib("to")
				if !ok {
					panic(data.Selector() + " is missing the 'to' meta-data tag")
				}
			}
			return nil
		},
	))
}
Пример #3
0
func RemoveInvalidRefs(document interface{}) (invalidRefs []InvalidRefData, err error) {
	err = model.Visit(document, model.FieldTypeVisitor(
		func(ref *Ref, metaData *model.MetaData) error {
			ok, err := ref.CheckID()
			if err != nil {
				return err
			}
			if !ok {
				invalidRefs = append(invalidRefs, InvalidRefData{ref, metaData})
				ref.Set(nil)
			}
			return nil
		},
	))
	if err != nil {
		return nil, err
	}
	return invalidRefs, nil
}
Пример #4
0
func (self *LabeledModelView) Render(ctx *Context) (err error) {
	return model.Visit(self.Model, model.FieldOnlyVisitor(
		func(field *model.MetaData) error {
			if !self.IsFieldExcluded(field) {
				if value, ok := field.ModelValue(); ok {
					if !value.IsEmpty() || !self.HideEmpty {
						v := SPAN(Config.LabeledModelViewValueClass, value)
						label := &Label{
							Class:   Config.LabeledModelViewLabelClass,
							Content: Escape(self.FieldLabel(field)),
							For:     v,
						}
						return Views{label, v}.Render(ctx)
					}
				}
			}
			return nil
		},
	))
}
Пример #5
0
func (self *Backend) getImageRefCollectionSelectors() map[*mongo.Collection][]string {
	if self.imageRefCollectionSelectors == nil {
		colSel := make(map[*mongo.Collection][]string)
		for _, collection := range mongo.Collections {
			doc := collection.NewDocument()
			model.SetAllSliceLengths(doc, 1)
			model.Visit(doc, model.FieldOnlyVisitor(
				func(data *model.MetaData) error {
					if _, ok := data.Value.Addr().Interface().(*media.ImageRef); ok {
						if _, ok := colSel[collection]; !ok {
							colSel[collection] = nil
						}
						colSel[collection] = append(colSel[collection], data.WildcardSelector())
					}
					return nil
				},
			))
		}
		self.imageRefCollectionSelectors = colSel
	}
	return self.imageRefCollectionSelectors
}
Пример #6
0
func (self *Form) Render(ctx *Context) (err error) {
	if self.OnSubmit == nil {
		panic("view.Form.OnSubmit must not be nil")
	}
	layout := self.GetLayout()
	if layout == nil {
		panic("view.Form.GetLayout() returned nil")
	}
	fieldFactory := self.GetFieldFactory()
	if fieldFactory == nil {
		panic("view.Form.GetFieldFactory() returned nil")
	}

	var formModel interface{}
	var hasErrors bool
	content := Views{&HiddenInput{Name: FormIDName, Value: self.FormID}}
	isPost := self.IsPost(ctx.Request)

	if self.GetModel == nil {
		submitButton := self.GetFieldFactory().NewSubmitButton(self.GetSubmitButtonText(), self.SubmitButtonConfirm, self)
		content = append(content, submitButton)
	} else {
		formModel, err = self.GetModel(self, ctx)
		if err != nil {
			return err
		}
		v := reflect.ValueOf(formModel)
		if !(v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct) &&
			!(v.Kind() == reflect.Map && v.Type().Key().Kind() == reflect.String) &&
			!(v.Type() == model.DynamicValueType) {
			panic(fmt.Errorf("Invalid form model type: %T", formModel))
		}
		if isPost {
			setPostValues := &setPostValuesStructVisitor{
				form:      self,
				formModel: formModel,
				ctx:       ctx,
			}
			err = model.Visit(formModel, setPostValues)
			if err != nil {
				return err
			}
		}
		validateAndFormLayout := &validateAndFormLayoutStructVisitor{
			form:        self,
			formLayout:  layout,
			formModel:   formModel,
			formContent: &content,
			ctx:         ctx,
			isPost:      isPost,
		}
		err = model.Visit(formModel, validateAndFormLayout)
		if err != nil {
			return err
		}
		hasErrors = len(validateAndFormLayout.fieldValidationErrors) > 0 || len(validateAndFormLayout.generalValidationErrors) > 0
	}

	if isPost && !hasErrors {
		message, redirect, err := self.OnSubmit(self, formModel, ctx)
		if err == nil {
			if redirect == nil {
				redirect = self.Redirect
			}
			if redirect != nil {
				return Redirect(redirect.URL(ctx))
			}
			if message == "" {
				message = self.SuccessMessage
			}
			if message != "" {
				self.GetLayout().SubmitSuccess(message, self, ctx, &content)
			}
		} else {
			if message == "" {
				message = err.Error()
			}
			self.GetLayout().SubmitError(message, self, ctx, &content)
		}
	}

	// Render HTML form element
	method := strings.ToUpper(self.Method)
	if method == "" {
		method = "POST"
	}
	action := self.Action
	if action == "" {
		action = "."
		if i := strings.Index(ctx.Request.RequestURI, "?"); i != -1 {
			action += ctx.Request.RequestURI[i:]
		}
	}
	// Hack: Value of hidden input FormIDName is not available when
	// enctype is multipart/form-data (bug?), so pass the form id as
	// URL parameter
	if self.Enctype == MultipartFormData && ctx.Request.Method != "POST" {
		action = utils.AddUrlParam(action, FormIDName, self.FormID)
	}

	ctx.Response.XML.OpenTag("form")
	ctx.Response.XML.AttribIfNotDefault("id", self.id)
	ctx.Response.XML.AttribIfNotDefault("class", self.Class)
	ctx.Response.XML.AttribIfNotDefault("style", self.Style)
	ctx.Response.XML.Attrib("method", method)
	ctx.Response.XML.Attrib("action", action)
	ctx.Response.XML.AttribIfNotDefault("enctype", self.Enctype)

	if len(content) > 0 {
		content.Init(content)
		err = content.Render(ctx)
		if err != nil {
			return err
		}
	}
	ctx.Response.XML.ForceCloseTag() // form
	return nil
}