class RestoreDeletedRecord(MarketingPermissionMixin, JsonResponseMixin, SingleObjectMixin, View): model = Changes http_method_names = [ 'post', ] def _restore_person(self): restore_vals = model_to_dict(self.changes_obj) # need to restore original id value restore_vals['id'] = restore_vals.pop('orig_id') # ForeignKeys are returned as keys -> need to convert to objects for key in ('created_by', 'modified_by'): restore_vals[key] = User.objects.get(pk=restore_vals.pop(key)) # action is not in Person model so remove kwarg restore_vals.pop('action') self.object = Person(**restore_vals) self.object.save() def delete(self, request, *args, **kwargs): self.deleted_pk = self.changes_obj.pk self.changes_obj.delete() return self.render_to_response(**kwargs) def get_json_data(self, **kwargs): return { 'change_id': kwargs['pk'], 'person_id': self.object.pk, } def post(self, request, *args, **kwargs): self.changes_obj = self.get_object() self._restore_person() return self.delete(request, *args, **kwargs)
def test_create_preapproval_by_querying_name(sample_person_one, sample_preapproval): sample_person_one.save() sample_preapproval.person = Person.get(Person.first_name == sample_person_one.first_name, Person.last_name == sample_person_one.last_name) sample_preapproval.save() assert sample_preapproval == Person.get(Person.last_name == sample_person_one.last_name).preapprovals.get()
class RestoreDeletedRecord(MarketingPermissionMixin, JsonResponseMixin, SingleObjectMixin, View): model = Changes http_method_names = ['post',] def _restore_person(self): restore_vals = model_to_dict(self.changes_obj) # need to restore original id value restore_vals['id'] = restore_vals.pop('orig_id') # ForeignKeys are returned as keys -> need to convert to objects for key in ('created_by', 'modified_by'): restore_vals[key] = User.objects.get(pk=restore_vals.pop(key)) # action is not in Person model so remove kwarg restore_vals.pop('action') self.object = Person(**restore_vals) self.object.save() def delete(self, request, *args, **kwargs): self.deleted_pk = self.changes_obj.pk self.changes_obj.delete() return self.render_to_response(**kwargs) def get_json_data(self, **kwargs): return { 'change_id': kwargs['pk'], 'person_id': self.object.pk, } def post(self, request, *args, **kwargs): self.changes_obj = self.get_object() self._restore_person() return self.delete(request, *args, **kwargs)
def test_rental_creation_by_querying_name(sample_person_one, sample_application, sample_rental): sample_person_one.save() sample_application.save() sample_rental.person = Person.get(Person.first_name == sample_person_one.first_name, Person.last_name == sample_person_one.last_name) sample_rental.save() assert sample_rental == Person.get(Person.last_name == sample_person_one.last_name).rentals.get()
def test_asset_creation_by_querying_name(sample_person_one, sample_application, sample_asset): sample_person_one.save() sample_application.save() sample_asset.person = Person.get(Person.first_name == sample_person_one.first_name, Person.last_name == sample_person_one.last_name) sample_asset.save() assert sample_asset == Person.get(Person.last_name == sample_person_one.last_name).assets.get()
def test_job_creation_by_querying_name(sample_person_one, sample_job, sample_application): sample_person_one.save() sample_application.save() sample_job.person = Person.get(Person.first_name == sample_person_one.first_name, Person.last_name == sample_person_one.last_name) sample_job.save() assert sample_job == Person.get(Person.last_name == sample_person_one.last_name).jobs.get()
def _new_person(self, request): self._person = Person( date_created=timezone.now(), created_by=request.user, date_modified=timezone.now(), modified_by=request.user, ) self._person.save()
def test_communication_by_querying_person(sample_person_one, sample_communication): sample_person_one.save() sample_communication.Person = Person.get(Person.first_name == sample_person_one.first_name, Person.last_name == sample_person_one.last_name) sample_communication.save() assert sample_communication == Person.get(Person.last_name == sample_person_one.last_name).communications.get()
def test_referral_creation_by_querying_referrer(sample_person_one, sample_person_two, sample_referral): sample_person_one.save() sample_person_two.save() sample_referral.referrer = Person.get(Person.first_name == sample_person_one.first_name, Person.last_name == sample_person_one.last_name) sample_referral.referral = Person.get(Person.first_name == sample_person_two.first_name, Person.last_name == sample_person_two.last_name) sample_referral.save() assert sample_referral == Person.get(Person.last_name == sample_person_one.last_name).referrals.get()
def _restore_person(self): restore_vals = model_to_dict(self.changes_obj) # need to restore original id value restore_vals['id'] = restore_vals.pop('orig_id') # ForeignKeys are returned as keys -> need to convert to objects for key in ('created_by', 'modified_by'): restore_vals[key] = User.objects.get(pk=restore_vals.pop(key)) # action is not in Person model so remove kwarg restore_vals.pop('action') self.object = Person(**restore_vals) self.object.save()
def _import_row(self, row, cell_map): person_attrs = {} for cell in UploadedCell.objects.filter(parent_row=row): if cell.cell_order in cell_map: person_attrs[cell_map[cell.cell_order]] = cell.content person_id = person_attrs.pop('id', None) email = person_attrs['email'] if 'email' in person_attrs else None try: person = Person.objects.get(pk=person_id) except Person.DoesNotExist: if email: try: person = Person.objects.filter(email=email)[0] except IndexError: person = Person( date_created=timezone.now(), created_by=self.request.user, ) except ValueError: row.has_error = True row.error_message = str(person_id) + ' is not a valid record id. ' + \ 'It should be an integer.' row.save() self.rows_imported['total'] += 1 return person_attrs['date_modified'] = timezone.now() person_attrs['modified_by'] = self.request.user if 'f1-category-split' in person_attrs: f1 = person_attrs.pop('f1-category-split') try: person_attrs['main_category'], \ person_attrs['main_category2'] = F1_SPLIT[f1].lower() except KeyError: person_attrs['main_category'] = \ person_attrs['main_category2'] = 'NA' if 'geo' in person_attrs: person_attrs['geo'] = GEO_DICT[person_attrs['geo'].lower()] \ if person_attrs['geo'].lower() in GEO_DICT else 'Unknown' try: for key, value in person_attrs.items(): setattr(person, key, value) person.save() self.rows_imported['success'] += 1 self.rows_imported['total'] += 1 if not row.row_is_first: row.delete() except Exception as e: row.has_error = True row.error_message = str(e) row.save() self.rows_imported['total'] += 1
def _import_row(self, row, cell_map): person_attrs = {} for cell in UploadedCell.objects.filter(parent_row=row): if cell.cell_order in cell_map: person_attrs[cell_map[cell.cell_order]] = cell.content person_id = person_attrs.pop('id', None) email = person_attrs['email'] if 'email' in person_attrs else None try: person = Person.objects.get(pk=person_id) except Person.DoesNotExist: if email: try: person = Person.objects.filter(email=email)[0] except IndexError: person = Person( date_created = timezone.now(), created_by = self.request.user, ) except ValueError: row.has_error = True row.error_message = str(person_id) + ' is not a valid record id. ' + \ 'It should be an integer.' row.save() self.rows_imported['total'] += 1 return person_attrs['date_modified'] = timezone.now() person_attrs['modified_by'] = self.request.user if 'f1-category-split' in person_attrs: f1 = person_attrs.pop('f1-category-split') try: person_attrs['main_category'], \ person_attrs['main_category2'] = F1_SPLIT[f1].lower() except KeyError: person_attrs['main_category'] = \ person_attrs['main_category2'] = 'NA' if 'geo' in person_attrs: person_attrs['geo'] = GEO_DICT[person_attrs['geo'].lower()] \ if person_attrs['geo'].lower() in GEO_DICT else 'Unknown' try: for key, value in person_attrs.items(): setattr(person, key, value) person.save() self.rows_imported['success'] += 1 self.rows_imported['total'] += 1 if not row.row_is_first: row.delete() except Exception as e: row.has_error = True row.error_message = str(e) row.save() self.rows_imported['total'] += 1
def test_random_person(db_fixture, hlast_name, hfirst_name, hcellphone, hemail, hbirthdate, haddress_current, haddress_mailing): with db_fixture.atomic() as txn: db_fixture.create_table(Person, True) Person.create(last_name=hlast_name, first_name=hfirst_name, cellphone=hcellphone, email=hemail, birthdate=hbirthdate, address_current=haddress_current, address_mailing=haddress_mailing, is_realtor=False) assert hfirst_name == Person.get(Person.last_name == hlast_name).first_name txn.rollback()
class Add(MarketingPermissionMixin, TemplateView): template_name = 'marketing/index_addins/table_row.html' def _duplicate_person(self, request): self._person = Person.objects.get(pk=request.POST['person_id']) self._person.pk = None self._person.created_by = request.user self._person.modified_by = request.user self._person.date_created = timezone.now() self._person.date_modified = timezone.now() self._person.do_not_call = False self._person.do_not_email = False self._person.email_alternate = '' self._person.save() def _new_person(self, request): self._person = Person( date_created=timezone.now(), created_by=request.user, date_modified=timezone.now(), modified_by=request.user, ) self._person.save() def get(self, request, *args, **kwargs): raise Http404() def post(self, request, *args, **kwargs): if 'person_id' in request.POST: try: self._duplicate_person(request) except Person.DoesNotExist: self._new_person(request) else: self._new_person(request) context = self.get_context_data(**kwargs) return super(Add, self).render_to_response(context) def get_context_data(self, **kwargs): context = super(Add, self).get_context_data(**kwargs) context['record'] = self._person context['geo_choices'] = GEO_CHOICES context['cat_choices'] = CAT_CHOICES context['div_choices'] = DIV_CHOICES return context
class Add(MarketingPermissionMixin, TemplateView): template_name = 'marketing/index_addins/table_row.html' def _duplicate_person(self, request): self._person = Person.objects.get(pk=request.POST['person_id']) self._person.pk = None self._person.created_by = request.user self._person.modified_by = request.user self._person.date_created = timezone.now() self._person.date_modified = timezone.now() self._person.save() def _new_person(self, request): self._person = Person( date_created=timezone.now(), created_by=request.user, date_modified=timezone.now(), modified_by=request.user, ) self._person.save() def get(self, request, *args, **kwargs): raise Http404() def post(self, request, *args, **kwargs): if 'person_id' in request.POST: try: self._duplicate_person(request) except Person.DoesNotExist: self._new_person(request) else: self._new_person(request) context = self.get_context_data(**kwargs) return super(Add, self).render_to_response(context) def get_context_data(self, **kwargs): context = super(Add, self).get_context_data(**kwargs) context['record'] = self._person context['geo_choices'] = GEO_CHOICES context['cat_choices'] = CAT_CHOICES context['div_choices'] = DIV_CHOICES return context
def sample_person_two(db_fixture): with db_fixture.atomic() as txn: db.create_table(Person, True) person = Person(last_name="Lothbrok", first_name="Athelstan", cellphone="098765432", email="*****@*****.**", birthdate=date(1995, 5, 1), address_current="800 Blimey Ln., Wessex, ENG, 90210", address_mailing="Same", is_realtor=True) yield person txn.rollback()
def sample_person_one(db_fixture): with db_fixture.atomic() as txn: db_fixture.create_table(Person, True) person = Person( last_name="Lothbrok", first_name="Ardeaf", cellphone="1234567890", email="*****@*****.**", birthdate=date(1995, 10, 1), address_current="700 Google Dr. #21B, San Mateo, CA, 98102", address_mailing="P.O. Box 3, San Mateo, CA, 98102", is_realtor=False) yield person txn.rollback()
def test_insert(db_fixture, person, dependent, application, preapproval, job, asset, rental, referral, communication): with db_fixture.atomic() as txn: objects = [ person, dependent, application, preapproval, job, asset, rental, referral, communication ] before_count = list() for object in objects: object.create_table(True) note("Initial {} = {}".format(object.__class__.__name__, model_to_dict(object))) for object in objects: before_count.append(object.select().count()) if isinstance(object, Person): main.insert(db_fixture, object) random_person = Person.select().order_by( fn.Random()).limit(1).get() for key, value in model_to_dict(object).items(): if key in ['person', 'parent', 'referrer', 'referral']: object.key = random_person setattr(object, "{}_id".format(key), random_person.id) try: # Need to do this try in case the application table is empty. random_application = Application.select().order_by( fn.Random()).limit(1).get() for key, value in model_to_dict(object).items(): if key in ['application']: object.key = random_application setattr(object, "{}_id".format(key), random_application.id) except Application.DoesNotExist: pass if not isinstance(object, Person): main.insert(db_fixture, object) after_count = (person.select().count(), dependent.select().count(), application.select().count()) for counts in zip(before_count, after_count): assert counts[0] == counts[1] - 1
def test_create_application_by_querying_name(sample_person_one, sample_application): sample_person_one.save() sample_application.save() assert sample_application == Person.get(Person.last_name == sample_person_one.last_name).applications.get()
def test_dependent_parent_equals_person_from_query(sample_dependent, sample_person_one): sample_person_one.save() sample_dependent.save() assert sample_dependent.parent == Person.get(Person.last_name == sample_person_one.last_name)
def test_query_matches_saved_person(sample_person_one): sample_person_one.save() assert sample_person_one.first_name == Person.get(Person.last_name == sample_person_one.last_name).first_name