Example #1
0
func RegisterDropbox(req *http.Request, render render.Render, registerDropboxForm RegisterDropboxForm, account *models.Account, logger *middlewares.Logger, ds *appx.Datastore) {
	logger.Infof("You are in register dropbox")

	authorization := &models.ExternalServiceAuthorization{
		AuthorizationType: models.DropBox,
		AccessToken:       registerDropboxForm.AccessToken,
		UserId:            models.DropBox.String() + "-" + registerDropboxForm.UserId,
	}

	authorization.SetParentKey(account.Key())

	err := ds.Load(authorization)
	if err != nil {
		println("I failed you becasue: %v", err.Error())
	}
	exists := err == nil

	if err := ds.Save(authorization); err != nil {
		logger.Errorf("Unable to register for dropbox %v", err)
		render.JSON(http.StatusInternalServerError, "Unable to register dropbox")
		return
	}

	if exists {
		DropboxDelta(req, ds, account, authorization)
	} else {
		DropboxInit(req, ds, account, authorization)
	}

	render.Status(http.StatusOK)
}
Example #2
0
func (trail *Trail) storeTags(context appengine.Context, db *appx.Datastore) {
	extractTags := func(data stream.T) stream.T {
		trail := data.(*Trail)
		return trail.AllTags()
	}

	err := rivers.FromData(trail).FlatMap(extractTags).Each(func(data stream.T) {
		tag := data.(*Tag)
		tag.SetParentKey(trail.ParentKey())
		tag.ImagePath = trail.Path
		tag.ImageProvider = DropBox
		datastore.RunInTransaction(context, func(c appengine.Context) error {
			if err := db.Load(tag); err == nil {
				tag.LikenessCount++
			} else {
				tag.LikenessCount = 1
			}

			return db.Save(tag)
		}, nil)
	}).Drain()
	if err != nil {
		println(">>>>Screwed", err.Error())
	}
}
func ExternalServiceAuthorizationProvider(ds *appx.Datastore, martiniContext martini.Context, account *models.Account) {
	authorization := &models.ExternalServiceAuthorization{}
	authorization.SetParentKey(account.Key())

	if err := ds.Load(authorization); err != nil {
		panic(err)
	}

	martiniContext.Map(authorization)
}
Example #4
0
func alreadyCategorized(ds *appx.Datastore) stream.PredicateFn {
	return func(data stream.T) bool {
		trail := data.(*models.Trail)

		err := ds.Load(trail)
		if err == datastore.ErrNoSuchEntity {
			return false
		}

		return err == nil
	}
}
Example #5
0
func Login(render render.Render, loginForm LoginForm, logger *middlewares.Logger, db *appx.Datastore, request *http.Request) {
	existingAccount := &models.Account{Id: loginForm.Id}
	if err := db.Load(existingAccount); err == nil {
		render.JSON(http.StatusOK, existingAccount)
		return
	}

	account := models.Accounts.New(loginForm.Id)
	if err := db.Save(account); err != nil {
		logger.Errorf("Error while trying to login for account by id: %v", err.Error())
		render.JSON(http.StatusBadRequest, err.Error())
		return
	}

	render.JSON(http.StatusCreated, account)
}
Example #6
0
func likeness(trailId string, likeness LikenessType, db *appx.Datastore, context appengine.Context) error {
	trail := Trail{}
	trail.SetEncodedKey(trailId)

	if err := db.Load(&trail); err != nil {
		println("The error: ", err.Error())
		return err
	}

	trail.Likeness = likeness
	trail.EvaluatedOn = time.Now()

	if trail.Likeness == LikedIt {
		trail.Tags = fetchLatLngFromGoogle(trail, context)
		trail.storeTags(context, db)
	}

	if err := db.Save(&trail); err != nil {
		println("The error: ", err.Error())
		return err
	}

	return nil
}
func FlagPostHandler(r render.Render, params martini.Params, appx *appx.Datastore) {
	postId := params["post_id"]

	response := model.Response{
		ErrorCode: http.StatusOK,
		Message:   []string{},
		Data:      nil,
	}

	post := model.Post{}
	post.SetEncodedKey(postId)
	err := appx.Load(&post)

	fmt.Print("aqui")
	if err != nil {
		fmt.Print("not found")
		fmt.Print(err)
		response.Message = []string{err.Error()}
		response.ErrorCode = http.StatusBadRequest
	} else {
		fmt.Print("found")
		fmt.Print(err)
		post.Flagged = true
		err = appx.Save(&post)

		if err != nil {
			fmt.Print("save problem")
			fmt.Print(err)
			response.Message = []string{err.Error()}
			response.ErrorCode = http.StatusInternalServerError
		} else {
			postResource := &resources.PostResource{}
			postResource.From(post)
			response.Data = postResource
		}
	}

	r.JSON(200, response)
}