コード例 #1
0
ファイル: test_helpers.py プロジェクト: drewpearce/data_utils
def test_to_json():
    assert helpers.to_json(None) is None
    assert helpers.to_json(True) is True
    assert helpers.to_json('test') == 'test'

    data = ['abc', 'def']
    expected = '[\n  "abc",\n  "def"\n]'
    assert helpers.to_json(data) == expected

    data = {'abc': 1, 'def': ['uvw', 'xyz']}
    expected = '{\n  "abc": 1,\n  "def": [\n    "uvw",\n    "xyz"\n  ]\n}'
    assert helpers.to_json(data) == expected
コード例 #2
0
def get_list():
	args = request.args
	page = 1 if 'page' not in args else int(args['page'])
	limit = 10 if 'limit' not in args else int(args['limit'])
	filters = {}
	if 'source' in args:
		filters['source'] = args['source']

	if 'like' in args:

		ids = elasticsearch_like(args['like'])
		query = Vacancy.query.filter_by(**filters).filter(Vacancy.id.in_(ids)).slice((page-1)*limit, page*limit)
		amount = len(Vacancy.query.filter_by(**filters).filter(Vacancy.id.in_(ids)).all())
	else:
		query = Vacancy.query.filter_by(**filters).slice((page-1)*limit, page*limit)
		amount = len(Vacancy.query.filter_by(**filters).all())

	pages_total = round(amount/limit)
	
	if page <=5:
		last_page=10 if pages_total>10 else pages_total
		first_page=1
	elif pages_total - page <=5:
		first_page = pages_total-10 if pages_total>10 else 1
		last_page = pages_total
	else :
		last_page = page+5
		first_page = page-4

	return jsonify(**{"data":[to_json(q)['fields'] for q in query.all()], 'current_page':page,'first_page': first_page, 'last_page':last_page})
コード例 #3
0
ファイル: handlers.py プロジェクト: bederson/qa
    def get(self):
        person = self.initUserContext()
        question_id = self.request.get("question_id")
        questionObj = Question.getQuestionById(question_id)
        phase = Question.getPhase(questionObj)

        isComparePhase = phase == PHASE_COMPARE_BY_SIMILARITY and questionObj
        maxNumToCompare = questionObj.getNumNotesToComparePerPerson() if isComparePhase else 0
        numNotesForComparison = questionObj.getNumNotesForComparison() if isComparePhase else 0
        assignment = SimilarIdeaAssignment.getCurrentAssignment(questionObj, person) if isComparePhase else None

        if assignment and len(assignment.compareToKeys) < numNotesForComparison:
            assignment = None

        template_values = get_default_template_values(self, person, questionObj)
        template_values["assignment"] = helpers.to_json(assignment.toDict() if assignment else None)
        template_values["num_notes_to_compare"] = maxNumToCompare
        template_values["num_notes_for_comparison"] = numNotesForComparison
        path = os.path.join(os.path.dirname(__file__), "../html/similar.html")
        self.response.out.write(template.render(path, template_values))
コード例 #4
0
ファイル: fields.py プロジェクト: davidrenne/django-history
 def get_db_prep_save(self, value, connection, prepared=False):
     if value is None: return
     return to_json(value)
コード例 #5
0
ファイル: handlers.py プロジェクト: bederson/qa
 def writeResponseAsJson(self, data):
     self.response.headers.add_header("Content-Type", "application/json", charset="utf-8")
     self.response.out.write(helpers.to_json(data))