def dates_in_range(start_dt=None, end_dt=None, entry_count=14): """Generates a list of `entry_count` entries of dates between the `start` and `end` date values given. Any date objects given will be converted to datetime objects. Default start date is a week ago from today. Default end date is today. There is no guarantee there will be a date on the `end` value, but there is a guarantee there will be no dates after it.""" if not end_dt: end_dt = dt.now() if entry_count < 1: return [] today = dt.now() aweek = timedelta(days=14) # convert any date objects into datetimes start_dt, end_dt = map(dt.date_to_datetime, [start_dt, end_dt]) if not start_dt: start_dt = end_dt - aweek if end_dt else today - aweek if not end_dt: end_dt = today if start_dt > end_dt: start_dt, end_dt = end_dt, start_dt day_span = (end_dt - start_dt).days pc = day_span / float(entry_count) date_range = [start_dt + timedelta(n * pc) for n in range(1, entry_count + 1)] return date_range
def test_all_results_are_datetimes(self): "Test every object returned is a datetime object" a_week_ago = dt.now() - timedelta(days=7) today = dt.now() dt_list = logic.dates_in_range(a_week_ago, today, entry_count=14) for dto in dt_list: self.assertTrue(isinstance(dto, datetime))
def test_list_dates_within_range(self): "Test that the dates in the range are within the given bounds" a_week_ago = dt.now() - timedelta(days=7) today = dt.now() dt_list = logic.dates_in_range(a_week_ago, today, entry_count=14) for dto in dt_list: print today print dto print a_week_ago print self.assertTrue(dto >= a_week_ago) self.assertTrue(dto <= today)
def release_article(article): """releases a galley to the public. if this is the first galley for this article released, then now() will be the date published for the article.""" #art = submissionobj.article pubart = article.publishedarticle if pubart.galley_set.count() < 1: raise IllegalStateException( "Article cannot be released if it has no galleys.") pubart.datetime_released = dt.now() pubart.datetime_published = dt.now() pubart.save() article.submission.release() article.submission.save()
def assignment_decision(assignment, decision, **kwargs): assignment.state = decision assignment.date_decision_made = dt.now() for key, val in kwargs.items(): setattr(assignment, key, val) assignment.save() return assignment
def test_many_unique_dummy_data(self): "Test that a series of dummy data can all have unique dates if necessary" now = dt.now() dummy_list = logic.dummy_entry_data_list(7, now, now - timedelta(days=7)) date_set = set([dummy['last_updated'] for dummy in dummy_list]) self.assertEqual(len(date_set), 7)
def add_transaction_or_update(cls, coin_id, tx_hash, block_time, sender, receiver, amount, status, tx_type, block_id, height, gas=0, gas_price=0, fee=0, contract=None, is_send=0, *, commit=True, session=None): """添加交易或更新交易""" session = session or db.session() sql = ( "insert into {table_name} (coin_id, tx_hash, block_time, sender, receiver, amount, " "status, type, gas, gas_price, fee, contract, block_id, height, is_send, " "create_time, update_time) " "values " "({coin_id}, '{tx_hash}', {block_time}, '{sender}', '{receiver}', {amount}, {status}, " "{type}, {gas}, {gas_price}, {fee}, {contract}, {block_id}, {height}, {is_send}, " "'{create_time}', '{update_time}')" "ON DUPLICATE KEY UPDATE block_time=values(block_time), gas=values(gas), " "gas_price=values(gas_price), " "fee=values(fee), block_id=values(block_id), height=values(height)").format( table_name=cls.__tablename__, coin_id=coin_id, tx_hash=tx_hash, block_time=block_time, sender=sender, receiver=receiver, amount=amount, status=status, type=tx_type, gas=gas, gas_price=gas_price, fee=fee, contract="'{}'".format(contract) if contract is not None else 'null', block_id=block_id, height=height, is_send=is_send, create_time=now(), update_time=now()) # saved 如果成功情况下是 None saved = session.execute(sql) if commit: # 自动提交 session.commit() return saved # 返回 session 等待自行处理 return session
def dummy_entry_data(**kwargs): """ Returns dummy models.Entry data. This can then be used to pass to `create_entry` or with several dummy entries like: add_or_update_entries(feedobj, map(dummy_entry, dates_in_range(15, start_dt, end_dt)) """ content = nonsense.gen_content() guid = "dummy - %s - %s" % (dt.now().microsecond, nonsense.rand.randint(0, 99)) title = nonsense.gen_title() link = 'http://example.org/entry/%s/' % guid today = dt.now() dd = { 'guid': guid, 'title': title, 'link': link, 'author': 'John Doe', 'last_updated': today, 'content': content, } dd.update(**kwargs) dd['slug'] = slugify(dd['title']) return dd
def submit_review(assignment, decision, review): if assignment.state != models.REVIEW_ACCEPTED: raise ValueError("cannot leave review, assignment in state %s" % assignment.state) rd, created = models.ReviewDecision.objects.get_or_create( assignment=assignment) if rd.date_completed: raise ValueError("cannot submit review: review already has a decision") assignment.state = models.REVIEW_SUBMITTED assignment.save() rd.accepted = decision rd.review = review rd.date_completed = dt.now() rd.save() return rd
def parse_feed(url, data): "Pulls out the bits we're interested in from the raw data and returns those values mapped to models.Feed fields." fd = data.feed # by discarding None values we force the model's default value for that field to be used. return _discard_dict_nulls({ 'url': url, 'website': parse_homepage(fd.link), 'name': fd.title, 'slug': slugify(fd.title), 'description': fd.get('subtitle'), 'language': fd.get('language'), 'contact': fd.get('author'), 'ttl': fd.get('ttl'), 'image': None, 'last_updated': dt.mkdt(fd.get('updated_parsed')), #'last_updated': dt.mkdt2(fd.get('updated')), # when we switch to UTC 'last_fetched': dt.now() #datetime.now() })
def test_mixed_dates_datetimes(self): "Test that if a date object is passed in, then all resulting objects will be datetime objects." start_dt = date.today() end_dt = dt.now() - timedelta(days=14) dt_list = logic.dates_in_range(start_dt, end_dt, 14) [self.assertTrue(isinstance(dto, datetime)) for dto in dt_list]
def test_standard_case(self): "Test a standard use case with 14 dates spanning the last week" today = dt.now() a_week_ago = today - timedelta(days=7) dt_list = logic.dates_in_range(a_week_ago, today, entry_count=14) self.assertEqual(len(dt_list), 14)