def get(self, request): result = json.loads(requests.get("https://datausa.io/api/covid19/states").text) filtered_list = list( filter( lambda item: ('2020/07/' in item['Date'] or '2020/08' in item['Date']) and item['Geography'] != 'American Samoa', result['data'])) for i in filtered_list: if State.objects.filter(name=i['Geography']).exists(): state_id = State.objects.get(name=i['Geography']).id else: state = State( name =i['Geography'], populations=i['Population'], is_deleted =False, created_at =Now(), updated_at =Now() ) state.save() state_id = state.id test = Test( state_id =state_id, count =i['Tests'], is_deleted=False, created_at=Now(), updated_at=Now() ) test.save() test_result = TestResult( test_id =test.id, confirmed_growth =i['ConfirmedGrowth'], confirmed =i['Confirmed'], confirmed_per_capita=i['ConfirmedPC'], positive_percent =i['PositivePct'], is_deleted =False, created_at =Now(), updated_at =Now() ) test_result.save() hospital_status = HospitalStatus( state_id =state_id, hospitalized=i['Hospitalized'], death =i['Deaths'], is_deleted =False, created_at =Now(), updated_at =Now() ) hospital_status.save() return JsonResponse({'result': "daily_crawling_success", 'data': filtered_list}, status=200)
def _user_from_http_request__session_token(request): if 'HTTP_X_SESSION_TOKEN' in request.META: session_token = request.META['HTTP_X_SESSION_TOKEN'] session_token_uuid = None try: session_token_uuid = uuid.UUID(session_token) except ValueError: return None session = None try: session = models.Session.objects.prefetch_related('user').get( Q(uuid=session_token_uuid) & (Q(expires_at__isnull=True) | Q(expires_at__gt=Now()))) except models.Session.DoesNotExist: return None if session: if session.expires_at is not None: session.expires_at = (datetime.datetime.utcnow() + _SESSION_EXPIRY_INTERVAL) session.save(update_fields=['expires_at']) return session.user return None
def get_model_instance(cls): return Action(timestamp=Now(), action_type=False, session=SessionTest.model_create(), participant=ParticipantTest.model_create(), quantity=2, event=EventTest.model_create())
def get_tracks(self) -> Tuple[Iterable[Track], int]: qs = self.get_base_queryset() if self.kwargs.get('mode') == 'pro': qs = self.pro_queryset(qs) elif self.kwargs.get('mode') == 'hipster': qs = qs.filter(play=None) elif self.kwargs.get('mode') == 'almost-100': qs = qs.exclude( play__date__gt=Show.current().end - datetime.timedelta(days=(7 * 80)), ).exclude(play=None) elif self.kwargs.get('mode') == 'decade': qs = qs.for_decade( int(self.kwargs.get('decade', self.default_decade))) elif self.kwargs.get('mode') == 'staple': # Staple track: having been played more than once per year(ish) # since the track was made available. Exclude tracks that don't # yet have enough plays to be reasonably called a "staple". qs = (qs.annotate(plays=Count('play')).filter( plays__gt=2).annotate(time_per_play=Cast( ((Now() - F('revealed')) / F('plays')), output_field=DurationField())).filter( time_per_play__lt=parse_duration('365 days'))) # order_by('?') fails when annotate() has been used return (sample(list(qs), 5), qs.count()) elif self.kwargs.get('mode') == 'short': length_msec = int( self.kwargs.get('minutes', self.default_minutes_count)) * 60 * 1000 qs = qs.filter(msec__gt=length_msec - 60_000, msec__lte=length_msec) return (qs.order_by('?')[:5], qs.count())
def AjaxCreatePlanning(request): if request.method == 'POST': planning = Planning.objects.filter(lead=request.POST.get('id'), active=False) if (planning): return HttpResponse('Finish plans') elif (request.POST.get('date') == ''): return HttpResponse('Set date') else: format = '%d/%m/%Y %H:%M' start = datetime.strptime(request.POST.get('date'), format).strftime('%Y-%m-%d %H:%M') lead = Leads.objects.get(id=request.POST.get('id')) Planning.objects.create(lead=lead, manager=lead.manager.closer, creator=request.user.username, type=request.POST.get('type'), update=start) lastid = Leads.objects.latest().id Leads.objects.filter(id=lastid).update(update=Now()) return HttpResponse('ok') return HttpResponse('Bad request')
def push_message(self, message): LineMessageQueue(employee=self, scheduled_at=Now(), message=json.dumps({ 'M': 't', 'text': message })).save()
def get(self, request): user = request.user plans = settings.PLAN_DEFINITIONS try: current_plan = Subscriptions.objects.get( account=user.account, period__contains=Now() ).plan except Subscriptions.DoesNotExist: current_plan = None try: limit = plans[current_plan]["limit"] except KeyError: limit = 0 count = {"domestic": 0, "international": 0} savings = {"domestic": 0, "international": 0} # TODO: optimize db usage (aggregate on DB) trips_booked = booking_models.BookingContact.objects.filter(user__account=user.account) for f in trips_booked: kind = "domestic" if f.is_domestic() else "international" count[kind] += 1 savings[kind] += max(0, AVERAGE_PRICES[kind] - f.data["conversion"]["USD"]) data = { "savings": savings, "count": count, "remaining": limit - sum(count.values()) if limit is not None else None } return Response(data)
def filter_for_api(self, academic_year): from partnership.models import PartnershipYear, PartnershipAgreement return self.annotate( current_academic_year=models.Value( academic_year.id, output_field=models.AutoField()), ).annotate( has_years_in=models.Exists( PartnershipYear.objects.filter( partnership=OuterRef('partnership_id'), academic_year=academic_year, )), has_valid_agreement_in_current_year=models.Exists( PartnershipAgreement.objects.filter( partnership=OuterRef('partnership_id'), status=AgreementStatus.VALIDATED.name, start_academic_year__year__lte=academic_year.year, end_academic_year__year__gte=academic_year.year, )), ).filter( # If mobility, should have agreement for current year # and have a partnership year for current year Q( partnership__partnership_type=PartnershipType.MOBILITY.name, has_valid_agreement_in_current_year=True, has_years_in=True, ) # Else all other types do not need agreement | (~Q(partnership__partnership_type=PartnershipType.MOBILITY.name) & Q(partnership__end_date__gte=Now())), # And must be public partnership__is_public=True, )
def auto_update_sensitivity_api(): all_files = SensitiveFiles.objects.all() for file in all_files: read_file = open(file.uploadfile.path, 'r') read_file_data = read_file.read() total_words = read_file_data.split() sensitivity = 0 array1 = [] for word in total_words: lower_word = word.lower() clean_lower_word = lower_word.strip("!@#$%^&*()_+-=<>,.?/~`") array1.append(clean_lower_word) for each in array1: if each == "secret": sensitivity += 10 elif each == "dathena": sensitivity += 7 elif each == "internal": sensitivity += 5 elif each == "external": sensitivity += 3 elif each == "public": sensitivity += 1 read_file.close() file_to_update = SensitiveFiles.objects.filter(pk=file.id) file_to_update.update(sensitivity=sensitivity) file_to_update.update(updated=Now()) print(f'{file}: {file.sensitivity}')
def get_service(queryset, latlong): for filtered_queryset in ( queryset, queryset.filter( Exists( Route.objects.filter( Q(end_date__gte=Now()) | Q(end_date=None), Q(start_date__lte=Now()) | Q(start_date=None), service=OuterRef('id')))), queryset.filter(geometry__bboverlaps=latlong.buffer(0.1)), queryset.filter(geometry__bboverlaps=latlong.buffer(0.05)), queryset.filter(geometry__bboverlaps=latlong)): try: return filtered_queryset.get() except queryset.model.MultipleObjectsReturned: continue
class ExpirableLinkModelView(viewsets.ModelViewSet): serializer_class = ExpirableLinkSerializer queryset = ExpirableLink.objects.annotate( expiration_time=ExpressionWrapper( F('time_created') + F('experation_period'), output_field=DateTimeField()) ).filter(expiration_time__gt=Now()) permission_classes = [IsCreationOfExpirableLinkAllowedOrReadOnly]
def now_function(): ''' 返回当前日期之前发布的应用 :return: ''' apps = App.objects.filter(publish_date__lte=Now()) print(apps)
def push_message(self, message, scheduled_at=Now()): LineMessageQueue(customer=self, scheduled_at=scheduled_at, message=json.dumps({ 'M': 't', 'text': message })).save()
def stock_update_view(request, *args, **kwargs): if request.method == 'POST': form = StockForm(request.POST) if form.is_valid(): prod = form.cleaned_data.get('name') qt = form.cleaned_data.get('qt') ordered = list(OrderedProduct.objects.filter(product_id=prod.id)) ordered.sort(key=sort_id) for i in range(qt): if i == len(ordered): break ordered[i].collected = Now() ordered[i].save() if qt > len(ordered): product = Product.objects.get(id=prod.id) product.quantity += qt product.save() form = StockForm() else: form = StockForm() context={ "form": form } return render(request, "ord_prod/stock_update.html", context)
def create_table(request): """ Example JSON Request: { "dining_hall": "F", "meal_period": "BR", "creator_id": 3 } """ try: payload = json.loads(request.body) dining_hall = payload["dining_hall"] datetime = Now() #change later meal = payload["meal_period"] creator = payload["creator_id"] except Exception as e: return JsonResponse({ 'error': 'Invalid JSON request - required fields: dining_hall, datetime, meal, creator' }) new_room = DiningTable.objects.create(dining_hall=dining_hall, datetime=datetime, users=[creator], unread_msg_count={creator: None}, meal_period=meal, creator_id=creator) response = "Created new table at " + new_room.get_dining_hall_display( ) + " with ID " + str(new_room.id) log.debug("[TABLES] " + response) response_data = {"Success": response} return JsonResponse(response_data)
def peer_cmd(): logger.info("Start") addresses = get_nodes_list() # explore every peer and collect updates updates = {} if settings.TEST_NET: for address in addresses: explore_node(address, updates) else: with ThreadPoolExecutor(max_workers=20) as executor: executor.map(lambda address: explore_node(address, updates), addresses) updates_with_data = tuple(filter(lambda x: x is not None, updates.values())) # if more than __% peers were gone offline in __min, probably network problem if len(updates_with_data) < get_count_nodes_online() * 0.9: logger.warning("Peers update was rejected: %d - %d", len(updates_with_data), len(addresses)) return # set all peers unreachable, if will no update - peer will be unreachable PeerMonitor.objects.update(state=PeerMonitor.State.UNREACHABLE) # calculate state and apply updates for update in updates_with_data: logger.debug("Update: %r", update) peer_obj = PeerMonitor.objects.filter( announced_address=update["announced_address"]).first() if not peer_obj: logger.info("Found new peer: %s", update["announced_address"]) update["state"] = get_state(update, peer_obj) form = PeerMonitorForm(update, instance=peer_obj) if form.is_valid(): form.save() else: logger.info("Not valid data: %r - %r", form.errors, update) PeerMonitor.objects.update(lifetime=F("lifetime") + 1) PeerMonitor.objects.filter( state__in=[PeerMonitor.State.UNREACHABLE, PeerMonitor.State.STUCK ]).update(downtime=F("downtime") + 1) PeerMonitor.objects.annotate(duration=ExpressionWrapper( Now() - F("last_online_at"), output_field=DurationField())).filter( duration__gte=timedelta(days=30)).delete() PeerMonitor.objects.update( availability=100 - (F("downtime") / F("lifetime") * 100), modified_at=timezone.now(), ) logger.info("Done")
def with_timedelta(self): """ Adds a DurationField with distance between now and the start time (future or past). Use with with_next_date() ! Returns: QuerySet """ return self.annotate(timedelta=models.Case( models.When(next_date__gte=Now(), then=models.F("next_date") - Now()), models.When(next_date__lt=Now(), then=Now() - models.F("next_date")), output_field=models.DurationField(), ))
def checker(request, file_id): current_user = request.user file_to_check = get_object_or_404(ClassifiedFile, pk=file_id) read_file = open(file_to_check.uploadfile.path, 'r') read_file_data = read_file.read() total_words = read_file_data.split() sensitivity = 0 array1 = [] for word in total_words: lower_word = word.lower() clean_lower_word = lower_word.strip("!@#$%^&*()_+-=<>,.?/~`") array1.append(clean_lower_word) for each in array1: if each == "secret": sensitivity += 10 elif each == "dathena": sensitivity += 7 elif each == "internal": sensitivity += 5 elif each == "external": sensitivity += 3 elif each == "public": sensitivity += 1 read_file.close() file_to_update = ClassifiedFile.objects.filter(pk=file_id) file_to_update.update(sensitivity=sensitivity) file_to_update.update(updated=Now()) return redirect("view_files", username=current_user)
def mark_run(self): """ Update the last_run to Now, and save. """ self.last_run = Now() self.save() self.refresh_from_db()
def test_upload_valid_negometrix_excel_file_2_valid_rows_1_invalid_row( self): interfaceCall = InterfaceCall.objects.create( date_time_creation=Now(), status='TestStatus', filename= 'test_upload_valid_negometrix_excel_file_2_valid_rows_1_invalid_row.xlsx' ) file = "rm/test/resources/test_upload_valid_negometrix_excel_file_2_valid_rows_1_invalid_row.xlsx" excelInterfaceFile = check_file_and_interface_type(file, interfaceCall) self.assertTrue(isinstance(excelInterfaceFile, NegometrixInterfaceFile)) excelInterfaceFile.process() contracten = interfaceCall.contracten.all() self.assertEqual(len(contracten), 2) contract1 = contracten[0] self.assertEqual(contract1.contract_nr, '44335') contract2 = contracten[1] self.assertEqual(contract2.contract_nr, '44337') received_data = interfaceCall.received_data.all() self.assertEqual(len(received_data), 4) self.assertEqual(received_data[0].status, OK) self.assertEqual(received_data[1].status, OK) self.assertEqual(received_data[2].status, ERROR) self.assertEqual(received_data[3].status, OK)
def home(request): orders = Order.objects.all() customers = Customer.objects.all() non_closed_orders = orders.filter(Q(status='Abierta') | Q(status='En revisión')) duration = ExpressionWrapper(Now() - F('date_created'), output_field=fields.DurationField()) non_closed_orders = non_closed_orders.annotate(duration=duration) non_closed_orders = non_closed_orders.annotate(duration_days=Extract('duration', 'day')) non_closed_orders = non_closed_orders.order_by("date_created") if request.user.group == "taller": taller_tag = TagOrder.objects.get(name="taller") non_closed_orders = non_closed_orders.filter(order_tags=taller_tag) taller_orders = orders.filter(order_tags=taller_tag) opened = taller_orders.filter(status='Abierta').count() on_revision = taller_orders.filter(status='En revisión').count() closed = taller_orders.filter(status='Cerrada').count() else: opened = orders.filter(status='Abierta').count() on_revision = orders.filter(status='En revisión').count() closed = orders.filter(status='Cerrada').count() context = {'orders': non_closed_orders, 'customers': customers, 'opened': opened, 'on_revision': on_revision, 'closed': closed, 'to_date': datetime.now()} return render(request, 'accounts/dashboard.html', context)
def do_periodic_moss_check(): classes = Class.objects.current_semester() min_time_from_deadline = datetime.timedelta(minutes=30) max_time_from_deadline = datetime.timedelta(days=7) assignments = AssignedTask.objects.filter( # active tasks clazz__in=classes, # that have a deadline deadline__isnull=False, ).annotate( # calculate date diff date_diff=ExpressionWrapper( Now() - F("deadline"), output_field=DurationField())).filter( # filter tasks with dates that are not too old or too new date_diff__gte=min_time_from_deadline, date_diff__lte=max_time_from_deadline) tasks = Task.objects.filter(assignedtask__in=assignments) cache = caches["default"] for task in tasks: if (cache.get(moss_result_cache_key(task.id)) is not None or cache.get(moss_job_cache_key(task.id)) is not None): continue logging.info(f"Scheduling MOSS check for {task.id}") enqueue_moss_check(task.id, notify=True)
def get_queryset(self): queryset = super().get_queryset() provider_id = getattr(self.request.user, "provider_id", None) if provider_id: # Filter for general-purpose policies, # or the ones written for this provider queryset = queryset.filter( Q(providers__isnull=True) | Q(providers=provider_id) ) # The raw results will expose what other providers this policy applies to # Is this information leakage? else: # Only general-purpose policies to other users queryset = queryset.filter(providers__isnull=True) # Filter by date range start_time = self.request.GET.get("start_time") if start_time: start_time = utils.from_mds_timestamp(int(start_time)) else: start_time = Now() range_q = Q(end_date__gt=start_time) | Q(end_date__isnull=True) end_time = self.request.GET.get("end_time") if end_time: end_time = utils.from_mds_timestamp(int(end_time)) range_q &= Q(start_date__lte=end_time) queryset = queryset.filter(range_q) return queryset
def find_matching_timeslot(start_time: datetime, duration: timedelta, subject: Subject, requester, qs: QuerySet['TimeSlot']) -> Optional['TimeSlot']: qs = qs.prefetch_related("appointment_set").select_related('owner').filter( start_time__lte=start_time, start_time__time__lte=start_time.timetz(), start_time__iso_week_day=start_time.isoweekday(), duration__gte=duration, owner__tutordata__subjects=subject, owner__tutordata__verified=True) if settings.NAKLAR_SCHEDULING_SCHOOLDATA: qs = qs.filter( owner__tutordata__schooldata=requester.studentdata.school_data) qs = qs.annotate(num_appointments=Count( 'owner__timeslot__appointment', filter=Q(owner__timeslot__appointment__start_time__gte=Now())) ).order_by('num_appointments') for timeslot in qs: for available in timeslot.available_slots( earliest_start=timezone.now() + timedelta( minutes=settings.NAKLAR_SCHEDULING_APPOINTMENT_DISTANCE)): if (start_time >= available.start_time and available.start_time + available.duration >= start_time + duration): return available.parent return None
def add(request): if 'c_user_name' not in request.session: return redirect('/users/login.php?redirect=/vaxign2/login') elif request.session['c_user_name'] not in TUser.objects.all().values_list( 'c_user_name', flat=True): return HttpResponse(status=403) context = {} form = ProjectsForm(request.POST or None) if request.method == 'POST': if form.is_valid(): formData = form.cleaned_data project = TVaxignProject( c_vaxign_projectname=formData['project_name'], c_description=formData['description'], c_note=formData['note'], c_lastupdate=Now(), c_isopen=formData['is_public'], c_authorized=1, c_userfk=request.session['c_user_name'], c_institution=formData['institution'], c_investigator=formData['investigator'], c_list_title=formData['list_project'], c_creation_time=Now(), c_is_locked=0, c_show_settings=0, c_grant=formData['grant'], ) project.save() permission = TUserVaxignPrivilege( c_userfk=request.session['c_user_name'], c_projectfk=project.c_vaxign_projectid, c_permission='update', ) permission.save() return redirect('/vaxign2/project/' + str(project.c_vaxign_projectid)) context['form'] = form return render(request, 'projects/add.html', context)
def feed(request): fg = FeedGenerator() fg.id("bearblog") fg.author({"name": "Bear Blog", "email": "*****@*****.**"}) newest = request.GET.get("newest") if newest: fg.title("Bear Blog Most Recent Posts") fg.subtitle("Most recent posts on Bear Blog") fg.link(href="https://bearblog.dev/discover/?newest=True", rel="alternate") all_posts = (Post.objects.annotate( upvote_count=Count("upvote"), ).filter( publish=True, blog__reviewed=True, blog__blocked=False, show_in_feed=True, published_date__lte=timezone.now(), ).order_by("-published_date").select_related("blog") [0:posts_per_page]) else: fg.title("Bear Blog Trending Posts") fg.subtitle("Trending posts on Bear Blog") fg.link(href="https://bearblog.dev/discover/", rel="alternate") all_posts = (Post.objects.annotate( upvote_count=Count("upvote"), score=ExpressionWrapper( ((Count("upvote") - 1) / ((Seconds(Now() - F("published_date"))) + 4)**gravity) * 100000, output_field=FloatField(), ), ).filter( publish=True, blog__reviewed=True, blog__blocked=False, show_in_feed=True, published_date__lte=timezone.now(), ).order_by("-score", "-published_date").select_related( "blog").prefetch_related("upvote_set")[0:posts_per_page]) for post in all_posts: fe = fg.add_entry() fe.id(f"{post.blog.useful_domain()}/{post.slug}/") fe.title(post.title) fe.author({"name": post.blog.subdomain, "email": "hidden"}) fe.link(href=f"{post.blog.useful_domain()}/{post.slug}/") fe.content(clean_text(mistune.html(post.content)), type="html") fe.published(post.published_date) fe.updated(post.published_date) if request.GET.get("type") == "rss": fg.link(href=f"{post.blog.useful_domain()}/feed/?type=rss", rel="self") rssfeed = fg.rss_str(pretty=True) return HttpResponse(rssfeed, content_type="application/rss+xml") else: fg.link(href=f"{post.blog.useful_domain()}/feed/", rel="self") atomfeed = fg.atom_str(pretty=True) return HttpResponse(atomfeed, content_type="application/atom+xml")
def _handle_max_created_at_diff(self, queryset, queue_name, max_created_at_diff): if max_created_at_diff < 0: raise CommandError(self.messages['invalid_max_created_at_diff']) if queryset.annotate(diff=ExpressionWrapper(Now() - F('created_at'), output_field=DurationField())).filter( diff__gt=timedelta(seconds=max_created_at_diff), ).exists(): raise CommandError(self.messages['max_created_at_diff_error'].format(max_created_at_diff, queue_name))
def get(self, request, *args, **kwargs): myDate = datetime.now() queryset = self.model.objects.annotate( delta1=ExpressionWrapper(F('end') - Now(), output_field=DurationField())) return render(request, self.template_name, { 'myDate': myDate, 'queryset': queryset, })
def cancel(self, user=None): if user and not user.is_authenticated: user = None return self.update( status=self.model.CANCELED, completed_by_user=user, completed=Now(), )
def list(self, request, *args, **kwargs): ''' Handle GET for all article elements Returns: Response == JSON serialized Article list ''' articles = Article.objects.all() sort = request.query_params.get('sort', None) relevant = request.query_params.get('relevant', None) if sort == 'true': articles = articles.annotate(Count( 'upvotes', distinct=True)).order_by('-upvotes').distinct() if relevant == 'true': #### https://stackoverflow.com/questions/38707848/query-annotation-with-date-difference articles = Article.objects.annotate(relevant=Count('upvotes')) \ .annotate(diff=(JulianDay(Now())-JulianDay(F('pub_date')))) \ .annotate(relevance=ExpressionWrapper(((Count('upvotes'))/((F('diff')+2)**1.8)), output_field=FloatField())).order_by('-relevance').distinct() coronavirus = request.query_params.get('coronavirus', None) if coronavirus == 'true': articles = articles.filter(title__icontains='corona').distinct() | Article.objects.filter(description__icontains='corona').distinct() | \ Article.objects.filter(title__icontains='covid').distinct() | Article.objects.filter(description__icontains='covid').distinct() search = request.query_params.get('search', None) if search: articles = articles.filter(title__icontains=search) | articles.filter(description__icontains=search) | \ articles.filter(title__icontains=search) | articles.filter(description__icontains=search) feed = request.query_params.get('feed', None) if feed: articles = articles.filter(feed_id=feed) custom = request.query_params.get('custom', None) if custom: articles = articles.filter( feed_id__subscriptions__user=request.auth.user) saved = request.query_params.get('saved', None) if saved: articles = articles.filter(user_saves__user=request.auth.user) favorites = request.query_params.get('favorites', None) if favorites: articles = articles.filter(upvotes__user=request.auth.user) usersummaries = request.query_params.get('usersummaries', None) if usersummaries: articles = articles.filter(summary__user=request.auth.user) page = self.paginate_queryset(articles) serializer = ArticleSerializer(page, many=True, context={'request': request}) # https://stackoverflow.com/questions/31785966/django-rest-framework-turn-on-pagination-on-a-viewset-like-modelviewset-pagina return self.get_paginated_response(serializer.data)