def test_that_importing_a_spreadsheet_over_modified_fields_overwrites_them(self): """Test with first name, email, phone number, address two, (if appropriate) donation amount, donation date, vol shift hours, and vol shift date""" # import a spreadsheet people_info = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] # make a spreadsheet of them. s =self._create_a_person_spreadsheet_and_clear_the_data() s.do_import(fields=["first_name","last_name","email","phone_number"]) # assert that they're back. people_info2 = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] self.assertEqual(people_info, people_info2) # change a value. Assert that they're not equal p = Person.objects_by_account(self.a1).all()[0] p.first_name = "foo" p.save() people_info2 = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] self.assertNotEqual(people_info, people_info2) # import again. Assert that we're back to where we started. s.do_import(fields=["first_name","last_name","email","phone_number"]) # assert that they're back. people_info2 = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] self.assertEqual(people_info, people_info2)
def test_queryset_for_new_group_rule_for_general_tag_contains(self): # create a new group rule (and TagSet) group, group_rule = self.test_create_new_group_rule_for_general_tag_contains() # hand-create a few people, some of whom match, and others who don't ppl = self._generate_people(number=6) self.create_tag_for_person(person=ppl[0], tagset_name="General", tag="really cool test tag") self.create_tag_for_person(person=ppl[2], tagset_name="General", tag="really cool test tag") self.create_tag_for_person(person=ppl[4], tagset_name="General", tag="another tag") self.create_tag_for_person(person=ppl[5], tagset_name="Donor", tag="really cool test tag") # assert the queryset string is right self.assertEqual(group_rule.queryset_filter_string, "filter(taggeditem__tag__in=Tag.objects_by_account(self.account).filter(tagset__name='General',name__icontains='test'))") # get the queryset, make sure it matches a hand-created one. qs = group.members hand_qs = Person.objects_by_account(self.account).filter(Q(pk=ppl[0].pk) | Q(pk=ppl[2].pk) ) self.assertEqualQuerySets(qs,hand_qs) # try for the other tag group group2, group_rule2 = self.test_create_new_group_rule_for_general_tag_contains(right_hand_term="another") hand_qs2 = Person.objects_by_account(self.account).filter(Q(pk=ppl[4].pk) ) self.assertEqualQuerySets(group2.members,hand_qs2)
def test_queryset_for_new_group_rule_for_custom_tag_is_exactly(self): # hand-create a few people, some of whom match, and others who don't ppl = self._generate_people(number=5) self.create_tag_for_person(person=ppl[0], tagset_name="new test tagset", tag="really cool test tag") self.create_tag_for_person(person=ppl[2], tagset_name="new test tagset", tag="really cool test tag") self.create_tag_for_person(person=ppl[4], tagset_name="new test tagset", tag="another tag") # create a new group rule group, group_rule = self.test_create_new_group_rule_for_custom_tag_is_exactly() # assert the queryset string is right self.assertEqual(group_rule.queryset_filter_string, "filter(taggeditem__tag__in=Tag.objects_by_account(self.account).filter(tagset__name='new test tagset',name__iexact='test'))") # get the queryset, make sure it matches a hand-created one. qs = group.members self.assertEqualQuerySets(qs,Person.objects_by_account(self.account).none()) # check the contains group opposite_group, opposite_group_rule = self.test_create_new_group_rule_for_custom_tag_is_exactly(right_hand_term="test", operator_name="contains") opposite_hand_qs = Person.objects_by_account(self.account).filter(Q(pk=ppl[0].pk) | Q(pk=ppl[2].pk) ) self.assertEqualQuerySets(opposite_group.members,opposite_hand_qs) # try for the other tag group group2, group_rule2 = self.test_create_new_group_rule_for_custom_tag_is_exactly(right_hand_term="another tag") hand_qs2 = Person.objects_by_account(self.account).filter(Q(pk=ppl[4].pk)) self.assertEqualQuerySets(group2.members,hand_qs2)
def test_queryset_for_new_group_rule_for_volunteer_status_is_inactive(self): from volunteers import VOLUNTEER_STATII # hand-create a few people, some of whom match, and others who don't ppl = self._generate_people() v = ppl[2].volunteer v.status = VOLUNTEER_STATII[2][0] #inactive v.save() v = ppl[4].volunteer v.status = VOLUNTEER_STATII[3][0] #temporarily inactive v.save() # create a new group rule group, group_rule = self.test_create_new_group_rule_for_volunteer_status_is_inactive() self.assertEqual(group_rule.queryset_filter_string, "filter(volunteer__status='inactive')") # get the queryset, make sure it matches a hand-created one. qs = group.members hand_qs = Person.objects_by_account(self.account).filter( Q(pk=ppl[2].pk) ) self.assertEqualQuerySets(qs,hand_qs) # check the opposite opposite_group, opposite_group_rule = self.test_create_new_group_rule_for_volunteer_status_is_inactive(operator_name="is not") oppostite_hand_qs = Person.objects_by_account(self.account).filter(Q(pk=ppl[0].pk) | Q(pk=ppl[1].pk) | Q(pk=ppl[3].pk) | Q(pk=ppl[4].pk) ) self.assertEqualQuerySets(opposite_group.members,oppostite_hand_qs)
def add_person_via_organization_search_results(request): people = Person.objects_by_account(request.account).none() if 'q' in request.GET: q = request.GET['q'] if q != "": qs = Person.objects_by_account(request.account).all() people = Person.search(q,queryset=qs,require_queryset=True, ignorable_chars=["-","(",")"]) return {"fragments":{"new_person_search_results":render_to_string("organizations/_add_person_to_org_results.html", locals())}}
def test_get_row(self): s = self.created_and_imported_csv_spreadsheet() # assert that the get_row function returns what I thought it should p1 = Person.objects_by_account(self.a1).all()[0] p2 = Person.objects_by_account(self.a1).all()[2] self.assertEqual(s.get_row(0), self._mailing_list_row_dict(p1)) self.assertEqual(s.get_row(2), self._mailing_list_row_dict(p2))
def test_num_rows(self): # get a sample random spreadsheet with a number of rows I know s = self.created_and_imported_csv_spreadsheet() # assert that the num_rows function returns what I thought it should self.assertEqual(s.num_rows, Person.objects_by_account(self.a1).count()) s2 = self.created_and_imported_excel_spreadsheet() # assert that the num_rows function returns what I thought it should self.assertEqual(s2.num_rows, Person.objects_by_account(self.a1).count())
def test_get_rows(self): s = self.created_and_imported_csv_spreadsheet() # assert that the get_rows function returns what I thought it should p1 = Person.objects_by_account(self.a1).all()[0] p2 = Person.objects_by_account(self.a1).all()[1] p3 = Person.objects_by_account(self.a1).all()[2] people = [p1, p2, p3] self.assertEqual(s.get_rows(0,3), [self._mailing_list_row_dict(p) for p in people ])
def get_target_object_people_Person(self): from people.models import Person if self.has_sufficient_fields_for_identity: # find the matching row # print self.data if "first_name" in self.data and "last_name" in self.data and ( self.data["first_name"] != "" or self.data["last_name"] != ""): q = Person.objects_by_account(self.account).all() if "first_name" in self.data: q = q.filter(first_name=self.data["first_name"]) if "last_name" in self.data: q = q.filter(last_name=self.data["last_name"]) if q.count() == 1: return q[0], False # print "first and last name were not enough" q = Person.objects_by_account(self.account).all() if "email" in self.data and self.data["email"] != "": q = q.filter(email=self.data["email"]) if q.count() == 1: return q[0], False elif q.count() > 1: if "first_name" in self.data: q = q.filter(first_name=self.data["first_name"]) if "last_name" in self.data: q = q.filter(last_name=self.data["last_name"]) if q.count() == 1: return q[0], False elif q.count() > 1: if "phone_number" in self.data: q = q.filter(phone_number=self.data["phone_number"]) if q.count() == 1: return q[0], False # print "email based search wasn't enough" q = Person.objects_by_account(self.account).all() if "phone_number" in self.data and self.data["phone_number"] != "": q = q.filter(phone_number=self.data["phone_number"]) if q.count() == 1: return q[0], False elif q.count() > 1: if "first_name" in self.data: q = q.filter(first_name=self.data["first_name"]) if "last_name" in self.data: q = q.filter(last_name=self.data["last_name"]) if q.count() == 1: return q[0], False # print "phone based search wasn't enough." return Person.raw_objects.create(account=self.account), True
def _that_importing_a_person_spreadsheet_refills_an_emptied_database(self, file_type=None, extension=None, fields=["first_name","last_name","email","phone_number"]): # create a bunch of people, store their info in a dict people_info = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] # make a spreadsheet of them. s = self._create_a_person_spreadsheet_and_clear_the_data(file_type, extension) s.do_import(fields=fields) # assert that they're back. people_info2 = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] self.assertEqual(people_info, people_info2)
def _account_numbers_dict(account): start_of_this_year = datetime.date(month=1, day=1, year=datetime.date.today().year) total_donations = Donation.objects_by_account(account).filter(date__gte=start_of_this_year).count() if not total_donations: total_donations = 0 total_donors = Donation.objects_by_account(account).filter(date__gte=start_of_this_year).order_by().all().aggregate(Count('donor', distinct=True))["donor__count"] if not total_donors: total_donors = 0 total_donation_amount = Donation.objects_by_account(account).filter(date__gte=start_of_this_year).order_by().all().aggregate(Sum('amount'))["amount__sum"] if not total_donation_amount: total_donation_amount = 0 average_donation = Donation.objects_by_account(account).filter(date__gte=start_of_this_year).order_by().all().aggregate(Avg('amount'))["amount__avg"] if not average_donation: average_donation = 0 total_volunteer_hours = CompletedShift.objects_by_account(account).filter(date__gte=start_of_this_year).order_by().all().aggregate(Sum('duration'))["duration__sum"] if not total_volunteer_hours: total_volunteer_hours = 0 total_people = Person.objects_by_account(account).count() total_orgs = Organization.objects_by_account(account).count() total_groups = Group.objects_by_account(account).count() total_tags = Tag.objects_by_account(account).count() total_taggeditems = TaggedItem.objects_by_account(account).count() recent_conversations = Conversation.objects_by_account(account).all()[:5] return locals()
def test_that_importing_the_same_spreadsheet_multiple_times_is_idempotent(self): people_info = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] # make a spreadsheet of them. s =self._create_a_person_spreadsheet_and_clear_the_data() s.do_import(fields=["first_name","last_name","email","phone_number"]) # assert that they're back. people_info2 = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] self.assertEqual(people_info, people_info2) s.do_import(fields=["first_name","last_name","email","phone_number"]) people_info2 = [self._person_dict(p) for p in Person.objects_by_account(self.a1)] self.assertEqual(people_info, people_info2)
def test_importing_a_zip_works(self): fields = ["zip",] s = self._create_a_person_spreadsheet_and_clear_the_data() s.do_import(fields=fields) # assert that they're back without names assert all(p.zip != None for p in Person.objects_by_account(self.a1))
def group_count(request): group_id = request.GET['group_id'] if group_id: group = get_or_404_by_account(Group, request.account, group_id) members = group.members else: members = Person.objects_by_account(request.account).all() return {"fragments":{"group_count":render_to_string("spreadsheets/_group_count.html", locals())}}
def test_ignoring_a_column_actually_ignores_it(self): fields = ["first_name","last_name","email",] s = self._create_a_person_spreadsheet_and_clear_the_data() s.do_import(fields=fields) # assert that they're back without names assert all(p.phone_number == None for p in Person.objects_by_account(self.a1))
def upcoming_birthdays(self): from people.models import Person, NORMALIZED_BIRTH_YEAR from dashboard import NUMBER_BIRTHDAYS_OF_TO_SHOW import datetime from django.db.models import Q today = datetime.date.today() normalized_today = datetime.date(day=today.day, month=today.month, year=NORMALIZED_BIRTH_YEAR) normalized_end_date = normalized_today + datetime.timedelta(days=30) if today.month == 12 and today.day > 24: normalized_end_date = normalized_end_date - datetime.timedelta(years=1) if Person.objects_by_account(self).filter(normalized_birthday__gte=today).count() + \ Person.objects_by_account(self).filter(normalized_birthday__lt=normalized_end_date).count() > NUMBER_BIRTHDAYS_OF_TO_SHOW-1: birthday_people = Person.objects_by_account(self).filter(Q(normalized_birthday__lt=normalized_end_date) | Q(normalized_birthday__gte=today)).all() else: birthday_people = Person.objects_by_account(self).filter(Q(normalized_birthday__lt=normalized_end_date) | Q(normalized_birthday__gte=today)) else: if Person.objects_by_account(self).filter(normalized_birthday__lt=normalized_end_date, normalized_birthday__gte=today).count() > NUMBER_BIRTHDAYS_OF_TO_SHOW-1: birthday_people = Person.objects_by_account(self).filter(normalized_birthday__lt=normalized_end_date, normalized_birthday__gte=today) else: birthday_people = Person.objects_by_account(self).filter(normalized_birthday__gte=normalized_today) if birthday_people: return birthday_people.all().order_by("birth_month","birth_day", "first_name", "last_name")[:NUMBER_BIRTHDAYS_OF_TO_SHOW] return None
def _create_a_person_spreadsheet_and_clear_the_data(self, file_type=CSV_TYPE, extension=None,): if not extension: if file_type == CSV_TYPE: extension = "csv" elif file_type == EXCEL_TYPE: extension = "xls" # create a person spreadsheet fh = Factory.people_mailing_list_spreadsheet_file(self.a1, file_type=file_type) # delete all the people Person.objects_by_account(self.a1).all().delete() # assert that they're gone self.assertEqual(Person.objects_by_account(self.a1).count(), 0) # import the spreadsheet s = SpreadsheetAbstraction(self.a1, fh, "people", filename="test.%s" % (extension,)) return s
def members(self): t = self.template_instance_class(self.account) if t.model == Person: if self.group: return self.group.members else: return Person.objects_by_account(self.account).all() else: if self.group: qs = self.group.members.filter() else: qs = Person.objects_by_account(self.account).all() filter_kwargs = {} if hasattr(t,"person_field"): filter_kwargs = {"%s__in" % t.person_field.replace(".","__") : qs} return t.model.objects_by_account(self.account).filter(**filter_kwargs) else: return qs
def _generate_people(self, number=5): from volunteers import VOLUNTEER_STATII people = [] for i in range(0,number): p = Factory.person(self.account) v = p.volunteer v.status = VOLUNTEER_STATII[1][0] v.save() people.append(p.pk) people = Person.objects_by_account(self.account).filter(pk__in=people) return people
def create_and_save_200_person_spreadsheet(self, fields=["first_name","last_name","email","phone_number"], spreadsheet_filename=None): if not spreadsheet_filename: spreadsheet_filename = "test.xls" full_filename = os.path.join(settings.PROJECT_ROOT, TEST_SPREADSHEET_PATH, spreadsheet_filename) if not os.path.exists(full_filename): Person.objects_by_account(self.account).all().delete() [Factory.person(self.account) for f in range(0,200)] fh = open(full_filename, 'w') q = Person.objects_by_account(self.account).all() SpreadsheetAbstraction.create_spreadsheet(q, fields, EXCEL_TYPE, file_handler=fh) fh.flush() fh.close() Person.objects_by_account(self.account).all().delete() return spreadsheet_filename
def test_queryset_for_new_group_rule_for_last_donation_is_after(self): # create a new group rule group, group_rule = self.test_create_new_group_rule_for_last_donation_is_after() # hand-create a few people, some of whom match, and others who don't ppl = self._generate_people() target_date = datetime.date(month=2,day=12,year=2009) Factory.donation(ppl[1], date=target_date+datetime.timedelta(days=10)) Factory.donation(ppl[2], date=target_date) Factory.donation(ppl[4], date=target_date-datetime.timedelta(days=59)) self.assertEqual(group_rule.queryset_filter_string, "filter(donor__donation__date__gt=datetime.date(month=2,day=12,year=2009))") # get the queryset, make sure it matches a hand-created one. qs = group.members hand_qs = Person.objects_by_account(self.account).filter(Q(pk=ppl[1].pk)) self.assertEqualQuerySets(qs,hand_qs) # check the opposite opposite_group, opposite_group_rule = self.test_create_new_group_rule_for_last_donation_is_after(operator_name="is before") oppostite_hand_qs = Person.objects_by_account(self.account).filter(Q(pk=ppl[4].pk)) self.assertEqualQuerySets(opposite_group.members,oppostite_hand_qs)
def test_queryset_for_new_group_rule_for_last_volunteer_shift_is_on(self): # create a new group rule (and TagSet) group, group_rule = self.test_create_new_group_rule_for_last_volunteer_shift_is_on() # hand-create a few people, some of whom match, and others who don't ppl = self._generate_people() target_date = datetime.date(month=3,day=24,year=2010) Factory.completed_volunteer_shift(ppl[2], date=target_date) Factory.completed_volunteer_shift(ppl[4], date=target_date) # assert the queryset string is right self.assertEqual(group_rule.queryset_filter_string, "filter(volunteer__completedshift__date=datetime.date(month=3,day=24,year=2010))") # get the queryset, make sure it matches a hand-created one. qs = group.members hand_qs = Person.objects_by_account(self.account).filter(Q(pk=ppl[2].pk) | Q(pk=ppl[4].pk) ) self.assertEqualQuerySets(qs,hand_qs)
def make_donors_for_each_person(cls, request): [cls.make_each_person_a_donor(Person,p,True) for p in Person.objects_by_account(request.account).all()]
def people(self): if self.group: return self.group.members else: return Person.objects_by_account(self.account).all()
def members(self): return Person.objects_by_account(self.account).filter(taggeditem__tag=self.tag)
def _db_identity_set(self): s = [] s.append([self._person_dict(p) for p in Person.objects_by_account(self.a1)]) s.append([self._donation_dict(d) for d in Donation.objects_by_account(self.a1)]) s.append([self._shift_dict(c) for c in CompletedShift.objects_by_account(self.a1)]) return s
def test_num_people_denominator(self): a = Factory.create_demo_site("test", quick=True) if Person.objects_by_account(a).count() > 0: self.assertEqual(a.num_people, Person.objects_by_account(a).count()) else: self.assertEqual(a.num_people,1)
def test_num_people(self): a = Factory.create_demo_site("test", quick=True) self.assertEqual(a.num_people, Person.objects_by_account(a).count())