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.')
def update(request, query_id): """Updates 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.') params = get_search_parameters(request.POST) title = request.POST.get('title') comment = request.POST.get('comment') date_lower = datetime.strptime(params['dates']['lower'], '%Y-%m-%d') date_upper = datetime.strptime(params['dates']['upper'], '%Y-%m-%d') try: Query.objects.filter(pk=query_id).update(query=params['query'], title=title, comment=comment, date_lower=date_lower, date_upper=date_upper) 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) except Exception as e: return json_response_message('ERROR', str(e)) return json_response_message('SUCCESS', 'Query saved.')
def create_query(request): """ Creates a new Query. """ params = get_search_parameters(request.POST) try: q = Query(query=params['query'], title=request.POST.get('title'), comment=request.POST.get('comment'), user=request.user) q.save() 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=q, date_lower=date_lower, date_upper=date_upper) p.save() for distr in Distribution.objects.all(): if distr.id in params['distributions']: q.exclude_distributions.add(distr) for art_type in ArticleType.objects.all(): if art_type.id in params['article_types']: q.exclude_article_types.add(art_type) for pillar in Pillar.objects.all(): if pillar.id in params['pillars']: q.selected_pillars.add(pillar) q.nr_results = count_results(q) q.save() except IntegrityError as _: return json_response_message( 'ERROR', 'A query with this title already exists.') except Exception as e: return json_response_message('ERROR', str(e)) return json_response_message('SUCCESS', '')
def create_query(request): """ Creates a new Query. """ params = get_search_parameters(request.POST) try: q = Query(query=params['query'], title=request.POST.get('title'), comment=request.POST.get('comment'), user=request.user) q.save() 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=q, date_lower=date_lower, date_upper=date_upper) p.save() for distr in Distribution.objects.all(): if distr.id in params['distributions']: q.exclude_distributions.add(distr) for art_type in ArticleType.objects.all(): if art_type.id in params['article_types']: q.exclude_article_types.add(art_type) for pillar in Pillar.objects.all(): if pillar.id in params['pillars']: q.selected_pillars.add(pillar) q.nr_results = count_results(q) q.save() except IntegrityError as _: return json_response_message('ERROR', 'A query with this title already exists.') except Exception as e: return json_response_message('ERROR', str(e)) return json_response_message('SUCCESS', '')