def recent_capsules(request): query_dict = request.GET limit = query_dict.get('limit', 10) to_time = int_or_none(query_dict.get('to_time')) to_time = datetime.datetime.fromtimestamp(to_time) if to_time else datetime.datetime.utcnow() to_time = to_time.replace(tzinfo=utc) capsules = Capsule.objects \ .filter(authors=request.user, last_modified__lt=to_time) \ .order_by('-last_modified')[:limit] caps = sanitize_capsule_list(capsules) return HttpResponse(json.dumps(caps, cls=MyEncoder), content_type="application/json")
def filter_capsules(request): query_dict = request.GET limit = query_dict.get('limit', 10) author_ids = query_dict.get('author_ids', '').split(',') author_usernames = query_dict.get('author_usernames') author_usernames = author_username.split(',') if author_usernames else [] author_ids = [int_or_none(aid) for aid in author_ids if int_or_none(aid) != None] authors = User.objects.filter(Q(pk__in=author_ids) | Q(username__in=author_usernames)) # tags = query_dict.get('tags', '') title = query_dict.get('title') text = query_dict.get('text') # for now this sucks since it has to match exactly path = query_dict.get('path') created_before = int_or_none(query_dict.get('created_before')) created_after = int_or_none(query_dict.get('created_after')) modified_before = int_or_none(query_dict.get('modified_before')) modified_after = int_or_none(query_dict.get('modified_after')) # can have this crazy structure of capsule = capsule.filter(...) without # submitting many queries to the db because the django ORM is smart and only # actually performs a query when the variable's information is used. capsules = Capsule.objects.all() if authors: capsules = capsules.filter(authors__in=authors) # if tags: # tags = tags.split(',') # capsules = capsules.filter(tags__name__in=tags) if title: capsules = capsules.filter(title=title) if text: capsules = capsules.filter(text=text) if path: capsules = capsules.filter(path=path) if created_before: capsules = capsules.filter(first_created__lt= datetime.datetime.utcfromtimestamp(created_before) \ .replace(tzinfo=utc)) if created_after: capsules = capsules.filter(first_created__gt= datetime.datetime.utcfromtimestamp(created_after) \ .replace(tzinfo=utc)) if modified_before: capsules = capsules.filter(last_modified__lt= datetime.datetime.utcfromtimestamp(modified_before) \ .Replace(tzinfo=utc)) if modified_after: capsules = capsules.filter(last_modified__gt= datetime.datetime.utcfromtimestamp(modified_after) \ .replace(tzinfo=utc)) return HttpResponse(json.dumps(sanitize_capsule_list(capsules[:limit]), cls=MyEncoder), content_type="application/json")
current_insp.append(field) if j <= 7 and is_new_rest: current_rest.append(field) elif j > 7: current_insp.append(field) if len(current_rest) > 0: session.add( \ Restaurant( \ id=int(current_rest[0]), \ name=current_rest[1], \ boro=current_rest[2], \ building=current_rest[3], \ street=current_rest[4], \ zip=int_or_none(current_rest[5]), \ phone=current_rest[6], \ cuisine=current_rest[7], \ ) \ ) session.add( \ Inspection( \ rest_id=int(current_insp[0]), \ inspection_date=str_to_date(current_insp[1]), \ action=current_insp[2], \ violation_code=current_insp[3], \ violation_desc=current_insp[4], \ is_critical=current_insp[5] == 'Critical', \ score=int_or_none(current_insp[6]), \ grade=grade_or_none(current_insp[7]), \