Exemple #1
0
    def date_time_between_dates(self, datetime_start=None, datetime_end=None, tzinfo=None):
        """
        Takes two DateTime objects and returns a random datetime between the two
        given datetimes.
        Accepts DateTime objects.

        :param datetime_start: DateTime
        :param datetime_end: DateTime
        :param tzinfo: timezone, instance of datetime.tzinfo subclass
        :example DateTime('1999-02-02 11:42:52')
        :return DateTime
        """
        if datetime_start is None:
            datetime_start = datetime.now(tzinfo)

        if datetime_end is None:
            datetime_end = datetime.now(tzinfo)

        timestamp = self.generator.random.randint(
            datetime_to_timestamp(datetime_start),
            datetime_to_timestamp(datetime_end),
        )
        if tzinfo is None:
            pick = datetime.fromtimestamp(timestamp, tzlocal())
            pick = pick.astimezone(tzutc()).replace(tzinfo=None)
        else:
            pick = datetime.fromtimestamp(timestamp, tzinfo)
        return pick
Exemple #2
0
    def date_time_between_dates(self, datetime_start=None, datetime_end=None, tzinfo=None):
        """
        Takes two DateTime objects and returns a random datetime between the two
        given datetimes.
        Accepts DateTime objects.

        :param datetime_start: DateTime
        :param datetime_end: DateTime
        :param tzinfo: timezone, instance of datetime.tzinfo subclass
        :example DateTime('1999-02-02 11:42:52')
        :return DateTime
        """
        if datetime_start is None:
            datetime_start = datetime.now(tzinfo)

        if datetime_end is None:
            datetime_end = datetime.now(tzinfo)

        timestamp = self.generator.random.randint(
            datetime_to_timestamp(datetime_start),
            datetime_to_timestamp(datetime_end),
        )
        if tzinfo is None:
            pick = datetime.fromtimestamp(timestamp, tzlocal())
            pick = pick.astimezone(tzutc()).replace(tzinfo=None)
        else:
            pick = datetime.fromtimestamp(timestamp, tzinfo)
        return pick
Exemple #3
0
    def _parse_date_time(cls, text, tzinfo=None):
        if isinstance(text, (datetime, date, real_datetime, real_date)):
            return datetime_to_timestamp(text)
        now = datetime.now(tzinfo)
        if isinstance(text, timedelta):
            return datetime_to_timestamp(now - text)
        if is_string(text):
            if text == 'now':
                return datetime_to_timestamp(datetime.now(tzinfo))
            parts = cls.regex.match(text)
            if not parts:
                return
            parts = parts.groupdict()
            time_params = {}
            for (name, param) in parts.items():
                if param:
                    time_params[name] = int(param)

            if 'years' in time_params:
                if 'days' not in time_params:
                    time_params['days'] = 0
                time_params['days'] += 365.24 * time_params.pop('years')

            return datetime_to_timestamp(now + timedelta(**time_params))
        if isinstance(text, int):
            return datetime_to_timestamp(now + timedelta(text))
        raise ValueError("Invalid format for date '{0}'".format(text))
Exemple #4
0
 def _parse_date_time(cls, text, tzinfo=None):
     if isinstance(text, (datetime, date, real_datetime, real_date)):
         return datetime_to_timestamp(text)
     now = datetime.now(tzinfo)
     if isinstance(text, timedelta):
         return datetime_to_timestamp(now - text)
     if is_string(text):
         if text == 'now':
             return datetime_to_timestamp(datetime.now(tzinfo))
         time_params = cls._parse_date_string(text)
         return datetime_to_timestamp(now + timedelta(**time_params))
     if isinstance(text, int):
         return datetime_to_timestamp(now + timedelta(text))
     raise ParseError("Invalid format for date '{0}'".format(text))
Exemple #5
0
 def _parse_date_time(cls, text, tzinfo=None):
     if isinstance(text, (datetime, date, real_datetime, real_date)):
         return datetime_to_timestamp(text)
     now = datetime.now(tzinfo)
     if isinstance(text, timedelta):
         return datetime_to_timestamp(now - text)
     if is_string(text):
         if text == 'now':
             return datetime_to_timestamp(datetime.now(tzinfo))
         time_params = cls._parse_date_string(text)
         return datetime_to_timestamp(now + timedelta(**time_params))
     if isinstance(text, int):
         return datetime_to_timestamp(now + timedelta(text))
     raise ParseError("Invalid format for date '{0}'".format(text))
Exemple #6
0
 def test_individuals_ogrn(self, faker, num_samples):
     max_year = datetime.now().year - 2000
     for _ in range(num_samples):
         ogrn = faker.individuals_ogrn()
         assert len(ogrn) == 15
         assert ogrn[0] == '3'
         assert 1 <= int(ogrn[1:3]) <= max_year
         assert 1 <= int(ogrn[3:5]) <= 92
         assert int(ogrn[:-1]) % 13 % 10 == int(ogrn[-1])
Exemple #7
0
 def test_businesses_ogrn(self, faker, num_samples):
     max_year = datetime.now().year - 2000
     for _ in range(num_samples):
         ogrn = faker.businesses_ogrn()
         assert len(ogrn) == 13
         assert ogrn[0] in ('1', '5')
         assert 1 <= int(ogrn[1:3]) <= max_year
         assert 1 <= int(ogrn[3:5]) <= 92
         assert int(ogrn[:-1]) % 11 % 10 == int(ogrn[-1])
Exemple #8
0
    def date_time_between_dates(cls, datetime_start=None, datetime_end=None):
        """
        Takes two DateTime objects and returns a random date between the two given dates.
        Accepts DateTime objects.

        :param datetime_start DateTime
        :param datetime_end DateTime
        :example DateTime('1999-02-02 11:42:52')
        :return DateTime
        """
        if datetime_start is None:
            datetime_start = datetime.now()

        if datetime_end is None:
            datetime_end = datetime.now()

        timestamp = random.randint(datetime_to_timestamp(datetime_start), datetime_to_timestamp(datetime_end))
        return datetime.fromtimestamp(timestamp)
Exemple #9
0
    def test_businesses_ogrn(self):
        max_year = datetime.now().year - 2000

        for i in range(self.num_sample_runs):
            ogrn = self.factory.businesses_ogrn()

            assert len(ogrn) == 13
            assert ogrn[0] in ('1', '5')
            assert 1 <= int(ogrn[1:3]) <= max_year
            assert 1 <= int(ogrn[3:5]) <= 92
            assert int(ogrn[:-1]) % 11 % 10 == int(ogrn[-1])
Exemple #10
0
    def test_individuals_ogrn(self):
        max_year = datetime.now().year - 2000

        for i in range(self.num_sample_runs):
            ogrn = self.factory.individuals_ogrn()

            assert len(ogrn) == 15
            assert ogrn[0] == '3'
            assert 1 <= int(ogrn[1:3]) <= max_year
            assert 1 <= int(ogrn[3:5]) <= 92
            assert int(ogrn[:-1]) % 13 % 10 == int(ogrn[-1])
Exemple #11
0
    def individuals_ogrn(self):
        """
        Returns primary state registration number for individuals
        (ru. основной государственный регистрационный номер, ОГРН).
        """
        year = '%02d' % self.random_int(min=1, max=datetime.now().year - 2000)
        region = '%02d' % self.random_int(min=1, max=92)
        tail = '%09d' % self.random_int(min=1, max=999999999)

        result = '3' + year + region + tail

        return result + str((int(result) % 13) % 10)
Exemple #12
0
    def businesses_ogrn(self):
        """
        Returns primary state registration number for businesses
        (ru. основной государственный регистрационный номер, ОГРН).
        """
        sign = self.random_element(('1', '5'))
        year = '%02d' % self.random_int(min=1, max=datetime.now().year - 2000)
        region = '%02d' % self.random_int(min=1, max=92)
        tail = '%07d' % self.random_int(min=1, max=9999999)

        result = sign + year + region + tail

        return result + str((int(result) % 11) % 10)
Exemple #13
0
    def date_time_between_dates(cls, datetime_start=None, datetime_end=None, tzinfo=None):
        """
        Takes two DateTime objects and returns a random date between the two given dates.
        Accepts DateTime objects.

        :param datetime_start DateTime
        :param datetime_end DateTime
        :param tzinfo: timezone, instance of datetime.tzinfo subclass
        :example DateTime('1999-02-02 11:42:52')
        :return DateTime
        """
        if datetime_start is None:
            datetime_start = datetime.now(tzinfo)

        if datetime_end is None:
            datetime_end = datetime.now(tzinfo)

        timestamp = random.randint(
            datetime_to_timestamp(datetime_start),
            datetime_to_timestamp(datetime_end),
        )
        return datetime.fromtimestamp(timestamp, tzinfo)
Exemple #14
0
    def date_time_this_month(cls, before_now=True, after_now=False):
        """
        Gets a DateTime object for the current month. 
        
        :param before_now: include days in current month before today
        :param after_now: include days in current month after today
        :example DateTime('2012-04-04 11:02:02')
        :return DateTime
        """
        now = datetime.now()
        this_month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
        next_month_start = datetime(now.year, now.month + 1 % 12, 1)

        if before_now and after_now:
            return cls.date_time_between_dates(this_month_start, next_month_start)
        elif not before_now and after_now:
            return cls.date_time_between_dates(now, next_month_start)
        elif not after_now and before_now:
            return cls.date_time_between_dates(this_month_start, now)
        else:
            return now
Exemple #15
0
    def date_time_this_decade(cls, before_now=True, after_now=False):
        """
        Gets a DateTime object for the decade year. 
        
        :param before_now: include days in current decade before today
        :param after_now: include days in current decade after today
        :example DateTime('2012-04-04 11:02:02')
        :return DateTime
        """
        now = datetime.now()
        this_decade_start = datetime(now.year - (now.year % 10), 1, 1)
        next_decade_start = datetime(this_decade_start.year + 10, 1, 1)

        if before_now and after_now:
            return cls.date_time_between_dates(this_decade_start, next_decade_start)
        elif not before_now and after_now:
            return cls.date_time_between_dates(now, next_decade_start)
        elif not after_now and before_now:
            return cls.date_time_between_dates(this_decade_start, now)
        else:
            return now
Exemple #16
0
    def date_time_this_month(cls, before_now=True, after_now=False, tzinfo=None):
        """
        Gets a DateTime object for the current month.

        :param before_now: include days in current month before today
        :param after_now: include days in current month after today
        :param tzinfo: timezone, instance of datetime.tzinfo subclass
        :example DateTime('2012-04-04 11:02:02')
        :return DateTime
        """
        now = datetime.now(tzinfo)
        this_month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)

        next_month_start = this_month_start + relativedelta.relativedelta(months=1)
        if before_now and after_now:
            return cls.date_time_between_dates(this_month_start, next_month_start, tzinfo)
        elif not before_now and after_now:
            return cls.date_time_between_dates(now, next_month_start, tzinfo)
        elif not after_now and before_now:
            return cls.date_time_between_dates(this_month_start, now, tzinfo)
        else:
            return now
Exemple #17
0
    def date_time_this_century(cls, before_now=True, after_now=False, tzinfo=None):
        """
        Gets a DateTime object for the current century.

        :param before_now: include days in current century before today
        :param after_now: include days in current century after today
        :param tzinfo: timezone, instance of datetime.tzinfo subclass
        :example DateTime('2012-04-04 11:02:02')
        :return DateTime
        """
        now = datetime.now(tzinfo)
        this_century_start = datetime(now.year - (now.year % 100), 1, 1, tzinfo=tzinfo)
        next_century_start = datetime(this_century_start.year + 100, 1, 1, tzinfo=tzinfo)

        if before_now and after_now:
            return cls.date_time_between_dates(this_century_start, next_century_start, tzinfo)
        elif not before_now and after_now:
            return cls.date_time_between_dates(now, next_century_start, tzinfo)
        elif not after_now and before_now:
            return cls.date_time_between_dates(this_century_start, now, tzinfo)
        else:
            return now
Exemple #18
0
    def date_time_this_decade(cls, before_now=True, after_now=False, tzinfo=None):
        """
        Gets a DateTime object for the decade year.

        :param before_now: include days in current decade before today
        :param after_now: include days in current decade after today
        :param tzinfo: timezone, instance of datetime.tzinfo subclass
        :example DateTime('2012-04-04 11:02:02')
        :return DateTime
        """
        now = datetime.now(tzinfo)
        this_decade_start = datetime(now.year - (now.year % 10), 1, 1, tzinfo=tzinfo)
        next_decade_start = datetime(this_decade_start.year + 10, 1, 1, tzinfo=tzinfo)

        if before_now and after_now:
            return cls.date_time_between_dates(this_decade_start, next_decade_start, tzinfo)
        elif not before_now and after_now:
            return cls.date_time_between_dates(now, next_decade_start, tzinfo)
        elif not after_now and before_now:
            return cls.date_time_between_dates(this_decade_start, now, tzinfo)
        else:
            return now
Exemple #19
0
    def date_time_this_year(self, before_now=True, after_now=False, tzinfo=None):
        """
        Gets a DateTime object for the current year.

        :param before_now: include days in current year before today
        :param after_now: include days in current year after today
        :param tzinfo: timezone, instance of datetime.tzinfo subclass
        :example DateTime('2012-04-04 11:02:02')
        :return DateTime
        """
        now = datetime.now(tzinfo)
        this_year_start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
        next_year_start = datetime(now.year + 1, 1, 1, tzinfo=tzinfo)

        if before_now and after_now:
            return self.date_time_between_dates(this_year_start, next_year_start, tzinfo)
        elif not before_now and after_now:
            return self.date_time_between_dates(now, next_year_start, tzinfo)
        elif not after_now and before_now:
            return self.date_time_between_dates(this_year_start, now, tzinfo)
        else:
            return now
Exemple #20
0
    def date_time_this_year(self, before_now=True, after_now=False, tzinfo=None):
        """
        Gets a DateTime object for the current year.

        :param before_now: include days in current year before today
        :param after_now: include days in current year after today
        :param tzinfo: timezone, instance of datetime.tzinfo subclass
        :example DateTime('2012-04-04 11:02:02')
        :return DateTime
        """
        now = datetime.now(tzinfo)
        this_year_start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
        next_year_start = datetime(now.year + 1, 1, 1, tzinfo=tzinfo)

        if before_now and after_now:
            return self.date_time_between_dates(this_year_start, next_year_start, tzinfo)
        elif not before_now and after_now:
            return self.date_time_between_dates(now, next_year_start, tzinfo)
        elif not after_now and before_now:
            return self.date_time_between_dates(this_year_start, now, tzinfo)
        else:
            return now
Exemple #21
0
    def state_change(self,
                     next_app_state_code=None,
                     triggered_by=None,
                     bap_yetkilisi_notu=None,
                     description='',
                     email_gonderilsin_mi=None,
                     yurutucu_log=None):

        try:
            next_state_info = self.state_change_checker(
                next_app_state_code=next_app_state_code)

        except Exception as e:
            raise Exception(e)

        if next_state_info.app_state_id:
            try:
                app_state_tracker = AppStateTracker(
                    state_id=next_state_info.app_state_id,
                    params={
                        'proje_id': self.entity.proje_id,
                        'satinalma_id': self.entity.id
                    },
                    date=datetime.now(),
                    job_type=JobTypes.satinalma_state_change,
                    triggered_by=triggered_by,
                    description="{} {}".format(
                        next_state_info.app_state_description, description))

                entity_model = DB.session.query(self.entity_type).filter(
                    self.entity_type.id == self.entity.id).first()
                entity_model.durum_id = next_state_info.app_state_id
                DB.session.add(app_state_tracker)

                payload = {
                    "message_type":
                    USER_ACTIVITY_MESSAGES.get("bap").get(
                        "satinalma_durum_degisimi").type_index,
                    "ekstra_mesaj":
                    "{} id li {} durumu {} yapildi.".format(
                        self.entity.__repr__(), self.entity.id,
                        next_state_info.app_state_description),
                    "nesne":
                    '{}'.format(self.entity.__repr__()),
                    "nesne_id":
                    self.entity.id,
                }

                signal_sender(notification=True, **payload)

                proje = DB.session.query(Proje).filter(
                    Proje.id == self.entity.proje_id).first()
                yurutucu_person_id = proje.proje_yurutucu.person.id

                satinalma = DB.session.query(
                    ProjeSatinAlmaTalebi.talep_numarasi.label(
                        "talep_numarasi")).options(lazyload("*")).filter(
                            ProjeSatinAlmaTalebi.id == self.entity.id).first()

                if yurutucu_log:
                    payload = {
                        "message_type":
                        USER_ACTIVITY_MESSAGES.get("bap").get(
                            "satinalma_durum_degisimi").type_index,
                        "ekstra_mesaj":
                        "{} id'li projenin  id'li {} satinalma talebi {} yapildi."
                        .format(proje.id, self.entity.id,
                                next_state_info.app_state_description),
                        "notification_receiver":
                        yurutucu_person_id,
                        "notification_title":
                        next_state_info.app_state_description,
                        "notification_message":
                        "{} numaralı projenin {} numaralı satınalma "
                        "talebinde yapılan durum değişimi için bap yetkili "
                        "notu; {}".format(proje.proje_no,
                                          satinalma.talep_numarasi,
                                          yurutucu_log),
                        "proje_id":
                        proje.id,
                        "related_table":
                        "proje_mesaj"
                    }

                    signal_sender(log=False, notification=True, **payload)

                    if email_gonderilsin_mi:
                        SatinalmaStateDispatcher.email_gonder(
                            proje=proje,
                            subject=next_state_info.app_state_description,
                            content_text=
                            "{} numaralı projenin {} numaralı satınalma "
                            "talebinde yapılan durum değişimi için bap yetkili "
                            "notu; {}".format(proje.proje_no,
                                              satinalma.talep_numarasi,
                                              yurutucu_log),
                            greeting_name="{} {}".format(
                                proje.proje_yurutucu.person.ad,
                                proje.proje_yurutucu.person.soyad))

                for bap_admin in bap_yetkili_and_admin_ids():
                    payload = {
                        "message_type":
                        USER_ACTIVITY_MESSAGES.get("bap").get(
                            "satinalma_durum_degisimi").type_index,
                        "ekstra_mesaj":
                        "{} id'li projenin  id'li {} satinalma talebi {} yapildi."
                        .format(proje.id, self.entity.id,
                                next_state_info.app_state_description),
                        "notification_receiver":
                        bap_admin.person_id,
                        "notification_title":
                        next_state_info.app_state_description,
                        "notification_message":
                        "{} numaralı projenin {} numaralı satınalma "
                        "talebi için durum değişimi yapıldı.{}".format(
                            proje.proje_no, satinalma.talep_numarasi,
                            bap_yetkilisi_notu
                            or next_state_info.app_state_description),
                    }

                    signal_sender(log=False, notification=True, **payload)

                DB.session.commit()
            except Exception as exc:
                raise Exception(exc)

        else:
            raise ValueError("Could not find next possible state's id.")

        return next_state_info.app_state_id
Exemple #22
0
 def date_time_this_month(cls):
     """
     :example DateTime('2012-04-04 11:02:02')
     """
     return cls.date_time_between('-%dd' % (datetime.now().day))
Exemple #23
0
 def date_time_this_decade(cls):
     """
     :example DateTime('2004-04-04 11:02:02')
     """
     return cls.date_time_between('-%dy' % (datetime.now().year % 10))
Exemple #24
0
 def date_time_this_century(cls):
     """
     :example DateTime('1964-04-04 11:02:02')
     """
     return cls.date_time_between('-%dy' % (datetime.now().year % 100))
Exemple #25
0
 def date_time_this_century(cls):
     """
     :example DateTime('1964-04-04 11:02:02')
     """
     return cls.date_time_between('-%dy' % (datetime.now().year % 100))
Exemple #26
0
 def date_time_this_decade(cls):
     """
     :example DateTime('2004-04-04 11:02:02')
     """
     return cls.date_time_between('-%dy' % (datetime.now().year % 10))
Exemple #27
0
 def date_time_this_month(cls):
     """
     :example DateTime('2012-04-04 11:02:02')
     """
     return cls.date_time_between('-%dd' % (datetime.now().day))