Skip to content

Commit

Permalink
Merge branch 'development' into 'main'
Browse files Browse the repository at this point in the history
update some styles, refactor views code and generate mail_info object when invoice is sent

See merge request amartinezmm/django_invoices_generator!1
  • Loading branch information
aalexmrt committed Jun 27, 2023
2 parents 368b188 + a0f2238 commit a9da961
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 27 deletions.
23 changes: 15 additions & 8 deletions invoices/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ def __init__(self, *args, **kwargs):
self.helper.form_tag = False
self.helper.layout = Layout(
Row(
Column('sequence', css_class='form-group col-md-3 mb-0'),
Column('number', css_class='form-group col-md-3 mb-0'),
Column('issued_date', css_class='form-group col-md-3 mb-0'),
Column('customer', css_class='form-group col-md-3 mb-0'),
Column(
'sequence', css_class='form-group col-lg-3 col-md-3 col-sm-2 mb-0'),
Column(
'number', css_class='form-group col-lg-3 col-md-3 col-sm-2 mb-0'),
Column('issued_date',
css_class='form-group col-lg-3 col-md-3 col-sm-3 mb-0'),
Column(
'customer', css_class='form-group col-lg-3 col-md-3 col-sm-5 mb-0'),
css_class='form-row'
)
)
Expand All @@ -41,14 +45,17 @@ def __init__(self, *args, **kwargs):
Row(
Column('id', type="hidden", css_class="d-none"),
Column('DELETE', type="hidden", css_class="d-none"),
Column('product', css_class='form-group col-md-3 mb-0'),
Column('quantity', css_class='form-group col-md-3 mb-0'),
Column('unit_price', css_class='form-group col-md-3 mb-0'),
Column(
'product', css_class='form-group col-lg-3 col-md-3 col-sm-5 mb-0'),
Column(
'quantity', css_class='form-group col-lg-3 col-md-3 col-sm-2 mb-0'),
Column('unit_price',
css_class='form-group col-lg-3 col-md-3 col-sm-3 mb-0'),
Div(
Div(
HTML("""<label class='empty-div form-label'>&nbsp</label>
<button type="button" class="buttonDynamic">Dynamic</button>""")
), css_class="form-group col-md-1 mb-0"), css_class="formsetDynamic"
), css_class="form-group col-lg-1 col-md-1 col-sm-1 mb-0"), css_class="formsetDynamic"
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.1.5 on 2023-06-26 02:42

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('invoices', '0014_address_alias'),
]

operations = [
migrations.AlterModelOptions(
name='address',
options={'ordering': ['alias']},
),
migrations.AlterModelOptions(
name='company',
options={'ordering': ['name']},
),
]
18 changes: 18 additions & 0 deletions invoices/migrations/0016_alter_mailinfo_sent_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-06-27 04:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('invoices', '0015_alter_address_options_alter_company_options'),
]

operations = [
migrations.AlterField(
model_name='mailinfo',
name='sent_timestamp',
field=models.DateTimeField(),
),
]
18 changes: 18 additions & 0 deletions invoices/migrations/0017_alter_mailinfo_sent_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-06-27 04:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('invoices', '0016_alter_mailinfo_sent_timestamp'),
]

operations = [
migrations.AlterField(
model_name='mailinfo',
name='sent_timestamp',
field=models.DateTimeField(default=None),
),
]
18 changes: 18 additions & 0 deletions invoices/migrations/0018_alter_mailinfo_sent_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-06-27 04:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('invoices', '0017_alter_mailinfo_sent_timestamp'),
]

operations = [
migrations.AlterField(
model_name='mailinfo',
name='sent_timestamp',
field=models.DateTimeField(blank=True, default=None),
),
]
18 changes: 18 additions & 0 deletions invoices/migrations/0019_alter_mailinfo_sent_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-06-27 04:57

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('invoices', '0018_alter_mailinfo_sent_timestamp'),
]

operations = [
migrations.AlterField(
model_name='mailinfo',
name='sent_timestamp',
field=models.DateTimeField(blank=True, null=True),
),
]
18 changes: 18 additions & 0 deletions invoices/migrations/0020_alter_mailinfo_sent_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-06-27 04:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('invoices', '0019_alter_mailinfo_sent_timestamp'),
]

operations = [
migrations.AlterField(
model_name='mailinfo',
name='sent_timestamp',
field=models.DateTimeField(blank=True, default=None, null=True),
),
]
2 changes: 1 addition & 1 deletion invoices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MailInfo(models.Model):
)
status = models.CharField(
max_length=10, choices=STATUS_CHOICES, default="Pending")
sent_timestamp = models.DateTimeField(auto_now=True)
sent_timestamp = models.DateTimeField(default=None, null=True, blank=True)

def __str__(self):
return self.status
Expand Down
4 changes: 2 additions & 2 deletions invoices/templates/invoices/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
<nav class="navigation">
{% if request.path != '/' %}
<a class="p-4 link-primary" href="{% url 'index' %}">Home</a>
{% endif %}
<a class="btn btn-warning" role="button" href="{% url 'add_invoice' %}"></a>
{% endif %}
<a class="btn btn-light" role="button" href="{% url 'add_invoice' %}"></a>
</nav>
</div>
</div>
Expand Down
13 changes: 8 additions & 5 deletions invoices/templates/invoices/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<thead>
<tr>
<th scope="col" class="p-4 align-middle text-center">
<button type="submit" class="btn btn-warning" value="Submit">Send</button>

</th>
<th scope="col" class="p-4 align-middle">
Number
Expand All @@ -24,7 +24,7 @@
Client
</th>
<th scope="col" class="p-4 align-middle text-center">
File
<button type="submit" class="btn btn-dark" value="Submit">Send</button>
</th>
</tr>
</thead>
Expand All @@ -41,11 +41,14 @@
<td class="p-4 align-middle">
{{invoice.issued_date|date:"j/m/Y"}}
</td>

<td class="p-4 align-middle">
{% if invoice.mail_info.status == 'Delivered' %}
Sent
{% else %}
Pending
<span class="badge rounded-pill bg-success">Sent</span>
{% elif invoice.mail_info.status == 'Pending' %}
<span class="badge rounded-pill bg-warning">Pending</span>
{% elif invoice.mail_info.status == 'Failed' %}
<span class="badge rounded-pill bg-danger">Failed</span>
{% endif %}
</td>
<td class="p-4 align-middle">
Expand Down
4 changes: 3 additions & 1 deletion invoices/utils/send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ def config_mail_server(email_account, email_pwd):


def send_invoice_email(email_account, email_pwd, sender_name, contact_email, contact_name, cc_emails_list, files_queryset):
cc_emails_list = cc_emails_list.replace(" ", "")
if cc_emails_list is None:
cc_emails_list = ""

cc_emails_list = cc_emails_list.replace(" ", "")
msg_mixed = prepare_msg(contact_name, contact_email,
sender_name, email_account, cc_emails_list, files_queryset)
receiver_email = cc_emails_list.split(",") + [contact_email]
Expand Down
40 changes: 30 additions & 10 deletions invoices/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
from django.shortcuts import render, redirect
import environ
from datetime import datetime
from django.core.files.base import ContentFile
from invoices.models import Customer, Invoice, Issuer, GlobalSettings, MailInfo, OrderLine, Product
from django.http import HttpResponseRedirect
from invoices.forms import InvoiceForm, OrderLineFormSet
from invoices.utils.send_email import *
from django.shortcuts import render, redirect
from django.urls import reverse
from django_weasyprint.views import WeasyTemplateResponse
import environ
from invoices.forms import InvoiceForm, OrderLineFormSet
from invoices.models import (
Customer,
Invoice,
Issuer,
GlobalSettings,
MailInfo,
OrderLine,
Product,
)
from invoices.utils.send_email import send_invoice_email

env = environ.Env()
environ.Env.read_env()
env.read_env()


def index(request):
Expand All @@ -19,7 +29,7 @@ def index(request):

send_invoices(ids_invoices)

return HttpResponseRedirect('/')
return HttpResponseRedirect(reverse('index'))

invoices_list = Invoice.objects.order_by('-number')
context = {
Expand Down Expand Up @@ -50,6 +60,7 @@ def make_invoice(request, id):
invoice = invoice_form.save()
invoice.issuer = Issuer.objects.filter(
company__name='issuer').first()
invoice.mail_info = MailInfo.objects.create()
invoice.save()

if order_formset.is_valid():
Expand Down Expand Up @@ -143,11 +154,20 @@ def send_invoices(invoices_list):

for customer in customers:
invoices_queryset = Invoice.objects.filter(
id__in=invoices_list).filter(customer=customer).values_list('sequence', 'number', 'pdf_document')
id__in=invoices_list).filter(customer=customer).values_list('sequence', 'number', 'pdf_document', 'mail_info')
if not invoices_queryset:
continue

send_invoice_email(GMAIL, GMAIL_PASSWORD, global_settings.issuer.company.name,
customer.company.contact.email, customer.company.contact.name, customer.company.contact.cc_email, invoices_queryset)
if send_invoice_email(GMAIL, GMAIL_PASSWORD, global_settings.issuer.company.name,
customer.company.contact.email, customer.company.contact.name, customer.company.contact.cc_email, invoices_queryset):
for invoice in invoices_queryset:
mail_info_id = invoice[3]
MailInfo.objects.filter(
pk=mail_info_id).update(status="Delivered", sent_timestamp=datetime.now())
else:
for invoice in invoices_queryset:
mail_info_id = invoice[3]
MailInfo.objects.filter(
pk=mail_info_id).update(status="Failed")

return HttpResponseRedirect('/')

0 comments on commit a9da961

Please sign in to comment.