def handle(self, *args, **options): # Initialize DES Siteattr.set_attribute("name", settings.INIT_OPTIONS['sitename'], "site name") Siteattr.set_attribute("descr", settings.INIT_OPTIONS['sitedescription'], "site description") des = DES.objects.create( domain=settings.INIT_OPTIONS['domain'], name=settings.INIT_OPTIONS['sitename'], cfg_time=time.time() ) # Initialize Superuser su, created = User.objects.get_or_create(username=settings.INIT_OPTIONS['su_username'], is_superuser=True, is_staff=True) if created: su.first_name=settings.INIT_OPTIONS['su_name'] su.last_name=settings.INIT_OPTIONS['su_surname'] su.email=settings.INIT_OPTIONS['su_email'] su.set_password(settings.INIT_OPTIONS['su_PASSWORD']) su.save() p, created = Person.objects.get_or_create(user=su) if created: p.display_name="%s %s" % (su.first_name, su.last_name) p.name=su.first_name p.surname=su.last_name p.save() # Add super user to DES_ADMIN role pr = ParamRole.get_role(DES_ADMIN, des=des) PrincipalParamRoleRelation.objects.get_or_create(user=su, role=pr)
def handle(self, *args, **options): # Initialize DES Siteattr.set_attribute("name", settings.INIT_OPTIONS['sitename'], "site name") Siteattr.set_attribute("descr", settings.INIT_OPTIONS['sitedescription'], "site description") des = DES.objects.all()[0] des.domain=settings.INIT_OPTIONS['domain'] des.name=settings.INIT_OPTIONS['sitename'] des.cfg_time=time.time() des.save() # Initialize Superuser su, created = User.objects.get_or_create(username=settings.INIT_OPTIONS['su_username'], is_superuser=True, is_staff=True) if created: su.first_name=settings.INIT_OPTIONS['su_name'] su.last_name=settings.INIT_OPTIONS['su_surname'] su.email=settings.INIT_OPTIONS['su_email'] su.set_password(settings.INIT_OPTIONS['su_PASSWORD']) su.save() p, created = Person.objects.get_or_create(user=su) if created: p.display_name="%s %s" % (su.first_name, su.last_name) p.name=su.first_name p.surname=su.last_name p.save() # Add super user to DES_ADMIN role pr = ParamRole.get_role(DES_ADMIN, des=des) PrincipalParamRoleRelation.objects.get_or_create(user=su, role=pr)
def admins(self): """ Return all users being administrators for this DES. """ # retrieve 'DES administrator' parametric role for this DES pr = ParamRole.get_role(DES_ADMIN, des=self) # retrieve all Users having this role return pr.get_users()
def referrers(self): """All User linked as platform operators for this resource. User who have role SUPPLIER_REFERRER.""" # retrieve 'Supplier Referrer' parametric role for this supplier pr = ParamRole.get_role(SUPPLIER_REFERRER, supplier=self) # retrieve all Users having this role return pr.get_users() | self.tech_referrers
def __init__(self, request, *args, **kw): self.__param_role = ParamRole.get_role(GAS_REFERRER_SUPPLIER, pact=kw['instance']) if self.__param_role.get_users(): kw['initial'] = kw.get('initial', {}).update({ 'pact_referrer' : self.__param_role.get_users()[0].person }) super(EditPactForm, self).__init__(*args, **kw) self._gas = request.resource.gas self.fields['pact_referrer'].queryset = self._gas.persons
def save(self): self.instance.gas = self._gas super(GAS_PactForm, self).save() people = self.cleaned_data.get('pact_referrer', []) log.debug("Selected referrers: %s" % people) pr = ParamRole.get_role(GAS_REFERRER_SUPPLIER, pact=self.instance) for p in people: PrincipalParamRoleRelation.objects.get_or_create(role=pr, user=p.user)
def get_allowed_transitions(obj, user): """Returns all allowed transitions for passed object and user. Takes the current state of the object into account. **Parameters:** obj The object for which the transitions should be returned. user The user for which the transitions are allowed. """ from gasistafelice.gas.models import GASSupplierOrder, GASMemberOrder from flexi_auth.models import ParamRole from gasistafelice.consts import GAS_MEMBER, GAS_REFERRER_SUPPLIER, GAS_REFERRER_TECH, DES_ADMIN if isinstance(obj, GASSupplierOrder): param_roles = [ ParamRole.get_role(GAS_REFERRER_SUPPLIER, pact=obj.pact), ParamRole.get_role(GAS_REFERRER_TECH, gas=obj.gas), ParamRole.get_role(DES_ADMIN, des=obj.des), ] elif isinstance(obj, GASMemberOrder): param_roles = [ ParamRole.get_role(GAS_MEMBER, obj.gas) ] else: return workflows.utils.get_allowed_transitions(obj, user) for pr in param_roles: if user in pr.get_users(): rv = workflows.utils.get_allowed_transitions(obj, user) break elif isinstance(obj, GASSupplierOrder) and user.person == obj.referrer_person: #FIXME: ugly ! rv = workflows.utils.get_allowed_transitions(obj, user) break else: rv = [] return rv
def _get_resource_list(self, request): """Rather than adding a 'users' method to the resource, we compute users list here, because users may be not still bound to the correspondent Person. This block is in fact used only for Admin purposes during a specific stage of the registration process. """ # User list pr = ParamRole.get_role(GAS_MEMBER, gas=request.resource) users = pr.get_users() users = users.filter(registrationprofile__activation_key=RegistrationProfile.ACTIVATED) return users.order_by('last_name', 'first_name')
def save(self): self.instance.gas = self._gas super(GASAddPactForm, self).save() people = self.cleaned_data.get('pact_referrers', []) log.debug("Selected referrers: %s" % people) # Set new referrers #TODO: refactoring needed in model (referrers_add Pact method or something better...) pr = ParamRole.get_role(GAS_REFERRER_SUPPLIER, pact=self.instance) for p in people: PrincipalParamRoleRelation.objects.get_or_create(role=pr, user=p.user)
def get_allowed_transitions(obj, user): """Returns all allowed transitions for passed object and user. Takes the current state of the object into account. **Parameters:** obj The object for which the transitions should be returned. user The user for which the transitions are allowed. """ from gf.gas.models import GASSupplierOrder, GASMemberOrder from flexi_auth.models import ParamRole from consts import GAS_MEMBER, GAS_REFERRER_SUPPLIER, GAS_REFERRER_TECH, DES_ADMIN if isinstance(obj, GASSupplierOrder): param_roles = [ ParamRole.get_role(GAS_REFERRER_SUPPLIER, pact=obj.pact), ParamRole.get_role(GAS_REFERRER_TECH, gas=obj.gas), ParamRole.get_role(DES_ADMIN, des=obj.des), ] elif isinstance(obj, GASMemberOrder): param_roles = [ParamRole.get_role(GAS_MEMBER, obj.gas)] else: return workflows.utils.get_allowed_transitions(obj, user) for pr in param_roles: if user in pr.get_users(): rv = workflows.utils.get_allowed_transitions(obj, user) break elif isinstance( obj, GASSupplierOrder) and user.person == obj.referrer_person: #FIXME: ugly ! rv = workflows.utils.get_allowed_transitions(obj, user) break else: rv = [] return rv
def _get_resource_list(self, request): """Rather than adding a 'users' method to the resource, we compute users list here, because users may be not still bound to the correspondent Person. This block is in fact used only for Admin purposes during a specific stage of the registration process. """ # User list pr = ParamRole.get_role(SUPPLIER_REFERRER, supplier=request.resource) users = pr.get_users() users = users.filter( registrationprofile__activation_key=RegistrationProfile.ACTIVATED) return users.order_by('last_name', 'first_name')
def save(self): super(EditPactForm, self).save() people = self.cleaned_data.get('pact_referrer', []) log.debug("Selected referrers: %s" % people) pr = ParamRole.get_role(GAS_REFERRER_SUPPLIER, pact=self.instance) referrers_users = [] for p in people: p, created = PrincipalParamRoleRelation.objects.get_or_create(role=pr, user=p.user) referrers_users.append(p.user) for u in pr.get_users(): if u not in referrers_users: PrincipalParamRoleRelation.objects.delete(role=pr, user=u)
def save(self): super(EditPactForm, self).save() people = self.cleaned_data.get('pact_referrers', []) log.debug("Selected referrers: %s" % people) pr = ParamRole.get_role(GAS_REFERRER_SUPPLIER, pact=self.instance) referrers_users = [p.user for p in people] for u in pr.get_users(): if u not in referrers_users: PrincipalParamRoleRelation.objects.filter(role=pr, user=u).delete() for u in referrers_users: p, created = PrincipalParamRoleRelation.objects.get_or_create( role=pr, user=u)
def profile_callback(self, user): user.first_name = self.cleaned_data['name'] user.last_name = self.cleaned_data['surname'] user.save() #2-Create base objects try: place, created = Place.objects.get_or_create( city=self.cleaned_data['city'], address='', name='' ) except Place.MultipleObjectsReturned as e: places = Place.objects.filter(city=self.cleaned_data['city'], address='', name='') place = places[0] log.warning("Multiple places found: %s id = %s" % ( place, map(lambda x : x.pk, places)) ) try: contact_email, created = \ Contact.objects.get_or_create(flavour="EMAIL", value=self.cleaned_data['email']) except Contact.MultipleObjectsReturned as e: contacts = Contact.objects.filter(flavour="EMAIL", value=self.cleaned_data['email']) contact_email = contacts[0] log.warning("Multiple contact found: %s id = %s" % ( contact_email, map(lambda x : x.pk, contacts)) ) try: contact_phone, created = \ Contact.objects.get_or_create(flavour="PHONE", value=self.cleaned_data['phone']) except Contact.MultipleObjectsReturned as e: contacts = Contact.objects.filter(flavour="PHONE", value=self.cleaned_data['phone']) contact_phone = contacts[0] log.warning("Multiple contact found: %s id = %s" % ( contact_phone, map(lambda x : x.pk, contacts)) ) #3-Create person # COMMENT matteo: even if there are already one (or more) persons with the same values (name, # username, place) it is not a problem, the data will be normalized after. person = Person( name = self.cleaned_data['name'], surname = self.cleaned_data['surname'], address = place, user = user ) person.save() person.contact_set.add( contact_email, contact_phone) gas = self.cleaned_data.get('gas_choice') if gas: # COMMENT fero: following our workflow we can't bind person to GAS now # we can bind User to role GAS Member for this GAS # in the activation phase (4a.) we would perform this step gm = GASMember( person=person, gas=gas ) gm.save() #WAS: pr = ParamRole.get_role(GAS_MEMBER, gas=gas) #WAS: ppr = PrincipalParamRoleRelation.objects.create(user=user, role=pr) #WAS: ppr.save() given_token = self.cleaned_data.get('gas_registration_token') if given_token: token_comment_text = "%s%s" % (DESRegistrationForm.REGISTRATION_TOKEN_PREFIX, given_token) Comment.objects.create( site = DjangoSite.objects.all()[0], content_object=gm, comment=token_comment_text ) #Send email for GAS_REFERRER_TECH techs = gas.tech_referrers_people if techs: #TODO Matteo: put also 'city' and 'phone' form fields in this email body = _("new registration from %(username)s %(usersurname)s with email %(email)s. User active status is %(active)s. Motivation: %(motivation)s...") % { 'username' : user.first_name, 'usersurname' : user.last_name, 'email' : user.email, 'active' : user.is_active, 'motivation' : self.cleaned_data['motivation'], } #from notification.models import Notice #GAS_REFERRER_TECH for tech in techs: #TODO: Notification or send email recipient = tech.user.email recipient = tech.preferred_email_contacts log.debug("Send email for tech %s with body %s" % (recipient, body)) #FORUM #FIXME: not set! Quale è l'email del FORUM del GAS? if gas.orders_email_contact: #TODO: Notification or send email forum = gas.orders_email_contact.value log.debug("Send email for FORUM %s with body %s" % (forum, body)) supplier = self.cleaned_data.get('supplier_choice') if supplier: pr = ParamRole.get_role(SUPPLIER_REFERRER, supplier=supplier) ppr = PrincipalParamRoleRelation.objects.create(user=user, role=pr) ppr.save()