def setUp(self): super(DownloadMPsTest, self).setUp() self.mp = Person.objects.create( legal_name="Jimmy Stewart", slug="jimmy-stewart", email="*****@*****.**" ) # Create MP position at the National Assembly Position.objects.create( person=self.mp, organisation=self.na, start_date=ApproximateDate(past=True), end_date=ApproximateDate(future=True), title=self.member, ) # MP currently active position at the DA Position.objects.create( person=self.mp, organisation=self.da, start_date=ApproximateDate(past=True), end_date=ApproximateDate(future=True), title=self.member, ) # MP contact number Contact.objects.create( content_type=ContentType.objects.get_for_model(self.mp), object_id=self.mp.id, kind=self.voice_kind, value="987654321", preferred=True, ) Contact.objects.create( content_type=ContentType.objects.get_for_model(self.mp), object_id=self.mp.id, kind=self.cell_kind, value="5555", preferred=True, ) Contact.objects.create( content_type=ContentType.objects.get_for_model(self.mp), object_id=self.mp.id, kind=self.twitter_kind, value="@jimmysteward", preferred=True, ) Contact.objects.create( content_type=ContentType.objects.get_for_model(self.mp), object_id=self.mp.id, kind=self.facebook_kind, value="Jimmy Steward", preferred=True, ) Contact.objects.create( content_type=ContentType.objects.get_for_model(self.mp), object_id=self.mp.id, kind=self.linkedin_kind, value="jimmy_steward", preferred=True, )
def queryset(self, request, queryset): if self.value(): year = int(self.value()) start_date = ApproximateDate(year=year, month=0, day=0) end_date = ApproximateDate(year=year, month=12, day=31) return queryset.filter(date__gte=start_date).filter( date__lte=end_date) else: return queryset
def test_short_near_future(self): position = models.Position.objects.create( person=self.person, organisation=self.organisation, start_date=ApproximateDate(year=2050, month=1, day=1), end_date=ApproximateDate(year=2050, month=6, day=30), ) current_positions = models.Position.objects.all().currently_active() self.assertEqual([], list(current_positions))
def test_normal_current(self): position = models.Position.objects.create( person=self.person, organisation=self.organisation, start_date=ApproximateDate(year=2000, month=1, day=1), end_date=ApproximateDate(year=2100, month=12, day=31), ) current_positions = models.Position.objects.all().currently_active() self.assertEqual([position], list(current_positions))
def test_from_past_not_current(self): position = models.Position.objects.create( person=self.person, organisation=self.organisation, start_date=ApproximateDate(past=True), end_date=ApproximateDate(year=2010, month=1, day=1)) current_positions = models.Position.objects.all().currently_active() self.assertEqual([], list(current_positions)) position.delete()
def test_from_soon_to_future(self): position = models.Position.objects.create( person=self.person, organisation=self.organisation, start_date=ApproximateDate(year=2050, month=12, day=25), end_date=ApproximateDate(future=True)) current_positions = models.Position.objects.all().currently_active() self.assertEqual([], list(current_positions)) position.delete()
def test_popolo_place_with_session(self): example_assembly = models.Organisation.objects.create( name='Example Assembly', slug='example-assembly', kind=models.OrganisationKind.objects.create( name='Chamber', slug='chamber' ), started=ApproximateDate(2009), ended=ApproximateDate(2011, 3, 20), ) example_session = models.ParliamentarySession.objects.create( name='Example Session', start_date=date(1970, 7, 1), end_date=date(1975, 12, 31), mapit_generation=self.generation.id, house=example_assembly, ) self.place.parliamentary_session = example_session self.place.save() self.position.place = self.place self.position.save() data = get_popolo_data('org.example', 'http://pombola.example.org/', inline_memberships=False) membership = data['memberships'][0] self.assertTrue(membership['area']) area = membership['area'] self.assertEqual( set(area.keys()), set(['area_type', 'id', 'identifier', 'name', 'session']) ) self.assertEqual(area['area_type'], 'PRV') self.assertEqual(area['name'], 'Test Province') self.assertEqual( area['id'], 'mapit:{0}'.format(self.mapit_test_province.id) ) self.assertEqual( area['identifier'], "http://pombola.example.org/mapit/area/{0}".format( self.mapit_test_province.id ) ) self.assertTrue(area['session']) session = area['session'] self.assertEqual(session['start_date'], '1970-07-01') self.assertEqual(session['end_date'], '1975-12-31') self.assertEqual(session['house_name'], 'Example Assembly') self.assertEqual(session['name'], 'Example Session') self.assertEqual(session['house_id'], example_assembly.id) self.assertEqual(session['id'], example_session.id) self.assertEqual(session['mapit_generation'], self.generation.id)
def get_approximate_date(date_str): try: date_obj = datetime.strptime(date_str, '%d/%m/%Y') return ApproximateDate(year=date_obj.year, month=date_obj.month, day=date_obj.day) except ValueError: try: date_obj = datetime.strptime(date_str, '%m/%Y') return ApproximateDate(year=date_obj.year, month=date_obj.month) except ValueError: return None return None
def test_is_upcoming(): now = timezone.now() now_event = Event(date=ApproximateDate(now.year, now.month, now.day)) yesterday = now - timedelta(days=1) yesterday_event = Event( date=ApproximateDate(yesterday.year, yesterday.month, yesterday.day)) tomorrow = now + timedelta(days=1) tomorrow_event = Event( date=ApproximateDate(tomorrow.year, tomorrow.month, tomorrow.day)) assert now_event.is_upcoming() assert not yesterday_event.is_upcoming() assert tomorrow_event.is_upcoming()
def prepare_date(self, date_str): try: date_obj = datetime.strptime(date_str, '%d/%m/%Y') return ApproximateDate(year=date_obj.year, month=date_obj.month, day=date_obj.day) except ValueError: try: date_obj = datetime.strptime(date_str, '%m/%Y') return ApproximateDate(year=date_obj.year, month=date_obj.month) except ValueError: return False return False
def handle_noargs(self, **options): for person in Person.objects.all(): party_positions = person.position_set.all().filter(title__slug='member').filter(organisation__kind__slug='party') if not party_positions: continue most_recent_party_position = party_positions[0] if most_recent_party_position.end_date == ApproximateDate(2012): most_recent_party_position.end_date = ApproximateDate(future=True) message = "2012 end_date to future for %s" % (most_recent_party_position,) if options['commit']: most_recent_party_position.save() print >> sys.stderr, "Changing " + message else: print >> sys.stderr, "Not changing " + message + "because --commit wasn't specified"
def setUp(self): self.test_kind = OrganisationKind(name="TestKind", slug="test-kind") self.organisation_future_ended = Organisation( name="Test Org", slug="test-org", kind=self.test_kind, ended=ApproximateDate(future=True) ) self.organisation_none_ended = Organisation( name="Basic Education", slug="basic-education", kind=self.test_kind, ) self.organisation_already_ended = Organisation( name="NCOP Appropriations", slug="ncop-appropriations", kind=self.test_kind, ended=ApproximateDate(2010,1,1), )
def test_display_dates(self): """Check that the date that is displayed is correct""" position = models.Position(person=self.person) # Dates that will be used for testing past = ApproximateDate(past=True) y2000 = ApproximateDate(year=2000) y2100 = ApproximateDate(year=2100) future = ApproximateDate(future=True) # test grid: start, end, uot tests = ( (None, None, ""), (None, past, "Ended"), (None, y2000, "Ended 2000"), (None, y2100, "Will end 2100"), (None, future, "Ongoing"), (past, None, "Started"), (past, past, "Ended"), (past, y2000, "Ended 2000"), (past, y2100, "Will end 2100"), (past, future, "Ongoing"), (y2000, None, "Started 2000"), (y2000, past, "Started 2000, now ended"), (y2000, y2000, "2000 → 2000"), (y2000, y2100, "2000 → 2100"), (y2000, future, "Started 2000"), (y2100, None, "Will start 2100"), (y2100, y2100, "2100 → 2100"), (y2100, future, "Will start 2100"), (future, None, "Not started yet"), (future, future, "Not started yet"), # These are impossible, but we don't validate against them. Best check something # sensible is returned. Might need if we ever do a site for Time Lords! (y2100, past, "Will start 2100, now ended"), (y2100, y2000, "2100 → 2000"), (future, past, "Ended"), (future, y2000, "Ended 2000"), (future, y2100, "Will end 2100"), ) for start_date, end_date, expected in tests: position.start_date = start_date position.end_date = end_date actual = position.display_dates() self.assertEqual( actual, expected, "%s -> %s should be '%s', not '%s'" % (start_date, end_date, expected, actual))
def handle_noargs(self, **options): pt = PositionTitle.objects.get(name='Constituency Contact') for old_position in pt.position_set.all(). \ currently_active(date_for_last_active_check): person = old_position.person print "Considering", old_position active_positions = person.position_set.all().currently_active() # Are they currently an MP or an MPL? na_memberships = active_positions.filter( organisation__slug='national-assembly', title__slug='member') # FIXME: Why are there two representations of MPLs? pl_memberships = active_positions.filter( title__slug='member', organisation__kind__slug='provincial-legislature') pl_memberships2 = active_positions.filter( title__slug='member-of-the-provincial-legislature') restart = False if na_memberships: print " Restarting because", person, "is currently a Member of the National Assembly" restart = True if pl_memberships or pl_memberships2: print " Restarting because", person, "is currently a Member of a Provincial Legislature" restart = True if restart: # Set the primary key to None so that when we save it, # that creates a new row: old_position.pk = None old_position.start_date = ApproximateDate( *date_to_start_new_positions.timetuple()[0:3]) old_position.end_date = ApproximateDate(future=True) if options['commit']: print " Saving the new position" old_position.save() else: print " Not saving the new position (--commit not specified)"
def test_submit_information_email_logic(self): """ Test event filtering logic for thank you emails. """ eight_weeks_ago = timezone.now() - timezone.timedelta(weeks=8) should_be_included = Event.objects.create(city="should be included", is_on_homepage=True, date=eight_weeks_ago) Event.objects.create(city="not on homepage", is_on_homepage=False, date=eight_weeks_ago) Event.objects.create(city="uncertain date", is_on_homepage=True, date=ApproximateDate(year=eight_weeks_ago.year, month=eight_weeks_ago.month)) Event.objects.create(city="data already provided", is_on_homepage=True, date=eight_weeks_ago, applicants_count=1, attendees_count=1) Event.objects.create(city="already sent", is_on_homepage=True, date=eight_weeks_ago, submit_information_email_sent=timezone.now()) with mock.patch('core.management.commands.handle_emails.send_mail' ) as mock_send_mail: handle_emails.send_submit_information_emails() # Only a single event should have been picked up self.assertEqual(mock_send_mail.call_count, 1) _, send_kwargs = mock_send_mail.call_args self.assertIn(should_be_included.city, send_kwargs['subject'])
def test_approximate_date_behaviour(self): """ Test logic for the behaviour of skipping events with approximate dates. Events with approximate dates should be skipped if ignore_approximate_dates is True """ # Create an event with an approximate date event = Event.objects.create(date=ApproximateDate(year=2017, month=1)) self.sending_args['events'] = [event] with mock.patch('core.management.commands.handle_emails.send_mail' ) as mock_send_mail: # We're ignoring approximate dates, so no email should be sent self.sending_args['ignore_approximate_events'] = True handle_emails.send_event_emails(**self.sending_args) self.assertEqual(mock_send_mail.call_count, 0) mock_send_mail.reset_mock() # Now we're not ignoring approximate dates, so a mail should be sent. self.sending_args['ignore_approximate_events'] = False handle_emails.send_event_emails(**self.sending_args) self.assertEqual(mock_send_mail.call_count, 1) mock_send_mail.reset_mock() # Still ignoring approximate dates, but now the event date is fixed, so a mail should be sent. event.date = timezone.datetime.now() event.save() handle_emails.send_event_emails(**self.sending_args) self.assertEqual(mock_send_mail.call_count, 1) mock_send_mail.reset_mock()
def get_matching_party(party_name, **options): party_name_to_use = party_name_corrections.get(party_name, party_name) # print "looking for '%s'" % (party_name_to_use,) matching_parties = Organisation.objects.filter( kind__slug='party', name__iexact=party_name_to_use) if not matching_parties: matching_parties = Organisation.objects.filter( kind__slug='party', name__istartswith=party_name_to_use) if len(matching_parties) == 0: if options.get('create', True): party_name_for_creation = party_name_to_use.title() new_party = Organisation( name=party_name_for_creation, slug=slugify(party_name_for_creation), started=ApproximateDate(datetime.date.today().year), ended=None, kind=OrganisationKind.objects.get(slug='party')) maybe_save(new_party, **options) return new_party else: raise CommandError, "Would have to create %s, but 'create' was False" % ( party_name, ) elif len(matching_parties) == 1: return matching_parties[0] else: raise Exception, "Multiple parties matched %s" % (party_name_to_use, )
def tidy_date(date): if not date: return None if date == 'future': return ApproximateDate(future=True) if re.match(r'^\d{4}$', date): return ApproximateDate(year=int(date)) month_year = re.match(r'^(\d{2})-(\d{4})$', date) if month_year: month, year = month_year.groups() return ApproximateDate(month=int(month), year=int(year)) print 'bad date: %s' % date
def event(request, city): now = timezone.now() now_approx = ApproximateDate(year=now.year, month=now.month, day=now.day) event = get_object_or_404(Event, page_url=city.lower()) if event.page_url != city: return redirect('core:event', city=event.page_url, permanent=True) user = request.user user_is_organizer = user.is_authenticated and event.has_organizer(user) is_preview = 'preview' in request.GET previewable = user.is_superuser or user_is_organizer or is_preview if not (event.is_page_live or previewable): return render(request, 'applications/event_not_live.html', { 'city': city, 'past': event.date <= now_approx }) return render( request, "core/event.html", { 'event': event, 'menu': event.menu.all(), 'content': event.content.prefetch_related('coaches', 'sponsors').filter(is_public=True), })
def adjust_approximate_date(ad, by_days): """Return an ApproximateDate offset from another by some days This refuses to adjust a 'future' ApproximateDate, and treats those without month or day specified as being on the first day of the month, or the first day of the year respectively. >>> ad = ApproximateDate(2014, 2, 17) >>> adjust_approximate_date(ad, -1) 2014-02-16 >>> ad = ApproximateDate(2014, 1, 1) >>> adjust_approximate_date(ad, -1) 2013-12-31 >>> ad = ApproximateDate(2014, 2) >>> adjust_approximate_date(ad, 50) 2014-03-23 >>> ad = ApproximateDate(2014) >>> adjust_approximate_date(ad, 40) 2014-02-10 """ if ad.future: raise Exception, "You can't adjust a future date" day = ad.day or 1 month = ad.month or 1 d = datetime.date(ad.year, month, day) d = d + datetime.timedelta(days=by_days) return ApproximateDate(d.year, d.month, d.day)
def test_approximate_date_behaviour(mailoutbox, send_kwargs): """ Test logic for the behaviour of skipping events with approximate dates. Events with approximate dates should be skipped if ignore_approximate_dates is True """ # Create an event with an approximate date event = Event.objects.create(date=ApproximateDate(year=2017, month=1), email="*****@*****.**") send_kwargs['events'] = [event] # We're ignoring approximate dates, so no email should be sent send_kwargs['ignore_approximate_events'] = True handle_emails.send_event_emails(**send_kwargs) assert len(mailoutbox) == 0 # Now we're not ignoring approximate dates, so a mail should be sent. send_kwargs['ignore_approximate_events'] = False handle_emails.send_event_emails(**send_kwargs) assert len(mailoutbox) == 1 # Still ignoring approximate dates, but now the event date is fixed, # so a mail should be sent. event.date = timezone.datetime.now() event.save() handle_emails.send_event_emails(**send_kwargs) assert len(mailoutbox) == 2
def test_submit_information_email_logic(mailoutbox): """ Test event filtering logic for thank you emails. """ eight_weeks_ago = timezone.now() - timezone.timedelta(weeks=8) should_be_included = Event.objects.create(city="should be included", is_on_homepage=True, date=eight_weeks_ago, email="*****@*****.**") Event.objects.create(city="not on homepage", is_on_homepage=False, date=eight_weeks_ago, email="*****@*****.**") Event.objects.create(city="uncertain date", is_on_homepage=True, date=ApproximateDate(year=eight_weeks_ago.year, month=eight_weeks_ago.month), email="*****@*****.**") Event.objects.create(city="data already provided", is_on_homepage=True, date=eight_weeks_ago, applicants_count=1, attendees_count=1, email="*****@*****.**") Event.objects.create(city="already sent", is_on_homepage=True, date=eight_weeks_ago, submit_information_email_sent=timezone.now(), email="*****@*****.**") handle_emails.send_submit_information_emails() # Only a single event should have been picked up assert len(mailoutbox) == 1 assert should_be_included.city in mailoutbox[0].subject
def event(request, page_url): now = timezone.now() now_approx = ApproximateDate(year=now.year, month=now.month, day=now.day) event_obj = get_object_or_404(Event, page_url=page_url.lower()) user = request.user user_is_organizer = user.is_authenticated and event_obj.has_organizer(user) is_preview = 'preview' in request.GET can_preview = user.is_superuser or user_is_organizer or is_preview if event_obj.date: is_past = event_obj.date <= now_approx else: is_past = False if not (event_obj.is_page_live or can_preview) or event_obj.is_frozen: return render(request, 'applications/event_not_live.html', { 'city': event_obj.city, 'page_url': page_url, 'past': is_past }) return render( request, "core/event.html", { 'event': event_obj, 'menu': event_obj.menu.all(), 'content': event_obj.content.prefetch_related( 'coaches', 'sponsors').filter(is_public=True), })
def event(request, city): now = timezone.now() now_approx = ApproximateDate(year=now.year, month=now.month, day=now.day) page = get_object_or_404(EventPage.objects.select_related('event'), url=city.lower()) if page.url != city: return redirect('core:event', city=page.url, permanent=True) can_show = request.user.is_authenticated() or 'preview' in request.GET if not page.is_live and not can_show: return render(request, 'applications/event_not_live.html', { 'city': city, 'past': page.event.date <= now_approx }) return render( request, "core/event.html", { 'page': page, 'menu': page.menu.all(), 'content': page.content.prefetch_related('coaches', 'sponsors').filter(is_public=True), })
def test_template_content(self): template = 'snippets/search_list.html' letter = LetterFactory( date=ApproximateDate(1862, 1, 1), writer=CorrespondentFactory(first_names='Francis P.', last_name='Black'), recipient=CorrespondentFactory(first_names='Eveline', last_name='Johnston'), place=PlaceFactory(name='Manassas Junction', state='Virginia')) Sentiment = namedtuple('Sentiment', ['id', 'name']) sentiments = [Sentiment(id=123, name='Positive/negative')] search_results = [(letter, 'sentiment highlight', sentiments, 'sentiment score')] rendered = render_to_string(template, context={'search_results': search_results}) for heading in ['Date', 'Writer', 'Recipient', 'Place']: self.assertIn(heading, rendered, "Heading '{}' should appear in HTML".format(heading)) self.assertIn(letter.list_date(), rendered, 'Letter list_date() should appear in HTML') self.assertIn(str(letter.writer), rendered, 'Letter writer should appear in HTML') self.assertIn(str(letter.recipient), rendered, 'Letter recipient should appear in HTML') self.assertIn(str(letter.place), rendered, 'Letter place should appear in HTML') self.assertIn('123', rendered, 'Sentiment ID should appear in HTML') self.assertIn('Positive/negative', rendered, 'Sentiment name should appear in HTML') self.assertIn('sentiment highlight', rendered, 'Sentiment highlight should appear in HTML') self.assertIn('sentiment score', rendered, 'Sentiment score should appear in HTML')
def importall(): for num in range(1,120): data = one_concession(num) if not data['field_name']: continue field, created = models.Project.objects.get_or_create( project_name = data['field_name'].title(), type='field', country='TN') for partner in data['partners']: partner_name = partner[0].title() try: partner_share = int(''.join(x for x in partner[1] if x.isdigit())) except Exception: partner_share = None company, created = models.Company.objects.get_or_create( company_name = partner_name) if data['production_per_day'] == '': print('no production figure available') continue prod, created = models.Production.objects.get_or_create( project=field, company=company, date=ApproximateDate( year=int(data['production_data_date'])), commodity='oil', #XXX is it always? actual_predicted='actual', level=data['production_per_day_normalized'], per='day',) prod.save() print('imported %s' % num)
def is_upcoming(self): if not self.date: return False now = timezone.now() now = ApproximateDate(year=now.year, month=now.month, day=now.day) if now < self.date: return True return False
def test_from_recent_to_blank_future(self): position = models.Position.objects.create( person=self.person, organisation=self.organisation, start_date=ApproximateDate(year=2001, month=4, day=1), end_date='') current_positions = models.Position.objects.all().currently_active() self.assertEqual([position], list(current_positions))
def setUp(self): self.command = Command() self.letter = LetterFactory( date=ApproximateDate(1862, 1, 1), body= 'As this is the beginin of a new year I thought as I was a lone to night I ' 'would write you a few lines to let you know that we are not all ded yet.' )
def setUp(self): self.parliament_kind = models.OrganisationKind.objects.create( name="Parliament", slug="parliament", ) self.na_organisation = models.Organisation.objects.create( slug='national-assembly', name='National Assembly', kind=self.parliament_kind ) self.ncop_organisation = models.Organisation.objects.create( slug='ncop', name='National Council of Provinces', kind=self.parliament_kind ) self.current_na_member = models.Person.objects.create( legal_name='Current NA Member', slug='current-na-member' ) self.current_na_member_position = models.Position.objects.create( person=self.current_na_member, organisation=self.na_organisation, start_date=ApproximateDate(past=True), end_date=ApproximateDate(future=True) ) self.old_na_member = models.Person.objects.create( legal_name='Old NA Member', slug='old-na-member' ) self.old_na_member_position = models.Position.objects.create( person=self.old_na_member, organisation=self.na_organisation, start_date=ApproximateDate(past=True), end_date=ApproximateDate(past=True) ) self.current_ncop_member = models.Person.objects.create( legal_name='Current NCOP Member', slug='current-ncop-member' ) self.current_ncop_member_position = models.Position.objects.create( person=self.current_ncop_member, organisation=self.ncop_organisation, start_date=ApproximateDate(past=True), end_date=ApproximateDate(future=True) ) self.old_ncop_member = models.Person.objects.create( legal_name='Old NCOP Member', slug='old-ncop-member' ) self.old_ncop_member_position = models.Position.objects.create( person=self.old_ncop_member, organisation=self.ncop_organisation, start_date=ApproximateDate(past=True), end_date=ApproximateDate(past=True) )