Example #1
0
class OfflinePurchaseRedemptionFormView(PostajobModelFormMixin,
                                        RequestFormViewBase):
    form_class = OfflinePurchaseRedemptionForm
    model = OfflinePurchase
    display_name = 'Offline Purchase'

    success_url = reverse_lazy('purchasedproducts_overview')
    add_name = 'offlinepurchase_redeem'
    update_name = None
    delete_name = None

    @method_decorator(user_is_allowed())
    def dispatch(self, *args, **kwargs):
        """
        Decorators on this function will be run on every request that
        goes through this class.

        """
        return super(OfflinePurchaseRedemptionFormView,
                     self).dispatch(*args, **kwargs)

    def set_object(self, request):
        """
        OfflinePurchases can only be redeemed (added) by generic users.

        """
        self.object = None
        if not resolve(request.path).url_name == self.add_name:
            raise Http404("postajob.views.OfflinePurchaseRedemptionFormView: "
                          "OfflinePurchases can only be redeemed, not edited "
                          "or deleted, by third-party users")
Example #2
0
class ProductFormView(PostajobModelFormMixin, RequestFormViewBase):
    form_class = ProductForm
    model = Product
    display_name = 'Product'
    prevent_delete = True

    success_url = reverse_lazy('product')
    add_name = 'product_add'
    update_name = 'product_update'

    def get(self, *args, **kwargs):
        company = get_company_or_404(self.request)
        if not can_modify(self.request.user, company, kwargs, "update product",
                          "create product"):
            return MissingActivity()

        return super(ProductFormView, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        company = get_company_or_404(self.request)
        if not can_modify(self.request.user, company, kwargs, "update product",
                          "create product"):
            return MissingActivity()
        return super(ProductFormView, self).post(*args, **kwargs)

    @method_decorator(user_is_allowed())
    def dispatch(self, *args, **kwargs):
        """
        Decorators on this function will be run on every request that
        goes through this class.

        """
        return super(ProductFormView, self).dispatch(*args, **kwargs)
Example #3
0
class JobFormView(BaseJobFormView):
    form_class = JobForm
    model = Job
    display_name = 'Job'
    template_name = 'postajob/job_form.html'

    success_url = reverse_lazy('jobs_overview')
    add_name = 'job_add'
    update_name = 'job_update'
    delete_name = 'job_delete'

    @method_decorator(user_is_allowed())
    def dispatch(self, *args, **kwargs):
        """
        Decorators on this function will be run on every request that
        goes through this class.

        """
        return super(JobFormView, self).dispatch(*args, **kwargs)

    def get(self, *args, **kwargs):
        company = get_company_or_404(self.request)
        if not can_modify(self.request.user, company, kwargs, "update job",
                          "create job"):
            return MissingActivity()

        return super(JobFormView, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        company = get_company_or_404(self.request)
        if not can_modify(self.request.user, company, kwargs, "update job",
                          "create job"):
            return MissingActivity()

        return super(JobFormView, self).post(*args, **kwargs)

    def get_queryset(self, request):
        super(JobFormView, self).get_queryset(request)
        kwargs = {'purchasedjob__isnull': True}
        self.queryset = self.queryset.filter(**kwargs)
        return self.queryset

    def get_context_data(self, **kwargs):
        context = super(JobFormView, self).get_context_data(**kwargs)
        context['order_type'] = 'job'
        return context
Example #4
0
class CompanyProfileFormView(PostajobModelFormMixin, RequestFormViewBase):
    form_class = CompanyProfileForm
    model = CompanyProfile
    prevent_delete = True
    display_name = 'Company Profile'

    success_url = reverse_lazy('purchasedmicrosite_admin_overview')
    add_name = 'companyprofile_add'
    update_name = 'companyprofile_edit'
    delete_name = 'companyprofile_delete'

    @method_decorator(user_is_allowed())
    def dispatch(self, *args, **kwargs):
        """
        Decorators on this function will be run on every request that
        goes through this class.

        """
        return super(CompanyProfileFormView, self).dispatch(*args, **kwargs)

    def set_object(self, *args, **kwargs):
        """
        Every add is actually an edit.

        """
        company = get_company_or_404(self.request)
        kwargs = {'company': company}
        self.object, _ = self.model.objects.get_or_create(**kwargs)
        return self.object

    def delete(self, request):
        raise Http404("postajob.views.CompanyProfileFormView: "
                      "delete not allowed")

    def get_context_data(self, **kwargs):
        context = super(CompanyProfileFormView,
                        self).get_context_data(**kwargs)
        company = get_company(self.request)
        context.setdefault('company', company)

        return context
Example #5
0
class OfflinePurchaseFormView(PostajobModelFormMixin, RequestFormViewBase):
    form_class = OfflinePurchaseForm
    model = OfflinePurchase
    display_name = 'Offline Purchase'

    success_url = reverse_lazy('offlinepurchase')
    add_name = 'offlinepurchase_add'
    update_name = 'offlinepurchase_update'
    delete_name = 'offlinepurchase_delete'

    @method_decorator(requires("delete offline purchase"))
    def delete(self, request):
        if self.object.redeemed_on:
            raise Http404("postajob.views.OfflinePurchaseFormView: "
                          "can't delete redeemed OfflinePurchases")
        return super(OfflinePurchaseFormView, self).delete(request)

    @method_decorator(user_is_allowed())
    def dispatch(self, *args, **kwargs):
        """
        Decorators on this function will be run on every request that
        goes through this class.

        """
        return super(OfflinePurchaseFormView, self).dispatch(*args, **kwargs)

    def get_success_url(self):
        if resolve(self.request.path).url_name == self.add_name:
            kwargs = {
                'pk': self.object.pk,
            }
            return reverse('offline_purchase_success', kwargs=kwargs)
        return self.success_url

    def set_object(self, request):
        """
        Attempting to determine what happens to Products in an already-redeemed
        OfflinePurchase is nearly impossible, so OfflinePurchases can
        only be added or deleted.

        """
        acceptable_names = [self.add_name, self.delete_name]
        if resolve(request.path).url_name not in acceptable_names:
            raise Http404("postajob.views.OfflinePurchaseFormView: "
                          "OfflinePurchases can only be added or deleted")
        return super(OfflinePurchaseFormView, self).set_object(request)

    def get(self, *args, **kwargs):
        company = get_company_or_404(self.request)
        if not can_modify(self.request.user, company, kwargs,
                          "update offline purchase",
                          "create offline purchase"):
            return MissingActivity()

        return super(OfflinePurchaseFormView, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        company = get_company_or_404(self.request)
        if not can_modify(self.request.user, company, kwargs,
                          "update offline purchase",
                          "create offline purchase"):
            return MissingActivity()

        return super(OfflinePurchaseFormView, self).post(*args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(OfflinePurchaseFormView,
                        self).get_context_data(**kwargs)
        context['show_product_labels'] = True

        return context
Example #6
0
class PurchasedProductFormView(PostajobModelFormMixin, RequestFormViewBase):
    form_class = PurchasedProductForm
    model = PurchasedProduct
    # The display name is determined by the product id and set in dispatch().
    display_name = '{product} - Billing Information'

    success_url = reverse_lazy('purchasedproducts_overview')
    add_name = 'purchasedproduct_add'
    update_name = 'purchasedproduct_update'
    delete_name = 'purchasedproduct_delete'

    purchase_field = 'product'
    purchase_model = Product

    @method_decorator(user_is_allowed())
    def dispatch(self, *args, **kwargs):
        """
        Determine and set which product is attempting to be purchased.

        """
        # The add url also has the pk for the product they're attempting
        # to purchase.

        self.product = get_object_or_404(self.purchase_model,
                                         pk=kwargs.get('product'))

        # Set the display name based on the model
        self.display_name = self.display_name.format(product=self.product)

        # If the product is free but the current user has no companies or an
        # un-filled-out profile, use the product form that only gets company
        # information.
        if self.product.cost < 0.01:
            company = get_company(self.request)
            profile = get_object_or_none(CompanyProfile, company=company)

            if company and profile and profile.address_line_one:
                invoice = Invoice.objects.create(
                    transaction_type=Invoice.FREE,
                    first_name=self.request.user.first_name,
                    last_name=self.request.user.last_name,
                    address_line_one=profile.address_line_one,
                    address_line_two=profile.address_line_two,
                    city=profile.city,
                    state=profile.state,
                    country=profile.country,
                    zipcode=profile.zipcode,
                    owner=self.product.owner,
                )
                purchase_kwargs = {
                    'product': self.product,
                    'invoice': invoice,
                    'owner': company,
                    'paid': True,
                }
                PurchasedProduct.objects.create(**purchase_kwargs)
                return redirect(self.success_url)
            else:
                self.display_name = self.product
                self.form_class = PurchasedProductNoPurchaseForm
        return super(PurchasedProductFormView, self).dispatch(*args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(PurchasedProductFormView, self).get_form_kwargs()
        kwargs['product'] = self.product
        return kwargs

    def set_object(self, request):
        """
        Purchased products can't be edited or deleted, so prevent anyone
        getting an actual object to edit/delete.

        """
        self.object = None
        if not resolve(request.path).url_name.endswith('_add'):
            raise Http404("postajob.views.PurchasedProductFormView: "
                          "PurchasedProducts can't be edited or deleted")

    def get_context_data(self, **kwargs):
        context = super(PurchasedProductFormView,
                        self).get_context_data(**kwargs)
        context['submit_btn_name'] = 'Buy'
        context[
            'submit_text'] = 'Your card will be charged when you click \"Buy\"'
        context['sidebar'] = True
        context['product'] = self.product
        return context
Example #7
0
class PurchasedJobFormView(BaseJobFormView):
    form_class = PurchasedJobForm
    model = PurchasedJob
    display_name = '{product} Job'

    success_url = reverse_lazy('purchasedproducts_overview')
    add_name = 'purchasedjob_add'
    update_name = 'purchasedjob_update'
    delete_name = 'purchasedjob_delete'
    template_name = 'postajob/job_form.html'

    purchase_field = 'purchased_product'
    purchase_model = PurchasedProduct

    def set_object(self, *args, **kwargs):
        if resolve(self.request.path).url_name == self.add_name:
            http404_view = "postajob.views.PurchasedJobFormView"
            if not self.product.can_post_more():
                # If more jobs can't be posted to the product, don't allow
                # the user to access the add view.
                raise Http404("{view}: all available jobs for this posting "
                              "have been used".format(view=http404_view))
            else:
                company = self.product.product.owner
                if company and hasattr(company, 'companyprofile'):
                    if self.request.user in company.companyprofile.blocked_users.all(
                    ):
                        # If the current user has been blocked by the company
                        # that we are trying to post a job to, don't allow
                        # the user to access the add view.
                        raise Http404(
                            "{view}: user is blocked from posting "
                            "to this company".format(view=http404_view))

        return super(PurchasedJobFormView, self).set_object(*args, **kwargs)

    @method_decorator(user_is_allowed())
    def dispatch(self, *args, **kwargs):
        """
        Determine and set which product is attempting to be purchased.

        """
        # The add url also has the pk for the product they're attempting
        # to purchase.
        if kwargs.get('product'):
            self.product = get_object_or_404(self.purchase_model,
                                             pk=kwargs.get('product'))
        else:
            obj = get_object_or_404(self.model, pk=kwargs.get('pk'))
            self.product = getattr(obj, self.purchase_field, None)

        # Set the display name based on the model
        self.display_name = self.display_name.format(product=self.product)

        return super(PurchasedJobFormView, self).dispatch(*args, **kwargs)

    def get(self, *args, **kwargs):
        company = get_company_or_404(self.request)

        if not can_modify(self.request.user, company, kwargs,
                          "update purchased job", "create purchased job"):
            return MissingActivity()

        return super(PurchasedJobFormView, self).get(*args, **kwargs)

    def post(self, *args, **kwargs):
        company = get_company_or_404(self.request)

        if not can_modify(self.request.user, company, kwargs,
                          "update purchased job", "create purchased job"):
            return MissingActivity()

        return super(PurchasedJobFormView, self).post(*args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(PurchasedJobFormView, self).get_form_kwargs()
        kwargs['product'] = self.product
        return kwargs

    def get_context_data(self, **kwargs):
        context = super(PurchasedJobFormView, self).get_context_data(**kwargs)
        context['order_type'] = 'purchasedjob'
        return context