Exemple #1
0
def vis_json_inout_pie(request):
    description = {
        "flow": ("string", "Flow"),
        "amount": ("number", "Amount"),
    }
    isum = get_egghead_from_user(request.user).total_income()
    osum = get_egghead_from_user(request.user).total_spending()
    data = [{
        "flow": _("Inflow"),
        "amount": isum
    }, {
        "flow": _("Outflow"),
        "amount": osum
    }]
    data_table = gviz_api.DataTable(description)
    data_table.LoadData(data)
    try:
        rid = int(request.GET["tqx"].split(":")[1].strip())
        # dblogger.debug("reqId is acquired from GET data: %d" % rid)
        return HttpResponse(
            data_table.ToJSonResponse(columns_order=("flow", "amount"),
                                      req_id=rid))
    except Exception, e:
        dblogger.debug(e)
        dblogger.debug("reqId is not found in GET, use 1 instead")
        return HttpResponse(
            data_table.ToJSonResponse(columns_order=("flow", "amount"),
                                      req_id=1))
Exemple #2
0
def egghead_edit(request, template="egghead/egghead.html"):
    if request.method == "POST":
        form = EggheadForm(request.POST)
        if form.is_valid():
            request.user.first_name = form.cleaned_data["first_name"]
            request.user.last_name = form.cleaned_data["last_name"]
            request.user.email = form.cleaned_data["email"]
            request.user.save()

            egghead = get_egghead_from_user(request.user)
            egghead.gtalk = form.cleaned_data["gtalk"]
            egghead.facebook = form.cleaned_data["facebook"]
            egghead.twitter = form.cleaned_data["twitter"]

            egghead.title = form.cleaned_data["title"]
            egghead.affiliation = form.cleaned_data["affiliation"]
            egghead.program_year = form.cleaned_data["program_year"]

            egghead.expertise_tags = form.cleaned_data["expertise_tags"]
            egghead.prior_experiences = form.cleaned_data["prior_experiences"]
            egghead.geographic_interests = form.cleaned_data[
                "geographic_interests"]
            egghead.sector_interests = form.cleaned_data["sector_interests"]
            egghead.save()

            profile = request.user.get_profile()
            profile.location = form.cleaned_data["location"]
            profile.about = form.cleaned_data["about"]
            profile.website = form.cleaned_data["website"]
            profile.save()
            return HttpResponseRedirect(
                reverse("egghead_detail", args=[request.user.username]))
    else:
        egghead = get_egghead_from_user(request.user)
        profile = request.user.get_profile()
        data = {
            "first_name": request.user.first_name,
            "last_name": request.user.last_name,
            "email": request.user.email,
            "gtalk": egghead.gtalk,
            "facebook": egghead.facebook,
            "twitter": egghead.twitter,
            "program_year": egghead.program_year,
            "prior_experiences": egghead.prior_experiences,
            "geographic_interests": egghead.geographic_interests,
            "sector_interests": egghead.sector_interests,
            "title": egghead.title,
            "affiliation": egghead.affiliation,
            "expertise_tags": egghead.expertise_tags,
            "location": profile.location,
            "about": profile.about,
            "website": profile.website,
        }
        form = EggheadForm(data)
    return render_to_response(template, {
        "form": form,
    },
                              context_instance=RequestContext(request))
def iknow_post_add_favorite(request):
    if request.method == "POST":
        try:
            question_id = request.POST["question_id"]
            question = Question.objects.get(pk=question_id)
            get_egghead_from_user(
                request.user).add_question_as_favorite(question)
            return HttpResponse("0")
        except Exception, e:
            klogger.warning(e)
            return HttpResponse("1")
 def execute(self):
     if self.src:
         egghead_src = get_egghead_from_user(self.src)
         egghead_src.wealth_notes = egghead_src.wealth_notes - self.amount
         egghead_src.save()
         egghead_src.update_record()
     if self.dst:
         egghead_dst = get_egghead_from_user(self.dst)
         egghead_dst.wealth_notes = egghead_dst.wealth_notes + self.amount
         egghead_dst.save()
         egghead_dst.update_record()
def iknow_ajax_dashboard(request, username, dtype):
    duser = User.objects.get(username=username)
    egghead = get_egghead_from_user(duser)
    if dtype == "asked":
        if duser.id == request.user.id:
            my_questions = egghead.get_questions_asked(max_item=-1)
        else:
            my_questions = egghead.get_questions_asked(max_item=-1,
                                                       purge_anonymous=True)
        title = _("Questions asked")
        template = "iknow/iknow_ajax_questions.html"
        qtype = "iasked"
        if not my_questions:
            return render_to_response(template, {
                "objs": None,
                "title": title,
                "order": "time",
                "page": 0,
                "total_pages": 0,
                "qtype": qtype,
                "home": False,
                "duser": duser,
            },
                                      context_instance=RequestContext(request))
        order = "time"
        page = 1
        if request.method == "POST":
            if request.POST.has_key("order"):
                order = request.POST["order"]
            if request.POST.has_key("page"):
                page = int(request.POST["page"])
        if order == "time":
            q_ordered = my_questions.order_by("-time_stamp")
        paginator = Paginator(q_ordered, 25)
        return render_to_response(template, {
            "objs": paginator.page(page),
            "title": title,
            "order": order,
            "page": page,
            "total_pages": paginator.num_pages,
            "qtype": qtype,
            "home": False,
            "duser": duser,
        },
                                  context_instance=RequestContext(request))

    elif dtype == "answered":
        try:
            if duser.id == request.user.id:
                my_answers = egghead.get_questions_answered(max_item=-1)
            else:
                my_answers = egghead.get_questions_answered(
                    max_item=-1, purge_anonymous=True)
            return render_to_response("iknow/iknow_ajax_myanswers.html", {
                "my_answers": my_answers,
            },
                                      context_instance=RequestContext(request))
        except Exception, e:
            klogger.warning(e)
def iknow_dashboard(request, username, template="iknow/iknow_dashboard.html"):
    duser = User.objects.get(username=username)
    egghead = get_egghead_from_user(duser)
    return render_to_response(template, {
        "duser": duser,
        "egghead": egghead,
    },
                              context_instance=RequestContext(request))
Exemple #7
0
def what_next(request, template="about/what_next.html"):
    egghead = get_egghead_from_user(request.user)
    features = Feature.objects.order_by("-creation_date")[:10]
    return render_to_response(template, {
        "egghead": egghead,
        "features": features,
    },
                              context_instance=RequestContext(request))
Exemple #8
0
def egghead_detail(request, username, template="egghead/egghead_detail.html"):
    person = User.objects.get(username=username)
    profile = person.get_profile()
    egghead = get_egghead_from_user(person)
    settings, created = Settings.objects.get_or_create(user=request.user)
    return render_to_response(template, {
        "person": person,
        "profile": profile,
        "settings": settings,
        "egghead": egghead,
    },
                              context_instance=RequestContext(request))
Exemple #9
0
def db_home(request, username, template="dashboard/db_home.html"):
    duser = User.objects.get(username=username)
    egghead = get_egghead_from_user(duser)
    total_user = EggHead.objects.count()
    max_wealth = EggHead.objects.aggregate(
        max_wealth=Max("wealth_notes"))["max_wealth"]
    return render_to_response(template, {
        "duser": duser,
        "max_wealth": max_wealth,
        "total_user": total_user,
        "egghead": egghead,
    },
                              context_instance=RequestContext(request))
def iknow_ajax_questions_fav(request, tribe_id, tag_id, template):
    title = _("Questions you have bookmarked")
    fqs = get_egghead_from_user(request.user).favorite_questions()
    if not tribe_id:
        fqs = fqs.filter(question__to_groups=None)
    else:
        tribe = Tribe.objects.get(pk=tribe_id)
        fqs = fqs.filter(question__to_groups=tribe)
    if tag_id:
        tag = QuestionTag.objects.get(pk=tag_id)
        fqs = fqs.filter(question__tags__icontains=tag.name)
    if not fqs:
        return render_to_response(template, {
            "objs": None,
            "title": title,
            "order": "time",
            "page": 0,
            "total_pages": 0,
            "qtype": "my_favorite",
            "home": False,
        },
                                  context_instance=RequestContext(request))
    order = "time"
    page = 1
    if request.method == "POST":
        if request.POST.has_key("order"):
            order = request.POST["order"]
        if request.POST.has_key("page"):
            page = int(request.POST["page"])
    if order == "time":
        fqs_ordered = fqs.order_by("-question__time_stamp")
    paginator = Paginator(fqs_ordered, 20)
    return render_to_response(template, {
        "objs": {
            "object_list":
            [fq.question for fq in paginator.page(page).object_list]
        },
        "title": title,
        "order": order,
        "page": page,
        "total_pages": paginator.num_pages,
        "qtype": "my_favorite",
        "home": False,
    },
                              context_instance=RequestContext(request))
def idoc_home(request, template="idoc/idoc_home.html"):
    if request.method == "POST":
        pass
    documents = IDocument.objects.order_by("-time_stamp")[:40]
    egghead = get_egghead_from_user(request.user)
    top_eggheads = EggHead.objects.order_by("-wealth_notes")[:10]
    top_eggheads_earning = EggHead.objects.order_by("-earning_total")[:10]
    top_tags = QuestionTag.objects.order_by("-count")[:10]
    tribes = Tribe.objects.filter(members=request.user)
    return render_to_response(template, {
        "docs": documents,
        "egghead": egghead,
        "top_eggheads": top_eggheads,
        "top_eggheads_earning": top_eggheads_earning,
        "top_tags": top_tags,
        "tribes": tribes,
    },
                              context_instance=RequestContext(request))
Exemple #12
0
def idesign_submit(request, template="idesign/idesign_submit.html"):
	egghead = get_egghead_from_user(request.user)
	form_error = list()
	if request.method == "POST":
		form = DesignIdeaForm(request.POST)
		is_valid = True
		points_offered = request.POST.get("points_offered", 0)
		if not points_offered: points_offered = 0
		if int(points_offered) > egghead.available_credits():
			is_valid = False
			form_error.append("Offered points exceed available credits")
		if form.is_valid() and is_valid:
			designidea = form.save(commit=False)
			designidea.creator = request.user
			designidea.time_stamp = datetime.now()
			if designidea.points_offered > 0:
				designidea.points_expiration = designidea.time_stamp + timedelta(days=int(60))
			cleaned_tags = []
			for tag in designidea.tags.split(","):
				cleaned_tag = utils.clean_tag(tag)
				if cleaned_tag:
					cleaned_tags.append(cleaned_tag)
					tag_obj, created = QuestionTag.objects.get_or_create(name=cleaned_tag)
					if not created: 
						tag_obj.update_count()
			if cleaned_tags:
				designidea.tags = ", ".join(cleaned_tags)
			designidea.points_final = designidea.points_offered
			designidea.save()
			worldbank_subsidize(request.user, "DI")
			if designidea.points_offered > 0:
				freezecredit = DesignIdeaFreezeCredit(time_stamp=datetime.now(), fuser=request.user, ftype="F",
					app="D", ttype="DS", amount=designidea.points_offered, designidea=designidea, cleared=False)
				freezecredit.save()
			return HttpResponseRedirect(reverse("idesign_home"))
		else:
			form_error.append("The form itself is not valid")
	else:
		form = DesignIdeaForm()
	return render_to_response(template, 
				{"form": form,
				 "form_error": form_error,
				},
				context_instance=RequestContext(request))
def iknow_home(request, template="iknow/iknow_home.html"):
    # for question in Question.objects.filter(status__in=("A","E","P")):
    #    question.update_time()
    my_questions = Question.objects.filter(status__in=("A","E","P"), asker=request.user)\
                    .order_by("-time_stamp")
    egghead = get_egghead_from_user(request.user)
    top_eggheads = EggHead.objects.order_by("-wealth_notes")[:10]
    top_eggheads_earning = EggHead.objects.order_by("-earning_total")[:10]
    top_tags = QuestionTag.objects.order_by("-count")[:10]
    tribes = Tribe.objects.filter(members=request.user)
    return render_to_response(template, {
        "egghead": egghead,
        "top_eggheads": top_eggheads,
        "top_eggheads_earning": top_eggheads_earning,
        "top_tags": top_tags,
        "tribes": tribes,
        "my_questions": my_questions,
    },
                              context_instance=RequestContext(request))
def iknow_question(request, question_id, template="iknow/iknow_question.html"):
    # question = Question.objects.get(pk=question_id)
    question = get_object_or_404(Question, pk=question_id)
    question.update_time()
    answers = question.get_answers(standard="thumbs")
    try:
        my_answer = Answer.objects.get(answerer=request.user,
                                       question=question)
        is_answered = True
        answer_form = AnswerForm(instance=my_answer)
    except:
        is_answered = False
        answer_form = AnswerForm()
    question.update_visits()
    if question.tags:
        results = search_questions(question.tags)
    else:
        results = search_questions(question.question_title)
    # results = results.exclude(index_pk=question.id)[:10]
    amendments = question.questionamendment_set.order_by("time_stamp")
    question_comments = question.questioncomment_set.order_by("-time_stamp")
    return render_to_response(template, {
        "question":
        question,
        "answer_form":
        answer_form,
        "amendments":
        amendments,
        "answers":
        answers,
        "is_answered":
        is_answered,
        "results":
        results,
        "is_my_favorite":
        get_egghead_from_user(request.user).has_question_as_favorite(question),
        "question_comments":
        question_comments,
    },
                              context_instance=RequestContext(request))
def iknow_ask(request, template="iknow/iknow_ask.html"):
    form_error = []
    egghead = get_egghead_from_user(request.user)
    tribes = request.user.tribes.all()
    if request.method == "POST":
        try:
            form = QuestionForm(request.POST)
        except IOError, e:
            klogger.warning("iknow_ask reading request.POST: %s" % e)
            return HttpResponseServerError
        if form.is_valid():
            question = Question()
            question.asker = request.user
            question.time_stamp = datetime.now()
            question.question_title = form.cleaned_data["title"]
            question.question_text = form.cleaned_data["details"]
            question.points_offered = form.cleaned_data["points_offered"]
            question.anonymous = form.cleaned_data["anonymous"]
            question.hide_answers = form.cleaned_data["hide_answers"]
            end_date = form.cleaned_data["end_date"]
            if question.points_offered > 0:
                question.points_expiration = datetime(
                    end_date.year, end_date.month, end_date.day,
                    int(form.cleaned_data["end_hour"]), 0, 0, 0)
            else:
                question.points_expiration = question.time_stamp + timedelta(
                    days=7)
            tags = form.cleaned_data["tags"]
            cleaned_tags = []
            for tag in tags.split(","):
                cleaned_tag = utils.clean_tag(tag)
                if cleaned_tag:
                    cleaned_tags.append(cleaned_tag)
                    tag_obj, created = QuestionTag.objects.get_or_create(
                        name=cleaned_tag)
                    if not created:
                        tag_obj.update_count()
            if cleaned_tags:
                question.tags = ", ".join(cleaned_tags)
            question.save()
            for key in request.POST:
                if key.startswith("tribeshare"):
                    tribe = Tribe.objects.get(pk=key.split("_")[1])
                    question.to_groups.add(tribe)
                if key.startswith("tribeemail"):
                    tribe = Tribe.objects.get(pk=key.split("_")[1])
            if question.points_offered > 0:
                freezecredit = FreezeCredit(time_stamp=datetime.now(),
                                            fuser=request.user,
                                            ftype="F",
                                            app="K",
                                            ttype="QA",
                                            amount=question.points_offered,
                                            question=question,
                                            cleared=False)
                freezecredit.save()

            # Reward 25 points if this is the asker's first question
            if request.user.questions_asked.count() == 1:
                worldbank_subsidize(question.asker, "KFQ", question)

            # Send a short message to users who subscribe, right now it's just a hack for Andy and me
            # To be fixed
            try:
                #recipients = ("5083188816", "6179092101", "6179011065")
                recipients = User.objects.filter(settings__new_q_notification_txt=True)\
                    .exclude(username=question.asker.username)
                phone_numbers = verified_phones(recipients)
                question_body = (
                    question.question_title + " - " +
                    question.question_text.replace("\n", " "))[:110]
                message = "Barter: " + question_body + " (prefix '" + str(
                    question.id) + "' to your answer)"
                sms_notify_list(phone_numbers, message)
            except Exception, e:
                klogger.info(e)

            try:
                recipients = User.objects.filter(settings__new_q_notification_email=True)\
                    .exclude(username=question.asker.username)
                current_site = Site.objects.get_current()
                subject = u"[ %s ] %s" % (
                    current_site.name, _("A new question entered the market"))
                t = loader.get_template("iknow/new_question.txt")
                c = Context({
                    "question": question,
                    "current_site": current_site,
                })
                message = t.render(c)
                from_email = settings.DEFAULT_FROM_EMAIL
                send_mail(subject, message, from_email,
                          verified_emails(recipients))
            except Exception, e:
                klogger.info(e)