Exemplo n.º 1
0
/**
	POST/PUT:	submit
	GET(or else):	Get info
**/
func handleRecommendation(resp http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	hash := vars["hash"]

	appDb := public.GetNewApplicationDatabase()
	defer appDb.Session.Close()

	recomm := appDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
	q := recomm.Find(bson.M{
		"hash": hash,
	})
	if n, err := q.Count(); err != nil || n <= 0 || len(hash) <= 0 {
		public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
			Message:     "Error",
			Description: "No Such page",
		})
		return
	}

	result := db.Recomm{}
	if err := q.One(&result); err != nil {
		public.LogE.Printf("Error fetching recommendation data for %s\n", hash)
		public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
			Message: "Error",
		})
		return
	}

	if req.Method == "POST" || req.Method == "PUT" {
		//Submit
		if result.Submitted { //Already submitted
			public.ResponseStatusAsJson(resp, 400, &public.SimpleResult{
				Message: "Error",
			})
			return
		}

		textContent := req.FormValue("textContent")
		fileObj := req.FormValue("fileObj")

		result.Content = textContent
		result.Attachment = fileObj
		result.Submitted = true
		err := recomm.UpdateId(result.Id, &result)
		if err != nil {
			public.LogE.Println("Update recommendation fields error: " + err.Error())
			public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
				Message: "Error",
			})
		} else {
			public.ResponseOkAsJson(resp, &public.SimpleResult{
				Message: "Success",
			})
		}
	} else {
		//Get info
		displayResult := public.RecommResult{
			Recommender: result.Recommender,
			ApplyUser:   result.ApplyUser,
			Done:        result.Submitted,
		}
		public.ResponseOkAsJson(resp, &displayResult)
	}
}
Exemplo n.º 2
0
func (this *exportApplication) fromDbApplication(form *db.ApplicationForm, isReviewer bool) {
	this.Timestamp = form.Timestamp

	this.Name = form.Name
	this.School = form.School
	this.Department = form.Department
	this.SchoolGrade = form.SchoolGrade
	this.Birthday = form.Birthday
	this.FormalId = form.FormalId
	this.Phone = form.Phone
	this.Email = form.Email
	this.Address = form.Address

	this.Topic = form.Topic
	this.Teacher = form.Teacher
	this.ResearchArea = form.ResearchArea
	this.ClassHistories = form.ClassHistories
	this.RelatedSkills = form.RelatedSkills
	this.AcademicGrade = form.AcademicGrade
	this.LangAbilities = form.LangAbilities

	//Hash
	this.Hash = public.NewSecureHashString()

	//Extras
	//Transform file id to url
	if client, err := storage.GetNewStorageClient(); err == nil {

		expireTime := time.Now().Add(time.Duration(1) * time.Hour) //an hour
		if obj, e := client.GetNewSignedURL(form.ResearchPlan, expireTime); e == nil {
			this.ResearchPlan = obj
		} else {
			public.LogE.Println("Get object error: " + e.Error())
		}
		if obj, e := client.GetNewSignedURL(form.Transcript, expireTime); e == nil {
			this.Transcript = obj
		} else {
			public.LogE.Println("Get object error: " + e.Error())
		}
		if len(form.Others) > 0 {
			if obj, e := client.GetNewSignedURL(form.Others, expireTime); e == nil {
				this.Others = obj
			} else {
				public.LogE.Println("Get object error: " + e.Error())
			}
		}
	} else {
		public.LogE.Printf("Error getting storage client")
	}

	appDb := public.GetNewApplicationDatabase()
	defer appDb.Session.Close()
	recomm := appDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
	var recommList []public.RecommResult

	for _, h := range form.Recommendations {
		//Transform recommendation from hash to structures
		q := recomm.Find(bson.M{
			"hash": h,
		})

		if n, e := q.Count(); e == nil && n > 0 {
			r := db.Recomm{}
			if e := q.One(&r); e == nil {
				r := public.RecommResult{
					Recommender: r.Recommender,
					ApplyUser:   r.ApplyUser,
					Done:        r.Submitted,
				}
				if isReviewer {
					r.Hash = h
				} else {
					r.Hash = ""
				}
				recommList = append(recommList, r)
			}
		}
	}
	this.Recommendations = recommList
}