def __iter__(self): if self.start_id > self.end_id: # This can happen if we are the last shard and # the shard size caused each previous shard to process an additional model return query = self.model.objects.using(self.db) if self.start_id: query = query.filter(pk__gt=self.start_id).filter(pk__lte=self.end_id) query = query.order_by('pk') for model in utils.get_in_batches(query, batch_size=500): # From the mapreduce docs (AbstractDatastoreInputReader): # The caller must consume yielded values so advancing the KeyRange # before yielding is safe. self.start_id = model.id yield model
def get_users(self, email): """Given an email, return matching user(s) who should receive a reset. This allows subclasses to more easily customize the default policies that prevent inactive users and users with unusable passwords from resetting their password. """ active_users = list(get_user_model()._default_manager.filter( email__iexact=email, is_active=True)) # Mild data migration # To use __iexact we have to resave models. Djangae do the rest # https://djangae.readthedocs.io/en/latest/db_backend/#special-indexes if len(active_users) == 0: for u in get_in_batches( get_user_model()._default_manager.all(), 30): u.save() active_users = list(get_user_model()._default_manager.filter( email__iexact=email, is_active=True)) return (u for u in active_users if u.has_usable_password())
def __iter__(self): pk_filters = {} if self.pk__gt is not None: pk_filters['pk__gt'] = self.pk__gt if self.pk__lte is not None: pk_filters['pk__lte'] = self.pk__lte if self.pk_last_read is not None: pk_filters['pk__gt'] = max(self.pk_last_read, pk_filters.get('pk__gt', self.pk_last_read)) qs = self.model.objects.all() if self.query is not None: qs.query = self.query qs = qs.using(self.db).filter(**pk_filters).order_by('pk') for model in get_in_batches(qs, batch_size=self._BATCH_SIZE): # From the mapreduce docs (AbstractDatastoreInputReader): # The caller must consume yielded values so advancing the KeyRange # before yielding is safe. self.pk_last_read = model.id yield model
def __iter__(self): pk_filters = {} if self.pk__gt is not None: pk_filters['pk__gt'] = self.pk__gt if self.pk__lte is not None: pk_filters['pk__lte'] = self.pk__lte if self.pk_last_read is not None: pk_filters['pk__gt'] = max( self.pk_last_read, pk_filters.get('pk__gt', self.pk_last_read)) qs = self.model.objects.all() if self.query is not None: qs.query = self.query qs = qs.using(self.db).filter(**pk_filters).order_by('pk') for model in get_in_batches(qs, batch_size=self._BATCH_SIZE): # From the mapreduce docs (AbstractDatastoreInputReader): # The caller must consume yielded values so advancing the KeyRange # before yielding is safe. self.pk_last_read = model.id yield model
def dashboard(request): # You can use it for preseeding database with users # import random # for i in range(100): # p = m.Person.objects.create( # name="test_{}".format(i), # last_name="test_{}".format(i), # amount_paid=0, # age=random.randint(8, 88)) # p.created_at = datetime.datetime(2016, random.randint(1, 12), random.randint(1, 20)) # p.save() # for j in range(random.randint(3, 20)): # r = m.Receipt.objects.create( # user=request.user, # person=p, # amount=random.randint(20, 600)) # r.created_at=datetime.datetime(2016, random.randint(1, 12), random.randint(1, 20)) # r.save() persons = list(get_in_batches(request.user.person_set.all(), CHUNK_SIZE)) # Pseudomigration for patient in persons: if patient.created_at is None: patient.created_at = datetime.datetime.utcnow() patient.save() data = {} data["count"] = len(persons) data["ages"] = charts_data.ages([p.age for p in persons]) p_ids = list(person.pk for person in persons) dental_charts_records = [] for i in range(0, len(persons), CHUNK_SIZE): dental_charts_records.extend( m.DentalChart.objects.filter( person__in=p_ids[i:i + CHUNK_SIZE]).values_list( "extraction", "filling", "rct")) data["dental_charts"] = charts_data.dental_charts(dental_charts_records) year_ago, end_of_month = charts_data.year_range() patients_records = [patient.created_at for patient in persons] patients_records.sort() data["patients"] = charts_data.patients(patients_records, year_ago, end_of_month) today = datetime.date.today() next_sat = (today + datetime.timedelta(days=(5 - today.weekday()) % 7)) next_sat2 = next_sat + datetime.timedelta(weeks=1) next_sat3 = next_sat + datetime.timedelta(weeks=2) next_appointments = [ person.date for person in persons if next_sat <= person.date < next_sat3 ] next_appointments.sort() data["appointment_next_week"] = len( [d for d in next_appointments if next_sat <= d < next_sat2]) data["appointment_next_week2"] = len( [d for d in next_appointments if next_sat2 <= d < next_sat3]) receipts_records = request.user.receipt_set.values("amount", "created_at") receipts_records = list(get_in_batches(receipts_records, CHUNK_SIZE)) receipts_records.sort() data["revenue"] = charts_data.revenue(receipts_records, year_ago, end_of_month) return render(request, 'core/dashboard.html', data)