Example #1
0
// BeforeSave invokes required actions before persisting.
func (u *User) BeforeSave(db *gorm.DB) (err error) {
	if u.Slug == "" {
		for i := 0; true; i++ {
			if i == 0 {
				u.Slug = slugify.Slugify(u.Username)
			} else {
				u.Slug = slugify.Slugify(
					fmt.Sprintf("%s-%d", u.Username, i),
				)
			}

			notFound := db.Where(
				"slug = ?",
				u.Slug,
			).Not(
				"id",
				u.ID,
			).First(
				&User{},
			).RecordNotFound()

			if notFound {
				break
			}
		}
	}

	if u.Email != "" {
		email, err := govalidator.NormalizeEmail(
			u.Email,
		)

		if err != nil {
			return fmt.Errorf("Failed to normalize email")
		}

		u.Email = email
	}

	if u.Password != "" {
		encrypt, err := bcrypt.GenerateFromPassword(
			[]byte(u.Password),
			bcrypt.DefaultCost,
		)

		if err != nil {
			return fmt.Errorf("Failed to encrypt password")
		}

		u.Hashword = string(encrypt)
	}

	if u.Hash == "" {
		u.Hash = base32.StdEncoding.EncodeToString(
			securecookie.GenerateRandomKey(32),
		)
	}

	return nil
}
Example #2
0
// BeforeSave invokes required actions before persisting.
func (u *Team) BeforeSave(db *gorm.DB) (err error) {
	if u.Slug == "" {
		for i := 0; true; i++ {
			if i == 0 {
				u.Slug = slugify.Slugify(u.Name)
			} else {
				u.Slug = slugify.Slugify(
					fmt.Sprintf("%s-%d", u.Name, i),
				)
			}

			notFound := db.Where(
				"slug = ?",
				u.Slug,
			).Not(
				"id",
				u.ID,
			).First(
				&Team{},
			).RecordNotFound()

			if notFound {
				break
			}
		}
	}

	return nil
}
Example #3
0
// automatically create files for content entries
func writeContents(path string, tip *contentNode) error {
	if tip.parent != nil {
		filename := path + "/" + "__module__.txt"
		if err := placePart(filename, tip.label); err != nil {
			return err
		}
	}

	for _, kid := range tip.children {
		slug := slugify.Slugify(kid.label)

		if kid.final() {
			filename := path + "/" + slug + ".txt"
			if err := placePart(filename, kid.label); err != nil {
				return err
			}

			continue
		}

		newPath := path + "/" + slug
		if err := os.Mkdir(newPath, 0700); err != nil {
			return err
		}

		writeContents(newPath, kid)
	}

	return nil
}
Example #4
0
func (a *Article) BeforeCreate() (err error) {
	u, err := url.Parse(a.LeadImage)
	if err != nil {
		a.LeadImage = ""
	}
	a.LeadImage = u.String()
	a.Slug = slugify.Slugify(a.Title)
	return
}
Example #5
0
func slugifyFilename(filename string) string {
	extension := filepath.Ext(filename)
	name := slugify.Slugify(filename[0 : len(filename)-len(extension)])
	return strings.Join([]string{name, extension}, "")
}
Example #6
0
File: new.go Project: tucnak/kamu
		book, err := model.LocateBook(wd)
		if err != nil {
			ctx.Log("couldn't locate the book:", err)
			return 1
		}

		self, err := book.PartByPath(wd)
		if err != nil {
			ctx.Log("outside the book's content directory")
			return 1
		}

		var slug string
		if userDefined, ok := ctx.Get("slug"); ok {
			slug = userDefined
		} else {
			slug = slugify.Slugify(caption)
		}

		err = book.Insert(self, caption, slug, ctx.Is("module"))
		if err != nil {
			ctx.Log("couldn't make a new part", err)
			return 1
		}

		fmt.Println("A new part has been created.")
		return 0
	},
}
Example #7
0
//PrettyURL converts string to url-friendly form
func PrettyURL(s string) string {
	return slugify.Slugify(s)
}