Exemplo n.º 1
0
    def handle(self, *args, **options):
        # Get the user inputs.
        schema_name = options['schema_name'][0]
        order_id = int_or_none(options['id'][0])
        title = options['title'][0]
        description = options['description'][0]
        type_of = int_or_none(options['type_of'][0])
        created_by_id = int_or_none(options['created_by_id'][0])
        last_modified_by_id = int_or_none(options['last_modified_by_id'][0])

        try:
            franchise = SharedFranchise.objects.get(schema_name=schema_name)
        except SharedFranchise.DoesNotExist:
            raise CommandError(_('Franchise does not exist!'))

        try:
            created_by = SharedUser.objects.get(id=created_by_id)
            last_modified_by = SharedUser.objects.get(id=last_modified_by_id)
        except SharedFranchise.DoesNotExist:
            raise CommandError(_('User ID # does not exist.'))

        # Connection will set it back to our tenant.
        connection.set_schema(schema_name, True)  # Switch to Tenant.

        # Defensive Code: Prevent continuing if the ID# does not exist.
        if not WorkOrder.objects.filter(id=order_id).exists():
            raise CommandError(
                _('ID # does not exists, please pick another ID #.'))

        # Create the user.
        work_order = WorkOrder.objects.get(id=order_id)

        # Created tasks.
        task = TaskItem.objects.create(created_by=created_by,
                                       last_modified_by=last_modified_by,
                                       type_of=type_of,
                                       due_date=work_order.start_date,
                                       is_closed=False,
                                       job=work_order,
                                       title=title,
                                       description=description)

        self.stdout.write(self.style.SUCCESS(_('Created "TaskItem" object.')))

        work_order.latest_pending_task = task
        work_order.save()

        self.stdout.write(self.style.SUCCESS(_('Updated "WorkOrder" object.')))

        # For debugging purposes.
        self.stdout.write(
            self.style.SUCCESS(
                _('Successfully created task in tenant account.')))
Exemplo n.º 2
0
    def get_queryset(self):
        """
        List
        """
        # Extract the order id.
        order_id = utils.int_or_none(self.kwargs.get("pk"))

        # Fetch queries based by user account.
        queryset = WorkOrderDeposit.objects.none()
        if self.request.user.is_staff():
            queryset = WorkOrderDeposit.objects.filter(
                order=order_id).order_by('-created_at')
        if self.request.user.is_associate():
            queryset = WorkOrderDeposit.objects.filter(
                order=order_id,
                order__associate__owner=self.request.user).order_by(
                    '-created_at')

        # Fetch all the queries.
        s = self.get_serializer_class()
        queryset = s.setup_eager_loading(self, queryset)

        # The following code will use the 'django-filter'
        filter = WorkOrderDepositFilter(self.request.GET, queryset=queryset)
        queryset = filter.qs

        # Return our filtered list.
        return queryset
Exemplo n.º 3
0
    def handle(self, *args, **options):
        # Get the user inputs.
        schema_name = options['schema_name'][0]
        task_id = int_or_none(options['id'][0])

        try:
            franchise = SharedFranchise.objects.get(schema_name=schema_name)
        except SharedFranchise.DoesNotExist:
            raise CommandError(_('Franchise does not exist!'))

        # Connection will set it back to our tenant.
        connection.set_schema(schema_name, True)  # Switch to Tenant.

        # Defensive Code: Prevent continuing if the ID# does not exist.
        if not TaskItem.objects.filter(id=task_id).exists():
            raise CommandError(
                _('ID # does not exists, please pick another ID #.'))

        # Create the user.
        task = TaskItem.objects.get(id=task_id)
        task.delete()

        # For debugging purposes.
        self.stdout.write(self.style.SUCCESS(
            _('Successfully deleted a task.')))
Exemplo n.º 4
0
    def validate_account_type(self, value):
        """
        Include validation for valid choices.
        """
        account_type = int_or_none(value)
        if account_type is None:
            raise serializers.ValidationError("Please select a valid choice.")
        else:
            if account_type == FRONTLINE_GROUP_ID:
                return value

            last_modified_by = self.context['last_modified_by']
            if last_modified_by.is_management_or_executive_staff():
                return value
            raise serializers.ValidationError("You do not have permission to change the account type.")
    def create(self, validated_data):
        """
        Override the `create` function to add extra functinality.
        """
        #-------------------------#
        # Get validated POST data #
        #-------------------------#
        customer = validated_data.get('customer', None)
        logger.info("Detected commercial customer...")

        organization_name = validated_data.get('organization_name', None)
        organization_type_of = int_or_none(validated_data.get('organization_type_of', None))
        organization_address_country = validated_data.get('organization_address_country', None)
        organization_address_locality = validated_data.get('organization_address_locality', None)
        organization_address_region = validated_data.get('organization_address_region', None)
        organization_post_office_box_number = validated_data.get('organization_post_office_box_number', None)
        organization_postal_code = validated_data.get('organization_postal_code', None)
        organization_street_address = validated_data.get('organization_street_address', None)
        organization_street_address_extra = validated_data.get('organization_street_address_extra', None)

        organization, created = Organization.objects.update_or_create(
            name=organization_name,
            type_of=organization_type_of,
            defaults={
                'type_of': organization_type_of,
                'name': organization_name,
                'address_country': organization_address_country,
                'address_locality': organization_address_locality,
                'address_region': organization_address_region,
                'post_office_box_number': organization_post_office_box_number,
                'postal_code': organization_postal_code,
                'street_address': organization_street_address,
                'street_address_extra': organization_street_address_extra,
            }
        )
        logger.info("Created organization.")
        if created:
            logger.info("Created organization.")
            organization.owner = customer.owner
            organization.save()

        customer.organization = organization
        customer.type_of = COMMERCIAL_CUSTOMER_TYPE_OF_ID
        customer.save()
        logger.info("Attached created organization to customer.")
        # Return the validated results.
        return validated_data
Exemplo n.º 6
0
 def post(self, request, pk=None):
     """
     Create
     """
     order_id = utils.int_or_none(pk)
     client_ip, is_routable = get_client_ip(self.request)
     write_serializer = WorkOrderDepositListCreateSerializer(
         data=request.data,
         context={
             'order_id': order_id,
             'created_by': request.user,
             'created_from': client_ip,
             'created_from_is_public': is_routable,
             'franchise': request.tenant
         })
     write_serializer.is_valid(raise_exception=True)
     order_obj = write_serializer.save()
     read_serializer = WorkOrderDepositRetrieveDeleteSerializer(order_obj,
                                                                many=False)
     return Response(data=read_serializer.data,
                     status=status.HTTP_201_CREATED)
    def handle(self, *args, **options):
        franchise_schema_name = options['franchise_schema_name'][0]
        associate_id = options['associate_id'][0]

        # Connection will set it back to our tenant.
        connection.set_schema(franchise_schema_name, True) # Switch to Tenant.

        try:
            associate = Associate.objects.get(id=int_or_none(associate_id))
            self.begin_processing(associate)

        except Associate.DoesNotExist:
            raise CommandError(_('Account does not exist with the id: %s') % str(associate_id))

        # Return success message.
        self.stdout.write(
            self.style.SUCCESS(
                _('Updated associate ID #%(id)s.' % {
                    'id': str(associate_id)
                })
            )
        )
Exemplo n.º 8
0
    def create(self, validated_data):
        """
        Override the `create` function to add extra functinality:

        - Create a `User` object in the public database.

        - Create a `SharedUser` object in the public database.

        - Create a `Staff` object in the tenant database.

        - If user has entered text in the 'extra_comment' field then we will
          a `Comment` object and attach it to the `Staff` object.

        - We will attach the staff user whom created this `Staff` object.
        """
        # Format our telephone(s)
        fax_number = validated_data.get('fax_number', None)
        if fax_number:
            fax_number = phonenumbers.parse(fax_number, "CA")
        telephone = validated_data.get('telephone', None)
        if telephone:
            telephone = phonenumbers.parse(telephone, "CA")
        other_telephone = validated_data.get('other_telephone', None)
        if other_telephone:
            other_telephone = phonenumbers.parse(other_telephone, "CA")

        validated_data['fax_number'] = fax_number
        validated_data['telephone'] = telephone
        validated_data['other_telephone'] = other_telephone

        # Extract our "email" field.
        email = validated_data.get('email', None)
        personal_email = validated_data.get('personal_email', None)

        #-------------------
        # Create our user.
        #-------------------

        owner = SharedUser.objects.create(
            first_name=validated_data['given_name'],
            last_name=validated_data['last_name'],
            email=email,
            is_active=validated_data['is_active'],
            franchise=self.context['franchise'],
            was_email_activated=True
        )
        logger.info("Created shared user.")

        # Attach the user to the `group` group.
        account_type = int_or_none(validated_data.get('account_type', None))
        if account_type:
            owner.groups.set([account_type])

        # Update the password.
        password = validated_data.get('password', None)
        owner.set_password(password)
        owner.save()

        #---------------------------------------------------
        # Create our `Staff` object in our tenant schema.
        #---------------------------------------------------

        # Create an "Staff".
        staff = Staff.objects.create(
            created_by=self.context['created_by'],
            last_modified_by=self.context['created_by'],
            description=validated_data.get('description', None),

            # Person
            given_name=validated_data['given_name'],
            last_name=validated_data['last_name'],
            middle_name=validated_data['middle_name'],
            birthdate=validated_data.get('birthdate', None),
            join_date=validated_data.get('join_date', None),
            gender=validated_data.get('gender', None),

            # Misc
            created_from = self.context['created_from'],
            created_from_is_public = self.context['created_from_is_public'],
            # . . .

            # Contact Point
            area_served=validated_data.get('area_served', None),
            available_language=validated_data.get('available_language', None),
            contact_type=validated_data.get('contact_type', None),
            email=email,
            personal_email=personal_email,
            fax_number=fax_number,
            # 'hours_available', #TODO: IMPLEMENT.
            telephone=telephone,
            telephone_extension=validated_data.get('telephone_extension', None),
            telephone_type_of=validated_data.get('telephone_type_of', None),
            other_telephone=other_telephone,
            other_telephone_extension=validated_data.get('other_telephone_extension', None),
            other_telephone_type_of=validated_data.get('other_telephone_type_of', None),

            # Postal Address
            address_country=validated_data.get('address_country', None),
            address_locality=validated_data.get('address_locality', None),
            address_region=validated_data.get('address_region', None),
            post_office_box_number=validated_data.get('post_office_box_number', None),
            postal_code=validated_data.get('postal_code', None),
            street_address=validated_data.get('street_address', None),
            street_address_extra=validated_data.get('street_address_extra', None),

            # Geo-coordinate
            elevation=validated_data.get('elevation', None),
            latitude=validated_data.get('latitude', None),
            longitude=validated_data.get('longitude', None),
            # 'location' #TODO: IMPLEMENT.

            # Emergency contact
            emergency_contact_name=validated_data.get('emergency_contact_name', None),
            emergency_contact_relationship=validated_data.get('emergency_contact_relationship', None),
            emergency_contact_telephone=validated_data.get('emergency_contact_telephone', None),
            emergency_contact_alternative_telephone=validated_data.get('emergency_contact_alternative_telephone', None),
        )
        logger.info("Created staff member.")

        # Update our staff again.
        staff.owner = owner
        staff.email = email
        staff.save()
        logger.info("Attached user object to staff member.")

        #------------------------
        # Set our `Tag` objects.
        #------------------------
        tags = validated_data.get('tags', None)
        if tags is not None:
            if len(tags) > 0:
                staff.tags.set(tags)

        #-----------------------------
        # Create our `Comment` object.
        #-----------------------------
        extra_comment = validated_data.get('extra_comment', None)
        if extra_comment is not None:
            comment = Comment.objects.create(
                created_by=self.context['created_by'],
                last_modified_by=self.context['created_by'],
                text=extra_comment,
                created_from = self.context['created_from'],
                created_from_is_public = self.context['created_from_is_public']
            )
            staff_comment = StaffComment.objects.create(
                about=staff,
                comment=comment,
            )

        # Update validation data.
        # validated_data['comments'] = StaffComment.objects.filter(staff=staff)
        validated_data['created_by'] = self.context['created_by']
        validated_data['last_modified_by'] = self.context['created_by']
        validated_data['extra_comment'] = None
        validated_data['id'] = staff.id

        # Return our validated data.
        return validated_data
Exemplo n.º 9
0
    def create(self, validated_data):
        """
        Override the `create` function to add extra functinality.
        """
        #-----------------------------
        # Get our inputs.
        #-----------------------------
        image_file = validated_data.get('image_file', None)
        upload_type_of = validated_data.get('upload_type_of', None)
        upload_id = int_or_none(validated_data.get('upload_id', None))
        created_by = self.context['created_by']
        created_from = self.context['created_from']
        created_from_is_public = self.context['created_from_is_public']

        # Save our object.
        image_upload = PublicImageUpload.objects.create(
            image_file=image_file,
            created_by=created_by,
            created_from=created_from,
            created_from_is_public=created_from_is_public,
        )

        # For debugging purposes only.
        logger.info("Created public image upload.")

        # Attach the uploaded image to the specific object type.
        if upload_type_of == "associate_avatar_image":
            obj = Associate.objects.get(id=upload_id)
            if obj.avatar_image:
                obj.avatar_image.delete()
            obj.avatar_image = image_upload
            obj.last_modified_from = self.context['created_from']
            obj.last_modified_from_is_public = self.context[
                'created_from_is_public']
            obj.save()
            logger.info("Attached public image upload to associate.")

        if upload_type_of == "customer_avatar_image":
            obj = Customer.objects.get(id=upload_id)
            if obj.avatar_image:
                obj.avatar_image.delete()
            obj.avatar_image = image_upload
            obj.last_modified_from = self.context['created_from']
            obj.last_modified_from_is_public = self.context[
                'created_from_is_public']
            obj.save()
            logger.info("Attached public image upload to customer.")

        if upload_type_of == "partner_avatar_image":
            obj = Partner.objects.get(id=upload_id)
            if obj.avatar_image:
                obj.avatar_image.delete()
            obj.avatar_image = image_upload
            obj.last_modified_from = self.context['created_from']
            obj.last_modified_from_is_public = self.context[
                'created_from_is_public']
            obj.save()
            logger.info("Attached public image upload to partner.")

        if upload_type_of == "staff_avatar_image":
            obj = Staff.objects.get(id=upload_id)
            if obj.avatar_image:
                obj.avatar_image.delete()
            obj.avatar_image = image_upload
            obj.last_modified_from = self.context['created_from']
            obj.last_modified_from_is_public = self.context[
                'created_from_is_public']
            obj.save()
            logger.info("Attached public image upload to staff.")

        # Return our validated data.
        return validated_data