def save(self, editor): """Save elder (optionally in context of given village relationship).""" self.instance.name = self.cleaned_data['name'] old_phone = self.instance.phone new_phone = self.cleaned_data['phone'] or None self.instance.phone = new_phone old_profile_role = self.instance.role old_relationship_role = ( self.rel.description_or_role if self.rel else self.instance.role) new_role = self.cleaned_data['role'] if old_profile_role == old_relationship_role: self.instance.role = new_role self.instance.relationships_from.filter( description=old_profile_role).update( description='') else: self.rel.description = new_role self.rel.save() self.instance.save() if new_phone and new_phone != old_phone: invites.send_invite_sms( self.instance, template_name='sms/invite_elder.txt', extra_context={ 'inviter': editor, 'student': self.rel.student if self.rel else None, }, ) return self.instance
def save(self, editor): """Save elder (optionally in context of given village relationship).""" self.instance.name = self.cleaned_data['name'] old_phone = self.instance.phone new_phone = self.cleaned_data['phone'] or None self.instance.phone = new_phone old_profile_role = self.instance.role old_relationship_role = (self.rel.description_or_role if self.rel else self.instance.role) new_role = self.cleaned_data['role'] if old_profile_role == old_relationship_role: self.instance.role = new_role self.instance.relationships_from.filter( description=old_profile_role).update(description='') else: self.rel.description = new_role self.rel.save() self.instance.save() if new_phone and new_phone != old_phone: invites.send_invite_sms( self.instance, template_name='sms/invite_elder.txt', extra_context={ 'inviter': editor, 'student': self.rel.student if self.rel else None, }, ) return self.instance
def save(self, rel=None): """ Save/return new elder profile & send invite, or return existing. Optionally takes ``rel`` between inviting elder and student. If given will override rel given at initialization time. A rel must be given either at initialization or save time. """ if rel is not None: self.rel = rel inviter = self.rel.elder name = self.cleaned_data.get('name') phone = self.cleaned_data.get('phone') relationship = self.cleaned_data.get('role', u"") if self.instance is None: self.instance = model.Profile.create_with_user( school=inviter.school, name=name, phone=phone, role=relationship, is_active=True, school_staff=False, invited_by=inviter, ) else: # remove other existing relationships with teacher-less students model.Relationship.objects.filter( from_profile=self.instance).exclude( from_profile__school_staff=True).exclude( to_profile=self.rel.student).delete() # send invite SMSes invites.send_invite_sms( self.instance, template_name='sms/invite_elder.txt', extra_context={ 'inviter': model.elder_in_context(self.rel), 'student': self.rel.student, }, ) rel, created = model.Relationship.objects.get_or_create( from_profile=self.instance, to_profile=self.rel.student, defaults={ 'description': relationship, } ) if not created and not rel.direct: rel.direct = True rel.save() return self.instance
def save(self, rel=None): """ Save/return new elder profile & send invite, or return existing. Optionally takes ``rel`` between inviting elder and student. If given will override rel given at initialization time. A rel must be given either at initialization or save time. """ if rel is not None: self.rel = rel inviter = self.rel.elder name = self.cleaned_data.get('name') phone = self.cleaned_data.get('phone') relationship = self.cleaned_data.get('role', u"") if self.instance is None: self.instance = model.Profile.create_with_user( school=inviter.school, name=name, phone=phone, role=relationship, is_active=True, school_staff=False, invited_by=inviter, ) else: # remove other existing relationships with teacher-less students model.Relationship.objects.filter( from_profile=self.instance).exclude( from_profile__school_staff=True).exclude( to_profile=self.rel.student).delete() # send invite SMSes invites.send_invite_sms( self.instance, template_name='sms/invite_elder.txt', extra_context={ 'inviter': model.elder_in_context(self.rel), 'student': self.rel.student, }, ) rel, created = model.Relationship.objects.get_or_create( from_profile=self.instance, to_profile=self.rel.student, defaults={ 'description': relationship, }) if not created and not rel.direct: rel.direct = True rel.save() return self.instance
def test_send_invite_sms_too_long(): """If the SMS invite text is too long, break it up at newlines.""" profile = mock.Mock() profile.phone = '+32165440987' profile.source_phone = '+13336660000' with mock.patch('portfoliyo.invites.loader') as mock_loader: mock_loader.render_to_string.return_value = ( "%s\n%s" % ('a' * 100, 'b' * 100)) invites.send_invite_sms(profile, 'foo', {}) assert profile.send_sms.call_count == 2 assert profile.send_sms.call_args_list[0][0][0] == 'a' * 100 assert profile.send_sms.call_args_list[1][0][0] == 'b' * 100
def test_send_invite_sms_too_long(): """If the SMS invite text is too long, break it up at newlines.""" profile = mock.Mock() profile.phone = '+32165440987' profile.source_phone = '+13336660000' with mock.patch('portfoliyo.invites.loader') as mock_loader: mock_loader.render_to_string.return_value = ("%s\n%s" % ('a' * 100, 'b' * 100)) invites.send_invite_sms(profile, 'foo', {}) assert profile.send_sms.call_count == 2 assert profile.send_sms.call_args_list[0][0][0] == 'a' * 100 assert profile.send_sms.call_args_list[1][0][0] == 'b' * 100