Exemple #1
0
// SetDatastoreKey sets the datastore Key and updates the records ID if needed.
// Advanced use only.
func (r *Record) SetDatastoreKey(key *datastore.Key) *Record {

	if key == nil {

		r.setID(NoIDValue)
		r.datastoreKey = nil

	} else {

		// does the key have an ID?
		if key.IntID() > 0 {

			// set the ID
			r.setID(key.IntID())

		}

		// set the key
		r.datastoreKey = key

	}

	// chain
	return r

}
Exemple #2
0
func (g *gapDatabase) getKeyString(key *datastore.Key) string {
	if tmp := key.IntID(); tmp != 0 {
		return strconv.FormatInt(tmp, 10)
	} else {
		return key.StringID()
	}
}
Exemple #3
0
func FromGAEWithoutValidate(k *datastore.Key) (result key.Key) {
	if k == nil {
		return key.Key("")
	}
	result = key.NewWithoutValidate(k.Kind(), k.StringID(), k.IntID(), FromGAEWithoutValidate(k.Parent()))
	return
}
Exemple #4
0
func FromGAE(k *datastore.Key) (result key.Key, err error) {
	if k == nil {
		return key.Key(""), nil
	}
	parent, err := FromGAE(k.Parent())
	if err != nil {
		return
	}
	return key.New(k.Kind(), k.StringID(), k.IntID(), parent)
}
Exemple #5
0
func newFolderRef(userID UserID, key *datastore.Key) FolderRef {
	ref := FolderRef{
		UserID: userID,
	}

	if key != nil {
		ref.FolderID = formatId("folder", key.IntID())
	}

	return ref
}
func TestDatastoreKeyForPersistedRecord(t *testing.T) {

	people := CreateTestModelWithPropertyType("modeltwo")
	person := people.New().setID(123)

	var key *datastore.Key = person.DatastoreKey()

	assertEqual(t, int64(123), key.IntID())
	assertEqual(t, people.RecordType(), key.Kind())

}
func TestDatastoreKeyForUnpersistedRecord(t *testing.T) {

	people := CreateTestModel()
	person := people.New()

	var key *datastore.Key = person.DatastoreKey()

	assertEqual(t, int64(0), key.IntID())
	assertEqual(t, people.RecordType(), key.Kind())

}
Exemple #8
0
// cacheDevListForAccKey puts the specified Device list into the cache (memcache).
func cacheDevListForAccKey(c appengine.Context, accKey *datastore.Key, devices []*ds.Device) {
	mk := prefixDevListForAccKey + strconv.FormatInt(accKey.IntID(), 10)

	data, err := json.Marshal(devices) // This can't really fail
	if err != nil {
		c.Errorf("Failed to encode device list to JSON: %v", err)
	}

	if err = memcache.Set(c, &memcache.Item{Key: mk, Value: data}); err != nil {
		c.Warningf("Failed to set %s in memcache: %v", mk, err)
	}
}
Exemple #9
0
func postBlog(c appengine.Context, b *blogEntry) error {
	k := datastore.NewKey(c, "Blog", "", 0, nil)
	var (
		k2  *datastore.Key
		err error
	)
	if k2, err = datastore.Put(c, k, b); err != nil {
		println("")
		return err
	}
	b.Id = k2.IntID()
	return nil
}
Exemple #10
0
func recursiveJson(key *datastore.Key) Response {
	var parentJson Response
	if key.Parent() != nil {
		parentJson = recursiveJson(key.Parent())
	}
	return Response{
		"stringID":  key.StringID(),
		"intID":     key.IntID(),
		"kind":      key.Kind(),
		"appID":     key.AppID(),
		"namespace": key.Namespace(),
		"parent":    parentJson,
	}
}
Exemple #11
0
func fillFields(key *datastore.Key, data map[string]interface{}) {
	data["kind"] = key.Kind()
	data["stringid"] = key.StringID()
	data["intid"] = key.IntID()
	data["appid"] = key.AppID()
	data["namespace"] = key.Namespace()
	if key.Parent() != nil {
		data["kind2"] = key.Parent().Kind()
		data["stringid2"] = key.Parent().StringID()
		data["intid2"] = key.Parent().IntID()
		if key.Parent().Parent() != nil {
			data["kind3"] = key.Parent().Parent().Kind()
			data["stringid3"] = key.Parent().Parent().StringID()
			data["intid3"] = key.Parent().Parent().IntID()
		}
	}
}
Exemple #12
0
func postStats(c appengine.Context, k *datastore.Key) error {
	st := Stats{}
	err := datastore.Get(c, k, &st)
	if err != nil {
		c.Infof("Failed to load %v: %v, trying alt data", k, err)
		err = loadOldStyle(c, k, &st)
		if err != nil {
			return err
		}
	}

	if st.Data == nil || len(st.Data) < 2 {
		c.Infof("No content found, ignoring %v.", k.Encode())
		return nil
	}

	if err := st.uncompress(); err != nil {
		c.Infof("Decompression of stat data failed: %v, left %s (skipping)",
			err, st.Data)
		return nil
	}

	doc := map[string]interface{}{}
	err = json.Unmarshal(st.Data, &doc)
	if err != nil {
		c.Infof("Problem parsing stat data from %v (%v): %v - ignoring",
			k, k.Encode(), err)
		return nil
	}
	doc["timestamp"] = st.Timestamp
	doc["type"] = "stats"
	doc["active_version"] = true
	doc["api_ver"] = st.ApiVer
	doc["addr"] = st.Addr
	doc["geo"] = map[string]interface{}{
		"countryCode": st.Country,
		"cityName":    st.City,
		"regionName":  st.Region,
		"latitude":    st.Lat,
		"longitude":   st.Lon,
	}

	_, err = storeInCouch(c, doc, fmt.Sprintf("stats_%d", k.IntID()))
	return err
}
Exemple #13
0
func createHandler(w http.ResponseWriter, r *http.Request) {

	var err os.Error
	var ok bool

	c := appengine.NewContext(r)

	printLog(c, "Post Handler Start")

	if r.Method != "POST" {
		printLog(c, "Not Post Method Return")
		return
	}

	err = r.ParseForm()
	check(err)

	u := user.Current(c)

	task := new(Tasks)

	task.Context = r.FormValue("context")
	task.Status = 0
	task.UserId = u.String()

	if err, ok = task.IsValid(); ok {
		check(err)
		// postエラーの場合にjQueryでステータスを取れるか?
	}

	printLog(c, "Validated")

	var resultKey *datastore.Key
	resultKey, err = datastore.Put(c, datastore.NewIncompleteKey("Tasks"), task)

	log.Println(err)
	check(err)

	printLog(c, "Puted")

	returnJson(w, resultKey.IntID())

}
Exemple #14
0
// GetDevListForAccKey returns the Device list (only name and KeyID is populatd) for the specified Account.
// The implementation applies caching: first memcache is checked if the Device list is already stored
// which is returned if so. Else the Device list is read from the Datastore and the list is put into the memcache
// before returning it.
//
// If there is no Device for the specified Account, nil is returned as the devices,
// and it is not considered an error (err will be nil).
func GetDevListForAccKey(c appengine.Context, accKey *datastore.Key) (devices []*ds.Device, err error) {
	// First check in memcache:
	mk := prefixDevListForAccKey + strconv.FormatInt(accKey.IntID(), 10)

	var item *memcache.Item
	if item, err = memcache.Get(c, mk); err == nil {
		// Found in memcache
		var devices []*ds.Device
		err = json.Unmarshal(item.Value, &devices)
		if err != nil {
			c.Errorf("Invalid DevList value stored in memcache: %s", item.Value)
			return nil, err
		}
		return devices, nil
	}

	// If err == memcache.ErrCacheMiss it's just not present,
	// else real Error (e.g. memcache service is down).
	if err != memcache.ErrCacheMiss {
		c.Errorf("Failed to get %s from memcache: %v", mk, err)
	}

	// Either way we have to search in Datastore:

	q := datastore.NewQuery(ds.ENameDevice).Ancestor(accKey).Order(ds.PNameName)

	var devKeys []*datastore.Key
	if devKeys, err = q.GetAll(c, &devices); err != nil {
		// Datastore error.
		c.Errorf("Failed to query Device list by ancestor: %v", err)
		return nil, err
	}
	for i := range devices {
		devices[i].KeyID = devKeys[i].IntID()
	}

	// Also store it in memcache
	cacheDevListForAccKey(c, accKey, devices)

	return devices, nil
}
Exemple #15
0
func (op *DataConn) Put(obj DataItem) error {

	var key *datastore.Key
	c := op.Wreq.C

	if id := obj.ID(); id > 0 {
		key = datastore.NewKey(c, op.Entity, "", obj.ID(), nil)
	} else {
		key = datastore.NewIncompleteKey(c, op.Entity, nil)
	}

	key, err := datastore.Put(c, key, obj)
	if err != nil {
		return err
	}
	obj.SetID(key.IntID())

	err = op.PutCacheItem(obj)

	return err
}
func extract(key *datastore.Key, field string) (string, error) {
	switch strings.ToLower(field) {
	case "kind":
		return key.Kind(), nil
	case "appid":
		return key.AppID(), nil
	case "namespace":
		return key.Namespace(), nil
	case "name":
		return key.StringID(), nil
	case "id":
		return fmt.Sprintf("%v", key.IntID()), nil
	case "parent":
		if key.Parent() == nil {
			return "", nil
		} else {
			return key.Parent().Encode(), nil
		}
	default:
		return "", fmt.Errorf("Unsupported field [%v]. Supported fields are kind, appID, namespace, name, id, parentkey.", field)
	}
}
Exemple #17
0
func smsVote(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)
	c.Infof("Send:", r.Body)

	//Data saving

	r.ParseForm()

	q := datastore.NewQuery("poll").Filter("Number =", r.Form.Get("To"))

	var poll Poll
	var key *datastore.Key

	t := q.Run(c)
	for {
		var p Poll
		k, err := t.Next(&p)
		if err == datastore.Done {
			break
		}
		if err != nil {
			c.Errorf("fetching next Person: %v", err)
			break
		}

		if inTimeSpan(p.Start, p.End, time.Now()) {
			poll = p
			key = k
			break
		}
	}

	q = datastore.NewQuery("vote").Filter("From =", r.Form.Get("From")).Filter("PollId =", strconv.FormatInt(key.IntID(), 10))

	t = q.Run(c)
	for {
		var p Vote
		_, err := t.Next(&p)
		if err == datastore.Done {
			break
		}
		if err != nil {
			c.Errorf("fetching next Person: %v", err)
			break
		}

		if p.Item == r.Form.Get("Body") {
			twiml := TwiML{Message: "You have already voted for this alternativ in this poll"}
			x, err := xml.Marshal(twiml)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			w.Header().Set("Content-Type", "application/xml")
			w.Write(x)
			return
		}
	}

	if !validOption(r.Form.Get("Body"), poll.Options) {
		twiml := TwiML{Message: "Invalid option. Options: " + poll.Options}
		x, err := xml.Marshal(twiml)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		w.Header().Set("Content-Type", "application/xml")
		w.Write(x)
		return
	}

	vote := Vote{
		Type:       "SMS",
		ClientId:   r.Form.Get("From"),
		MessageSid: r.Form.Get("MessageSid"),
		From:       r.Form.Get("From"),
		To:         r.Form.Get("To"),
		Body:       r.Form.Get("Body"),
		Item:       r.Form.Get("Body"),
		PollId:     strconv.FormatInt(key.IntID(), 10),
		Timestamp:  time.Now(),
	}

	_, err := datastore.Put(c, datastore.NewIncompleteKey(c, "vote", nil), &vote)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	//End reply
	twiml := TwiML{Message: "Thanks for your vote!"}
	x, err := xml.Marshal(twiml)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/xml")
	w.Write(x)
}
func translate(sourceKey *datastore.Key, translateAppId bool, targetAppId string, translateNamespace bool, targetNamespace string) (*datastore.Key, error) {
	if !translateAppId {
		targetAppId = sourceKey.AppID()
	}
	if !translateNamespace {
		targetNamespace = sourceKey.Namespace()
	}
	var translatedParent *datastore.Key = nil
	if sourceKey.Parent() != nil {
		var err error
		translatedParent, err = translate(sourceKey.Parent(), translateAppId, targetAppId, translateNamespace, targetNamespace)
		if err != nil {
			return nil, err
		}
	}
	return datastorekey.CreateKey(nil, targetAppId, targetNamespace, sourceKey.Kind(), sourceKey.StringID(), sourceKey.IntID(), translatedParent)
}
Exemple #19
0
func (g *Goon) setStructKey(src interface{}, key *datastore.Key) error {
	v := reflect.ValueOf(src)
	t := v.Type()
	k := t.Kind()

	if k != reflect.Ptr {
		return fmt.Errorf("goon: Expected pointer to struct, got instead: %v", k)
	}

	v = reflect.Indirect(v)
	t = v.Type()
	k = t.Kind()

	if k != reflect.Struct {
		return fmt.Errorf(fmt.Sprintf("goon: Expected struct, got instead: %v", k))
	}

	idSet := false
	kindSet := false
	parentSet := false
	for i := 0; i < v.NumField(); i++ {
		tf := t.Field(i)
		vf := v.Field(i)

		if !vf.CanSet() {
			continue
		}

		tag := tf.Tag.Get("goon")
		tagValues := strings.Split(tag, ",")
		if len(tagValues) > 0 {
			tagValue := tagValues[0]
			if tagValue == "id" {
				if idSet {
					return fmt.Errorf("goon: Only one field may be marked id")
				}

				switch vf.Kind() {
				case reflect.Int64:
					vf.SetInt(key.IntID())
					idSet = true
				case reflect.String:
					vf.SetString(key.StringID())
					idSet = true
				}
			} else if tagValue == "kind" {
				if kindSet {
					return fmt.Errorf("goon: Only one field may be marked kind")
				}
				if vf.Kind() == reflect.String {
					if (len(tagValues) <= 1 || key.Kind() != tagValues[1]) && g.KindNameResolver(src) != key.Kind() {
						vf.Set(reflect.ValueOf(key.Kind()))
					}
					kindSet = true
				}
			} else if tagValue == "parent" {
				if parentSet {
					return fmt.Errorf("goon: Only one field may be marked parent")
				}
				dskeyType := reflect.TypeOf(&datastore.Key{})
				vfType := vf.Type()
				if vfType.ConvertibleTo(dskeyType) {
					vf.Set(reflect.ValueOf(key.Parent()).Convert(vfType))
					parentSet = true
				}
			}
		}
	}

	if !idSet {
		return fmt.Errorf("goon: Could not set id field")
	}

	return nil
}
Exemple #20
0
/**
 * put session
 * @param  {[type]} w http.ResponseWriter [description]
 * @param  {[type]} r *http.Request       [description]
 * @return {[type]}   [description]
 */
func putSession(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	//Get the sessions id from the URL.
	id := mux.Vars(r)["id"]

	currentUserKey, _, _ := getCurrentUserKey(c, r)

	query := datastore.NewQuery("Session").Ancestor(currentUserKey).Filter("Draft =", true)
	// If the user is logged in fetch only their lists.
	sessions := []Session{}
	keys, err := query.GetAll(c, &sessions)
	if err != nil {
		SetError(w, r, 400, "")
		return
	}

	// Update the encoded keys and encode the lists.
	session := Session{}
	found := false
	var key *datastore.Key
	for i, k := range keys {
		if strconv.FormatInt(k.IntID(), 10) == id {
			found = true
			session = sessions[i]
			session.ID = k.IntID()
			key = k
		}
	}
	if !found {
		SetError(w, r, 404, "session not found")
		return
	}

	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		SetError(w, r, 400, "")
		return
	}
	if err := r.Body.Close(); err != nil {
		SetError(w, r, 400, "")
		return
	}

	postedSession := Session{}
	if err := json.Unmarshal(body, &postedSession); err != nil {
		SetError(w, r, 400, "")
		return
	}

	if err := validator.Validate(postedSession); err != nil {
		w.WriteHeader(400) // unprocessable entity
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
		return
	}

	postedSession.Added = time.Now().UnixNano() / int64(time.Millisecond)
	postedSession.Draft = false
	key, err = datastore.Put(c, key, &postedSession)
	if err != nil {
		SetError(w, r, 400, "")
		return
	}
	session.ID = key.IntID()

	SendEmail(r, "confirmed.html", map[string]string{"hostname": config.Get.HOSTNAME, "name": session.Firstname, "talk": session.SessionName}, "Votre session a bien été enregistré", session.Email, config.Get.EMAIL_SENDER)
	sendNotif(w, r)
	json.NewEncoder(w).Encode(postedSession)
	return
}
func (user *UserDTO) setKey(key *datastore.Key) *UserDTO {
	user.Key = strconv.FormatInt(key.IntID(), 10)
	return user
}
Exemple #22
0
// Return the (URL safe) string ID associated with the dinger key
func KeyToDingerId(key *datastore.Key) string {
	return fmt.Sprint(key.IntID())
}
Exemple #23
0
func rebuildKey(c appengine.Context, key *datastore.Key) *datastore.Key {
	if key == nil {
		return nil
	}
	return datastore.NewKey(c, key.Kind(), key.StringID(), key.IntID(), rebuildKey(c, key.Parent()))
}
Exemple #24
0
func (a *Audit) setKey(key *datastore.Key) {
	a.Key = key
	if key != nil {
		a.KeyID = key.IntID()
	}
}
Exemple #25
0
// ClearDevListForAccKey clears the cached Device list for the specified Account Key.
func ClearDevListForAccKey(c appengine.Context, accKey *datastore.Key) {
	mk := prefixDevListForAccKey + strconv.FormatInt(accKey.IntID(), 10)
	if err := memcache.Delete(c, mk); err != nil {
		c.Warningf("Failed to delete %s from memcache: %v", mk, err)
	}
}
Exemple #26
0
func createDocument(e env, projectId string) bool {
	userKey, done := getUser(e)
	if done {
		return true
	}

	snapshot, snapshotKey, done := getLastSnapshot(e, userKey, projectId)
	if done {
		return true
	}

	var nodeKey *datastore.Key

	err := datastore.RunInTransaction(e.ctx, func(ctx appengine.Context) error {
		node := Node{
			Folder: e.r.FormValue("folder") == "true",
		}
		var err error

		nodeKey, err = datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "node", snapshotKey), &node)
		if err != nil {
			return err
		}

		parentId, err := strconv.ParseInt(e.r.FormValue("parent"), 10, 64)
		if err != nil {
			return err
		}
		var parent Node
		parentKey := datastore.NewKey(ctx, "node", "", parentId, snapshotKey)
		if err = datastore.Get(ctx, parentKey, &parent); err != nil {
			return err
		}
		if !parent.Folder {
			return errors.New("Parent is not a folder")
		}

		parent.Children = append(parent.Children, nodeKey.IntID())
		if _, err = datastore.Put(ctx, parentKey, &parent); err != nil {
			return err
		}

		snapshot.Nodes = append(snapshot.Nodes, nodeKey.IntID())
		_, err = datastore.Put(ctx, snapshotKey, snapshot)
		return err
	}, nil)

	if err != nil {
		e.w.Header().Set("Content-type", "text/html; charset=utf-8")
		e.w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(e.w, "Could not create document")
		log.Println("Could not create document.", err)
		return true
	}

	if err = json.NewEncoder(e.w).Encode(map[string]int64{"id": nodeKey.IntID()}); err != nil {
		e.w.Header().Set("Content-type", "text/html; charset=utf-8")
		e.w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(e.w, "Nigh impossible error")
		log.Println("Could not make json out of id")
	}

	e.w.Header().Set("Content-Type", "application/json; charset=utf-8")
	return true
}
Exemple #27
0
func getTeamMemberByTeamAndMember(c appengine.Context, teamKey *datastore.Key, memberKey *datastore.Key, currentMem *Member) (*TeamMember, error) {
	var teamMember *TeamMember
	var err error

	if lookupOthers(currentMem) {
		q := datastore.NewQuery("TeamMember").
			Filter("TeamKey =", teamKey).
			Filter("MemberKey =", memberKey).
			Limit(1)

		var teamMembers []TeamMember
		var keys []*datastore.Key

		keys, err = q.GetAll(c, &teamMembers)

		if len(teamMembers) == 1 {
			for idx, _ := range teamMembers {
				tm := teamMembers[idx]
				key := keys[idx]

				tm.setKey(key)

				teamMember = &tm
			}

		} else {
			err = errors.New(fmt.Sprintf("Could not find TeamMember for Team: %d, Member: %d", teamKey.IntID(), memberKey.IntID()))
		}
	}

	if teamMember != nil {
		c.Debugf("TeamMember found: %+v", teamMember)
	}

	return teamMember, err
}
Exemple #28
0
func SendEmail(c appengine.Context, key *datastore.Key) error {
	// Lookup the publication
	var pub Publication
	c.Infof("Using key %s", key.StringID())
	if err := datastore.Get(c, key, &pub); err != nil {
		return err
	}

	// Update the mod times
	pub.LastChecked = time.Now()

	// Go and get the content
	client := urlfetch.Client(c)
	resp, err := client.Get(pub.Url)
	if err != nil {
		return err
	}
	if resp.StatusCode != 200 {
		return &UrlFetchError{key.IntID(), pub.Url, resp.Status}
	}

	buf := make([]byte, resp.ContentLength)
	_, err = resp.Body.Read(buf)
	if err != nil {
		return err
	}

	checksum := strconv.FormatUint(crc64.Checksum(buf, crc64.MakeTable(crc64.ISO)), 36)
	if checksum == pub.Checksum {
		c.Infof("Checksum has not changed.  Not sending mail.")
		return nil
	}

	c.Infof("Checksum for [%s] has changed.  Sending mail.", pub.Name)
	pub.Length = resp.ContentLength
	pub.LastChanged = time.Now()
	pub.Checksum = checksum

	doc := &mail.Attachment{
		Name: "Update from Enbook.me",
		Data: buf,
	}
	message := &mail.Message{
		Sender:      "Enbook.me <*****@*****.**>",
		To:          []string{pub.Email},
		Subject:     "",
		Body:        "Update from Enbook.me",
		Attachments: []mail.Attachment{*doc},
	}
	if err := mail.Send(c, message); err != nil {
		c.Errorf("Failed to send mail [%s].", pub.Name)
		return err
	}
	pub.LastUpdateSent = time.Now()

	// Save the changes
	if _, err = datastore.Put(c, key, &pub); err != nil {
		c.Errorf("Failed to save datastore entity [%s].", pub.Name)
		return err
	}
	return nil
}
Exemple #29
0
// register is the logic implementation of the Register page.
func register(p *page.Params) {
	// Register page is special. Unlike with other pages we do have to check User and Account here
	// Because even though Register page works with User, logged in user is not required for this page!

	// User might have logged out on a separate tab
	if p.User == nil {
		return
	}
	// User might have registered on a different tab
	if p.Account != nil {
		return
	}

	fv := p.Request.PostFormValue

	if fv("submitRegister") == "" {
		// No form submitted. Initial values:
		p.Custom["GoogleAccount"] = p.User.Email
		return
	}

	p.Custom["GoogleAccount"] = fv("googleAccount")
	p.Custom["ContactEmail"] = fv("contactEmail")
	p.Custom["AcceptTermsAndPolicy"] = fv("acceptTermsAndPolicy")

	// Checks:
	switch {
	case !checkGoogleAccounts(p, fv("googleAccount")):
	case !checkContactEmail(p, fv("contactEmail")):
	case !checkAcceptTermsAndPolicy(p, fv("acceptTermsAndPolicy")):
	}

	// UNTIL PROJECT GOES PUBLIC, DISABLE REGISTRATION:
	if !appengine.IsDevAppServer() && p.ErrorMsg == nil {
		p.ErrorMsg = "REGISTRATION IS CURRENTLY DISABLED! Contact the administrator if you would like to register!"
		return
	}
	// END OF: UNTIL PROJECT GOES PUBLIC, DISABLE REGISTRATION

	if p.ErrorMsg != nil {
		return
	}

	// All data OK, save new Account
	c := p.AppCtx
	acc := ds.Account{Email: p.User.Email, Lemail: strings.ToLower(p.User.Email), UserID: p.User.ID, ContactEmail: fv("contactEmail"), Created: time.Now()}
	var key *datastore.Key
	if key, p.Err = datastore.Put(c, datastore.NewIncompleteKey(c, ds.ENameAccount, nil), &acc); p.Err == nil {
		p.Custom["Created"] = true
		acc.KeyID = key.IntID()
		p.Account = &acc
		// Put new Account into the cache
		cache.CacheAccount(c, p.Account)
	}

	// Send registration email (Account info email)
	const adminEmail = "Andras Belicza <*****@*****.**>"
	msg := &mail.Message{
		Sender:  adminEmail,
		To:      []string{acc.Email},
		Bcc:     []string{adminEmail},
		ReplyTo: adminEmail,
		Subject: "[IczaGPS] Account Info",
		Body:    fmt.Sprintf(accountInfoMail, acc.Email),
	}
	if len(acc.ContactEmail) > 0 {
		msg.Cc = []string{acc.ContactEmail}
	}
	if err := mail.Send(c, msg); err == nil {
		c.Infof("Sent successful registration email.")
	} else {
		c.Errorf("Couldn't send email: %v", err)
	}
}
Exemple #30
0
/**
 * put draft
 * @param  {[type]} w http.ResponseWriter [description]
 * @param  {[type]} r *http.Request       [description]
 * @return {[type]}   [description]
 */
func putDraft(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	//Get the sessions id from the URL.
	id := mux.Vars(r)["id"]

	currentUserKey, _, _ := getCurrentUserKey(c, r)

	query := datastore.NewQuery("Session").Ancestor(currentUserKey).Filter("Draft =", true)
	// If the user is logged in fetch only their lists.
	sessions := []Session{}
	keys, err := query.GetAll(c, &sessions)
	if err != nil {
		SetError(w, r, 400, "")
		return
	}

	// Update the encoded keys and encode the lists.
	session := Session{}
	found := false
	var key *datastore.Key
	for i, k := range keys {
		if strconv.FormatInt(k.IntID(), 10) == id {
			found = true
			session = sessions[i]
			session.ID = k.IntID()
			key = k
		}
	}
	if !found {
		SetError(w, r, 404, "draft not found")
		return
	}

	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		SetError(w, r, 400, "")
		return
	}
	if err := r.Body.Close(); err != nil {
		SetError(w, r, 400, "")
		return
	}

	postedSession := Draft{}
	if err := json.Unmarshal(body, &postedSession); err != nil {
		SetError(w, r, 400, "")
		return
	}

	if err := validator.Validate(postedSession); err != nil {
		w.WriteHeader(400) // unprocessable entity
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
		return
	}

	postedSession.Added = time.Now().UnixNano() / int64(time.Millisecond)
	postedSession.Draft = true
	key, err = datastore.Put(c, key, &postedSession)
	if err != nil {
		SetError(w, r, 400, "")
		return
	}
	session.ID = key.IntID()

	json.NewEncoder(w).Encode(postedSession)
	return
}