本文整理汇总了Python中texcavator.utils.json_response_message函数的典型用法代码示例。如果您正苦于以下问题:Python json_response_message函数的具体用法?Python json_response_message怎么用?Python json_response_message使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了json_response_message函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: user_login
def user_login(request):
username = request.POST.get('username')
password = request.POST.get('password')
next_url = request.POST.get('next_url')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# TODO: are these date_limits really necessary?
date_limits = daterange2dates('')
dates = [date_limits['lower'], date_limits['upper']]
daterange = [int(d.replace('-', '')) for d in dates]
params = {
"user_id": user.id,
"user_name": user.username,
"daterange": daterange,
# TODO: what is timestamp used for? Is it really necessary
"timestamp": TIMESTAMP,
"next_url": next_url
}
return json_response_message('SUCCESS', '', params)
else:
return json_response_message('ERROR', 'Account disabled.\n'
'Please contact the system '
'administrator.')
return json_response_message('ERROR', 'Oops, that is not correct!')
示例2: doc_count
def doc_count(request):
"""
Returns the number of documents returned by the current query
"""
logger.info('services/doc_count/ - user: {}'.format(request.user.username))
if settings.DEBUG:
print >> stderr, "doc_count()"
params = get_search_parameters(request.GET)
result = count_search_results(settings.ES_INDEX,
settings.ES_DOCTYPE,
params['query'],
params['dates'],
params['distributions'],
params['article_types'],
params['pillars'])
count = result.get('count', None)
if count:
params = {'doc_count': count}
logger.info('services/doc_count/ - returned calculated count.')
return json_response_message('ok', 'Retrieved document count.', params)
logger.info('services/doc_count/ - returned "unable to retrieve".')
return json_response_message('error', 'Unable to retrieve document count')
示例3: check_status_by_task_id
def check_status_by_task_id(request, task_id):
"""Returns the status of the generate_tv_cloud task
If the task is finished, the results of the task are returned.
"""
if not request.is_ajax():
return json_response_message('ERROR', 'No access.')
# TODO: use generic AsyncResult (from celery.result import AsyncResult)
# so this function can be used to check the status of all asynchronous
# tasks. However, when this import statement is put in this module, an
# error is produced (celery module has no attribute result).
# When typing this import statement in the Python
# console or in the Django interactive shell, there is no error message.
task = generate_tv_cloud.AsyncResult(task_id)
try:
if task.ready():
if task.successful():
return json_response_message('ok', '', task.get())
else:
return json_response_message('ERROR', 'Generating word cloud '
'failed.')
else:
return json_response_message('WAITING', task.status, task.result)
except AttributeError as e:
return json_response_message('ERROR', 'Other error: {}'.format(str(e)))
示例4: search
def search(request):
"""Perform search request and return html string with results"""
logger.info('services/search/ - user: {}'.format(request.user.username))
params = get_search_parameters(request.REQUEST)
valid_q, result = do_search(settings.ES_INDEX,
settings.ES_DOCTYPE,
params['query'],
params['start']-1, # Zero based counting
params['result_size'],
params['dates'],
params['distributions'],
params['article_types'],
params['pillars'],
sort_order=params['sort_order'])
if valid_q:
html_str = elasticsearch_htmlresp(settings.ES_INDEX,
params['start'],
params['result_size'],
result)
return json_response_message('ok', 'Search completed', {'html': html_str})
else:
result = escape(result).replace('\n', '<br />')
msg = 'Unable to parse query "{q}"<br /><br />'. \
format(q=params['query'])
msg = msg + result.replace('\n', '<br />')
return json_response_message('error', msg)
示例5: search
def search(request):
"""Perform search request and return html string with results"""
logger.info('services/search/ - user: {}'.format(request.user.username))
params = get_search_parameters(request.GET)
if not validate_dates(params['dates']):
msg = 'You entered an invalid date range. Please check your date filters.'
return json_response_message('error', msg)
valid_q, result = do_search(settings.ES_INDEX,
settings.ES_DOCTYPE,
params['query'],
params['start']-1, # Zero based counting
params['result_size'],
params['dates'],
params['distributions'],
params['article_types'],
params['pillars'],
sort_order=params['sort_order'])
if valid_q:
return json_response_message('ok', 'Search completed', {'hits': result['hits']})
else:
result = escape(result).replace('\n', '<br />')
msg = 'Unable to parse query "{q}"<br /><br />'. \
format(q=params['query'])
msg = msg + result.replace('\n', '<br />')
return json_response_message('error', msg)
示例6: get_query
def get_query(request, query_id):
"""
Returns a single Query, checks if Query belongs to User.
"""
query = get_object_or_404(Query, pk=query_id)
if not request.user == query.user:
return json_response_message('ERROR', 'Query does not belong to user.')
return json_response_message('OK', '', {'query': query.get_query_dict()})
示例7: retrieve_document
def retrieve_document(request, doc_id):
"""Retrieve a document from the ES index"""
logger.info('services/retrieve/{}'.format(doc_id))
document = get_document(settings.ES_INDEX, settings.ES_DOCTYPE, doc_id)
if document:
return json_response_message('SUCCESS', '', document)
return json_response_message('ERROR', 'Document not found.')
示例8: delete_stopword
def delete_stopword(request, stopword_id):
"""
Deletes a Stopword.
"""
stopword = StopWord.objects.get_object_or_404(pk=stopword_id)
if request.user != stopword.user:
return json_response_message('ERROR', 'Stopword does not belong to this user.')
msg = 'Stopword {} deleted.'.format(stopword.word)
stopword.delete()
return json_response_message('SUCCESS', msg)
示例9: query
def query(request, query_id):
"""Returns a query.
"""
query = get_object_or_404(Query, pk=query_id)
if not request.user == query.user:
return json_response_message('ERROR', 'Query does not belong to user.')
params = {
'query': query.get_query_dict()
}
return json_response_message('OK', '', params)
示例10: delete_stopword
def delete_stopword(request, stopword_id):
"""Deletes a stopword from the stopword list.
"""
stopword = StopWord.objects.get(pk=stopword_id)
if not stopword:
return json_response_message('ERROR', 'Stopword not found.')
if not request.user == stopword.user:
return json_response_message('ERROR', 'Stopword does not belong to '
'user.')
stopword.delete()
return json_response_message('SUCCESS', 'Stopword deleted.')
示例11: delete
def delete(request, query_id):
"""Deletes a query.
"""
query = Query.objects.get(pk=query_id)
if not query:
return json_response_message('ERROR', 'Query not found.')
if not request.user == query.user:
return json_response_message('ERROR', 'Query does not belong to user.')
q = query.title
query.delete()
return json_response_message('SUCCESS', 'Query "{}" deleted.'.format(q))
示例12: update
def update(request, query_id):
"""
Updates an existing Query.
"""
query = Query.objects.get(pk=query_id)
if not query:
return json_response_message('ERROR', 'Query not found.')
if not request.user == query.user:
return json_response_message('ERROR', 'Query does not belong to user.')
params = get_search_parameters(request.POST)
try:
query.query = params['query']
query.title = request.POST.get('title')
query.comment = request.POST.get('comment')
query.save()
Period.objects.filter(query__pk=query_id).delete()
for date_range in params['dates']:
date_lower = datetime.strptime(date_range['lower'], '%Y-%m-%d')
date_upper = datetime.strptime(date_range['upper'], '%Y-%m-%d')
p = Period(query=query, date_lower=date_lower, date_upper=date_upper)
p.save()
query.exclude_distributions.clear()
for distr in Distribution.objects.all():
if distr.id in params['distributions']:
query.exclude_distributions.add(distr)
query.exclude_article_types.clear()
for art_type in ArticleType.objects.all():
if art_type.id in params['article_types']:
query.exclude_article_types.add(art_type)
query.selected_pillars.clear()
for pillar in Pillar.objects.all():
if pillar.id in params['pillars']:
query.selected_pillars.add(pillar)
query.nr_results = count_results(query)
query.save()
except Exception as e:
return json_response_message('ERROR', str(e))
return json_response_message('SUCCESS', 'Query updated.')
示例13: metadata
def metadata(request):
"""This view will show metadata aggregations"""
params = get_search_parameters(request.REQUEST)
result = metadata_aggregation(settings.ES_INDEX,
settings.ES_DOCTYPE,
params['query'],
params['dates'],
params['distributions'],
params['article_types'],
params['pillars'])
# Categorize newspaper_ids per Pillar
pillars = Counter()
for n in result['aggregations']['newspaper_ids']['buckets']:
pillar = 'None'
try:
newspaper = Newspaper.objects.get(pk=n['key'])
except Newspaper.DoesNotExist:
# TODO: this means there's a paper_dc_identifier in ElasticSearch without a corresponding Newspaper.
newspaper = None
if newspaper and newspaper.pillar:
pillar = newspaper.pillar.name
pillars[pillar] += n['doc_count']
# Mimic the result of the other aggregations
result['aggregations']['pillar'] = [{'key': k, 'doc_count': v} for (k, v) in pillars.iteritems()]
return json_response_message('success', 'Complete', result['aggregations'])
示例14: index
def index(request):
"""
Returns the list of Queries for the current User.
"""
queries = Query.objects.filter(user=request.user).order_by('-date_created')
queries_json = [q.get_query_dict() for q in queries]
return json_response_message('OK', '', {'queries': queries_json})
示例15: cancel_by_task_id
def cancel_by_task_id(request, task_id):
"""Cancel Celery task.
"""
logger.info('services/cancel_task/{}'.format(task_id))
AsyncResult(task_id).revoke(terminate=True)
return json_response_message('ok', '')