Пример #1
0
 def __init__(self, owned_by, middleman_ref=None):
     super(PersonBean,self).__init__(owned_by)
     if middleman_ref:
         self.middleman_ref = middleman_ref
     else:
         self.middleman_ref = None
     self.name = ""
     self.nickname = ""
     self.lastname = ""
     self.introduction = ""
     self.parent = None
     self.birthday = FuzzyDate(year=0,month=0,day=0)
Пример #2
0
class PersonBean(ContactBean):
    @classmethod
    def new_person(cls, owned_by):
        """factory method for a new person bean"""
        return PersonBean(owned_by,None)

    @classmethod
    def load(cls, key):
        entity = Person.get(key)
        person = PersonBean(entity.owned_by,None)
        person.entity = entity
        person.name = entity.name
        person.nickname = entity.nickname
        person.lastname = entity.lastname
        person.birthday = entity.birthday
        person.introduction = entity.introduction
        if entity.middleman_ref:
            person.middleman_ref = str(entity.middleman_ref.key())
        return person

    @classmethod
    def edit(cls, owned_by, request):
        """factory method for a new person bean"""
        person = PersonBean(owned_by,None)
        if request.get('Person_key', None):
            person.entity = Person.get(request.get('Person_key'))
        person.name = request.get('name')
        person.nickname = request.get('nickname',"")
        person.lastname = request.get('lastname',"")
        person.introduction = request.get('introduction',"")
        person.middleman_ref = request.get('middleman_ref',None)
        try:
            birthday = int(request.get("birthday", None))
        except ValueError:
            birthday = 0
        except TypeError:
            birthday = 0
        try:
            birthmonth = int(request.get("birthmonth", None))
        except ValueError:
            birthmonth = 0
        except TypeError:
            birthmonth = 0
        try:
            birthyear = int(request.get("birthyear", None))
        except ValueError:
            birthyear = 0
        except TypeError:
            birthyear = 0
        person.birthday = FuzzyDate(day=birthday,month=birthmonth,year=birthyear)
        return person

    @classmethod
    def new_person_via_middleman(cls, owned_by, middleman_ref):
        """factory method for a new person wirth middleman bean"""
        return PersonBean(owned_by,middleman_ref)

    def __init__(self, owned_by, middleman_ref=None):
        super(PersonBean,self).__init__(owned_by)
        if middleman_ref:
            self.middleman_ref = middleman_ref
        else:
            self.middleman_ref = None
        self.name = ""
        self.nickname = ""
        self.lastname = ""
        self.introduction = ""
        self.parent = None
        self.birthday = FuzzyDate(year=0,month=0,day=0)

    def validate(self):
        if len(self.name) < 1:
            return ['Name is required']
        return []

    def get_template_values(self):
        """return person data as template_values"""
        super(PersonBean,self).get_template_values()
        self.template_values.update(prepare_birthday_selectors())
        if self.middleman_ref:
            self.template_values['middleman_ref'] = self.middleman_ref
            # look him up to fill the fields
            middleman = Person.get(db.Key(self.middleman_ref))
            if middleman:
                self.template_values['middleman_name'] = middleman.name
                self.template_values['middleman_lastname'] = middleman.lastname
        self.template_values['name'] = self.name
        self.template_values['nickname'] = self.nickname
        self.template_values['lastname'] = self.lastname
        self.template_values['introduction'] = self.introduction
        self.template_values['birthday'] = str(self.birthday.get_day())
        self.template_values['birthmonth'] = str(self.birthday.get_month())
        self.template_values['birthyear'] = str(self.birthday.get_year())
        return self.template_values

    def put(self):
        if self.middleman_ref:
            middleman = db.Key(self.middleman_ref)
        else:
            middleman = None
        try:
            self.entity.name = self.name
            self.entity.nickname = self.nickname
            self.entity.lastname = self.lastname
            self.entity.introduction = self.introduction
            self.entity.birthday = self.birthday
            self.entity.middleman_ref = middleman
            # parent is needed for building an entity group with LoginUser
            self.entity.parent = self.parent
            self.entity.put()
        except AttributeError:
            # prepare database object for new person
            self.entity = Person(parent=self.parent, owned_by=self.owned_by, name=self.name,
                                lastname=self.lastname,
                                nickname=self.nickname, birthday=self.birthday,
                                introduction=self.introduction, middleman_ref=middleman)
            self.entity.put()
        if not self.parent:
            # generate search keys for contact; cannot run in transaction context
            update_index(self.entity)
        # delete birthday memcache
        memcache.delete('birthdays',namespace=str(self.entity.owned_by.key()))