Esempio n. 1
0
 def send_acceptation(self, host):
     subject = _("Invitation to join %(name)s accepted by  %(full_name)s" %
                 {
                     'name': self.company.name,
                     'full_name': self.guest.get_full_name()
                 })
     message_template = fo_get_template(
         host, 'enterprise/emails/acceptation.txt', True)
     message_context = Context({
         'invited':
         self.get_full_name(),
         'guest':
         self.guest.get_full_name(),
         'company':
         self.company.name,
         'revocation_url':
         "%s%s" % (host,
                   reverse('revoke_invitation',
                           kwargs={'token': self.revocation_token})),
         'EMAIL_BASE_TEMPLATE':
         select_template(fo_get_template(host,
                                         settings.EMAIL_BASE_TEMPLATE)),
         'ADDRESS_TEMPLATE':
         select_template(fo_get_template(host, settings.COMPANY_ADDRESS)),
     })
     message = message_template.render(message_context)
     send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
               [self.guest.email])
Esempio n. 2
0
 def send_acceptation(self, host):
     subject = _("Invitation to join %(name)s accepted by  %(full_name)s" %{'name':self.company.name,
                                                                            'full_name':self.guest.get_full_name()})
     message_template = fo_get_template(host,'enterprise/emails/acceptation.txt', True)
     message_context = Context({'invited': self.get_full_name(),
                                'guest': self.guest.get_full_name(),
                                'company': self.company.name,
                                'revocation_url':"%s%s" %(host, reverse('revoke_invitation',
                                                                      kwargs={'token':self.revocation_token})),
                                'EMAIL_BASE_TEMPLATE':select_template(fo_get_template(host, settings.EMAIL_BASE_TEMPLATE)),
                                'ADDRESS_TEMPLATE':select_template(fo_get_template(host, settings.COMPANY_ADDRESS)),
                               })
     message = message_template.render(message_context)
     send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.guest.email])
Esempio n. 3
0
def change_company(request, customer_id):
    # FIXME: Make this view honor the returned_url if any for other apps that
    # will call it
    customer = get_object_or_404(Clients, pk=customer_id)
    return_url = request.GET.get('return_url', None)
    form = EnterpriseForm(request.POST or None, instance=customer)
    if form.is_valid():
        customer = form.save(commit=False)
        customer.id_user = Users.objects.get(email=request.user.email)
        with reversion.create_revision():
            customer.save()
            reversion.set_user(request.user)
            reversion.set_comment("Company altered from %s" %
                                  request.META.get('REMOTE_ADDR', None))
        messages.add_message(request, messages.INFO, _('Company info saved'))
        if return_url:
            return redirect(return_url)

        return redirect('home')
    return render(
        request,
        fo_get_template(request.get_host(), 'enterprise/add_company.html'), {
            'form': form,
            'title': _("Change company")
        })
Esempio n. 4
0
def add_company(request):
    # FIXME: Make this view honor the returned_url if any for other apps that
    # will call it
    return_url = request.GET.get('return_url', None)

    form = EnterpriseForm(request.POST or None)
    if form.is_valid():
        customer = form.save(commit=False)
        try:
            customer.id_user = Users.objects.get(login=request.user.email)
        except Users.DoesNotExist:
            try:
                # FIXME: Remove these and do it just when the connection is first made
                user = Users.objects.create(email=request.user.email, login=request.user.email)
                customer.id_user = user
            except:
                # The login is not the email ('admin'), if this fail then crash.
                customer.id_user = Users.objects.get(email=request.user.email)            
        #Cyril wants this to be always 1
        customer.company_type = CompanyTypes.objects.get(pk=1)
        with reversion.create_revision():
            customer.save()
            reversion.set_user(request.user)
            reversion.set_comment("Company added from %s" % request.META.get('REMOTE_ADDR', None))
        manager = Roles.objects.get(name='manager')
        Clients2Users.objects.create(user=customer.id_user, client=customer, role=manager)

        if return_url:
            return redirect(return_url)
        return redirect('home')
    return render(request, fo_get_template(request.get_host(), 'enterprise/add_company.html'), {'form':form, 'title':_("Add company")})
Esempio n. 5
0
def list_invoices(request, customer_id, message=None):
    current_user = Users.objects.get(email=request.user.email)
    customer = get_object_or_404(current_user.clients_set, pk=customer_id)
    return render(request, fo_get_template(request.get_host(),'invoice/list_invoices.html'),
                              {'invoice_list': customer.invoices_set.filter(type_doc='facture', paid=False),
                               'quote_list': customer.invoices_set.filter(type_doc='devis'),
                               'subscription_list': customer.subscription_set.filter(type_doc='invoice'),
                               'subscrptionquote_list': customer.subscription_set.filter(type_doc='quote'),
                               'company': customer,
                               'message':message})
Esempio n. 6
0
def detail_subscription(request, subscription_id):
    current_user = Users.objects.get(email=request.user.email)
    subscriptions = [c.subscription_set.all() for c in current_user.clients_set.all()]
    if not subscriptions:
        raise Http404
    qs  = reduce(operator.or_, subscriptions)
    subscription = get_object_or_404(qs, pk=subscription_id)

    return render(request, fo_get_template(request.get_host(),'invoice/detail_subscriptions.html'),
                              {'subscription':subscription,
                               'subscription_details': subscription.subscriptionrow_set.order_by('id')})
Esempio n. 7
0
def detail_invoice(request, invoice_id):
    current_user = Users.objects.get(email=request.user.email)
    invoices = [c.invoices_set.all() for c in current_user.clients_set.all()]
    if not invoices:
        raise Http404
    qs  = reduce(operator.or_, invoices)
    invoice = get_object_or_404(qs, pk=invoice_id)
    
    return render(request, fo_get_template(request.get_host(),'invoice/detail_invoices.html'),
                              {'invoice':invoice,
                               'invoice_details': invoice.invoicerows_set.order_by('order')})
Esempio n. 8
0
def list_companies(request):
    try:
        current_user = Users.objects.get(email=request.user.email)
        # We keep a loose coupling with native Users database from the backend
        customer_list = current_user.clients_set.all()
        
    except Users.DoesNotExist:
        customer_list = None

    return render(request, fo_get_template(request.get_host(),'invoice/list_companies.html'),
                              {'customer_list': customer_list})
Esempio n. 9
0
def hipay_payment_url(request, invoice_id, internal_transid, action, payment_type):
    """URL to redirect the client on canceled payment by the customer"""
    if payment_type == 'invoice':
        c_object = get_object_or_404(Invoices, pk=invoice_id)
        tr = c_object.invoicetransaction_set.get(pk=internal_transid)
    else:
        c_object = get_object_or_404(Subscription, pk=invoice_id)
        tr = c_object.subscriptiontransaction_set.get(pk=internal_transid)
    tr.first_status = action
    tr.save()
    return render(request, fo_get_template(request.get_host(),'invoice/hipay/%s_payment.html'%(action,)), {'payment_type':payment_type,'invoice':c_object})
Esempio n. 10
0
def revoke_invitations(request):
    try:
        current_user = Users.objects.get(email=request.user.email)
    except Users.DoesNotExist:
        current_user = Users.objects.create(email=request.user.email, login=request.user.email)

    target_roles = Roles.objects.get(name='manager')
    # 1/ Search all the companies where the current user is a Manager
    # 2/ Show all the invitations concerning those companies (revocable)
    managed_compagnies = Clients.objects.filter(clients2users__role=target_roles, clients2users__user=current_user)
    invitations = [c.invitation_set.all() for c in managed_compagnies]
    invitations.append(current_user.invitation_set.all())
    if invitations:
        qs = reduce(operator.or_, invitations)
    else:
        qs = Invitation.objects.none()

    return render(request, fo_get_template(request.get_host(),'enterprise/revoke_invitations.html'), {'invitations':qs.filter(revoked=False)})
Esempio n. 11
0
def change_company(request, customer_id):
    # FIXME: Make this view honor the returned_url if any for other apps that
    # will call it
    customer = get_object_or_404(Clients, pk=customer_id)
    return_url = request.GET.get('return_url', None)
    form = EnterpriseForm(request.POST or None, instance=customer)
    if form.is_valid():
        customer = form.save(commit=False)
        customer.id_user = Users.objects.get(email=request.user.email)
        with reversion.create_revision():
            customer.save()
            reversion.set_user(request.user)
            reversion.set_comment("Company altered from %s" % request.META.get('REMOTE_ADDR', None))
        messages.add_message(request, messages.INFO, _('Company info saved'))
        if return_url:
            return redirect(return_url)

        return redirect('home')
    return render(request, fo_get_template(request.get_host(),'enterprise/add_company.html'), {'form':form, 'title':_("Change company")})
Esempio n. 12
0
def add_company(request):
    # FIXME: Make this view honor the returned_url if any for other apps that
    # will call it
    return_url = request.GET.get('return_url', None)

    form = EnterpriseForm(request.POST or None)
    if form.is_valid():
        customer = form.save(commit=False)
        try:
            customer.id_user = Users.objects.get(login=request.user.email)
        except Users.DoesNotExist:
            try:
                # FIXME: Remove these and do it just when the connection is first made
                user = Users.objects.create(email=request.user.email,
                                            login=request.user.email)
                customer.id_user = user
            except:
                # The login is not the email ('admin'), if this fail then crash.
                customer.id_user = Users.objects.get(email=request.user.email)
        #Cyril wants this to be always 1
        customer.company_type = CompanyTypes.objects.get(pk=1)
        with reversion.create_revision():
            customer.save()
            reversion.set_user(request.user)
            reversion.set_comment("Company added from %s" %
                                  request.META.get('REMOTE_ADDR', None))
        manager = Roles.objects.get(name='manager')
        Clients2Users.objects.create(user=customer.id_user,
                                     client=customer,
                                     role=manager)

        if return_url:
            return redirect(return_url)
        return redirect('home')
    return render(
        request,
        fo_get_template(request.get_host(), 'enterprise/add_company.html'), {
            'form': form,
            'title': _("Add company")
        })
Esempio n. 13
0
def invite_user(request):
    current_user = Users.objects.get(email=request.user.email)
    qs = current_user.clients_set.all()
    if not qs:
        raise Http404
    
    form = InvitationForm(request.POST or None, qs=qs, initial={'company': qs.order_by('pk')[0] })
    if form.is_valid():
        # Check if the user is not already in
        company = form.cleaned_data['company']
        email = form.cleaned_data['email']
        try:
            invited = Users.objects.get(email=email)
        except Users.DoesNotExist:
            invited=None

        try:
            Invitation.objects.get(email=email, company=company, guest=current_user, revoked=False)
            messages.add_message(request, messages.INFO, _("An invitation to %(email)s is already pending acceptation for %(company)s, resend the invitation if you think the previous one is lost" %{'company':company, 'email':email}))
            return redirect('revoke_invitations')
        except Invitation.DoesNotExist:
            pass

        try:
            Clients2Users.objects.get(user=invited, client=company)
        except Clients2Users.DoesNotExist:
            invitation = form.save(commit=False)
            invitation.guest=current_user
            invitation.save()
            base_host = "http%s://%s" %('s' if request.is_secure() else '',
                                        request.get_host())
            invitation.send_invitation(base_host)
            messages.add_message(request, messages.INFO, _("An email have been sent to the user, you'll be notified once the invitation is accepted"))
            return redirect('revoke_invitations')
        else:
            logger.warn(u"The user %s is already allowed to manage the company %s"%(invited.email, company))
            messages.add_message(request, messages.ERROR, _("The user %s is already allowed to manage the company %s" %(invited,company)))
    return render(request, fo_get_template(request.get_host(),'enterprise/invite_user.html'), {'form':form, 'title':_("Invite a user")})
Esempio n. 14
0
def revoke_invitations(request):
    try:
        current_user = Users.objects.get(email=request.user.email)
    except Users.DoesNotExist:
        current_user = Users.objects.create(email=request.user.email,
                                            login=request.user.email)

    target_roles = Roles.objects.get(name='manager')
    # 1/ Search all the companies where the current user is a Manager
    # 2/ Show all the invitations concerning those companies (revocable)
    managed_compagnies = Clients.objects.filter(
        clients2users__role=target_roles, clients2users__user=current_user)
    invitations = [c.invitation_set.all() for c in managed_compagnies]
    invitations.append(current_user.invitation_set.all())
    if invitations:
        qs = reduce(operator.or_, invitations)
    else:
        qs = Invitation.objects.none()

    return render(
        request,
        fo_get_template(request.get_host(),
                        'enterprise/revoke_invitations.html'),
        {'invitations': qs.filter(revoked=False)})
Esempio n. 15
0
def invite_user(request):
    current_user = Users.objects.get(email=request.user.email)
    qs = current_user.clients_set.all()
    if not qs:
        raise Http404

    form = InvitationForm(request.POST or None,
                          qs=qs,
                          initial={'company': qs.order_by('pk')[0]})
    if form.is_valid():
        # Check if the user is not already in
        company = form.cleaned_data['company']
        email = form.cleaned_data['email']
        try:
            invited = Users.objects.get(email=email)
        except Users.DoesNotExist:
            invited = None

        try:
            Invitation.objects.get(email=email,
                                   company=company,
                                   guest=current_user,
                                   revoked=False)
            messages.add_message(
                request, messages.INFO,
                _("An invitation to %(email)s is already pending acceptation for %(company)s, resend the invitation if you think the previous one is lost"
                  % {
                      'company': company,
                      'email': email
                  }))
            return redirect('revoke_invitations')
        except Invitation.DoesNotExist:
            pass

        try:
            Clients2Users.objects.get(user=invited, client=company)
        except Clients2Users.DoesNotExist:
            invitation = form.save(commit=False)
            invitation.guest = current_user
            invitation.save()
            base_host = "http%s://%s" % ('s' if request.is_secure() else '',
                                         request.get_host())
            invitation.send_invitation(base_host)
            messages.add_message(
                request, messages.INFO,
                _("An email have been sent to the user, you'll be notified once the invitation is accepted"
                  ))
            return redirect('revoke_invitations')
        else:
            logger.warn(
                u"The user %s is already allowed to manage the company %s" %
                (invited.email, company))
            messages.add_message(
                request, messages.ERROR,
                _("The user %s is already allowed to manage the company %s" %
                  (invited, company)))
    return render(
        request,
        fo_get_template(request.get_host(), 'enterprise/invite_user.html'), {
            'form': form,
            'title': _("Invite a user")
        })