aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2015-11-16 11:48:16 +0200
committerFilipp Lepalaan <filipp@mac.com>2015-11-16 11:48:16 +0200
commitae51ca63fc46ae2375c6966d0ab24d5213f36315 (patch)
treee2f3a39ed31fc4ad50c55eaa0ad824b34bbe0c21
parentb36cd5c50493ca3567c5acfef665352bd39eadd3 (diff)
downloadServo-ae51ca63fc46ae2375c6966d0ab24d5213f36315.tar.gz
Servo-ae51ca63fc46ae2375c6966d0ab24d5213f36315.tar.bz2
Servo-ae51ca63fc46ae2375c6966d0ab24d5213f36315.zip
Fixed periodic checks with custom email address
-rw-r--r--servo/exceptions.py8
-rwxr-xr-xservo/management/commands/cron.py72
-rw-r--r--servo/models/common.py19
3 files changed, 54 insertions, 45 deletions
diff --git a/servo/exceptions.py b/servo/exceptions.py
index 2cde43d..f4910ba 100644
--- a/servo/exceptions.py
+++ b/servo/exceptions.py
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
-class ServoException(Exception):
- """docstring for ServoException"""
+
+class ConfigurationError(Exception):
def __init__(self, arg):
- super(ServoException, self).__init__()
- self.arg = arg
- \ No newline at end of file
+ super(ConfigurationError, self).__init__(arg)
diff --git a/servo/management/commands/cron.py b/servo/management/commands/cron.py
index 5255104..d4e6b89 100755
--- a/servo/management/commands/cron.py
+++ b/servo/management/commands/cron.py
@@ -5,6 +5,7 @@ from datetime import date, timedelta
from django.conf import settings
from django.core.files import File
+from django.core.mail import send_mail
from django.utils.translation import ugettext as _
from django.core.management.base import BaseCommand
@@ -18,12 +19,14 @@ from servo.models import Inventory, Order, PurchaseOrder, User
def send_table(sender, recipient, subject, table, send_empty=False):
- from django.core.mail import send_mail
- if not send_empty and not table.has_body():
+ if send_empty is False and table.has_body() is False:
return
config = Configuration.conf()
- settings.EMAIL_HOST = config.get('smtp_host')
+ host, port = Configuration.get_smtp_server()
+
+ settings.EMAIL_HOST = host
+ settings.EMAIL_PORT = int(port)
settings.EMAIL_USE_TLS = config.get('smtp_ssl')
settings.EMAIL_HOST_USER = config.get('smtp_user')
settings.EMAIL_HOST_PASSWORD = config.get('smtp_password')
@@ -34,13 +37,17 @@ def send_table(sender, recipient, subject, table, send_empty=False):
class Command(BaseCommand):
help = "Runs Servo's periodic commands"
- def update_invoices(self):
+ def __init__(self, *args, **kwargs):
+ super(Command, self).__init__(*args, **kwargs)
uid = Configuration.conf('imap_act')
- user = User.objects.get(pk=uid)
+ self.mail_user = User.objects.get(pk=uid)
+ self.mail_recipient = Configuration.notify_email_address()
+ self.mail_sender = Configuration.get_default_sender(self.mail_user)
+ def update_invoices(self):
for a in GsxAccount.objects.all():
try:
- a.default(user)
+ a.default(self.mail_user)
lookup = gsxws.Lookup(shipTo=a.ship_to, invoiceDate=date.today())
invoices = lookup.invoices()
for i in invoices:
@@ -59,47 +66,36 @@ class Command(BaseCommand):
"""
Reports on cases that have been red for a day
"""
- conf = Configuration.conf()
-
- try:
- sender = conf['default_sender']
- except KeyError:
- raise ValueError('Default sender address not defined')
-
now = timezone.now()
limit = now - timedelta(days=1)
for l in Location.objects.filter(enabled=True):
table = CsvTable()
- table.addheader(['Order', 'Assigned To', 'Status', 'Days red'])
+ subject = _(u"Repairs aging beyond limits at %s") % l.title
+ table.addheader(['ORDER', 'ASSIGNED_TO', 'STATUS', 'DAYS_RED'])
# "Aging" repairs are ones that have been red for at least a day
- orders = Order.objects.filter(
- location=l,
- state__lt=Order.STATE_CLOSED,
- status_limit_yellow__lt=limit
- )
+ orders = Order.objects.filter(location=l,
+ state__lt=Order.STATE_CLOSED,
+ status_limit_yellow__lt=limit)
for o in orders:
- username = o.get_user_name() or _("Unassigned")
+ username = o.get_user_name() or _("Nobody")
status_title = o.get_status_name() or _("No Status")
days = (now - o.status_limit_yellow).days
table.addrow([o.code, username, status_title, days])
- subject = _(u"Repairs aging beyond limits at %s") % l.title
-
if Configuration.notify_location():
- send_table(sender, l.email, subject, table)
- if Configuration.notify_email_address():
- send_table(sender, conf['notify_address'], subject, table)
+ recipient = l.manager.email if l.manager else l.email
+ send_table(self.mail_sender, recipient, subject, table)
+ if self.mail_recipient:
+ send_table(self.mail_sender, self.mail_recipient, subject, table)
def notify_stock_limits(self):
- conf = Configuration.conf()
-
- try:
- sender = conf['default_sender']
- except KeyError:
- raise ValueError('Default sender address not defined')
+ """
+ Notifies the correct parties of inventory items stocking status
+ """
+ subject = _(u"Products stocked below limit")
for l in Location.objects.filter(enabled=True):
out_of_stock = Inventory.objects.filter(
@@ -108,18 +104,16 @@ class Command(BaseCommand):
)
table = CsvTable()
- table.addheader(['Product', 'Minimum', 'Stocked'])
+ table.addheader(['PRODUCT', 'MINIMUM', 'STOCKED'])
for i in out_of_stock:
table.addrow([i.product.code, i.amount_minimum, i.amount_stocked])
- subject = _(u"Products stocked below limit")
-
if Configuration.notify_location():
- email = l.manager.email if l.manager else l.email
- send_table(sender, email, subject, table)
- if Configuration.notify_email_address():
- send_table(sender, conf['notify_address'], subject, table)
+ recipient = l.manager.email if l.manager else l.email
+ send_table(self.mail_sender, recipient, subject, table)
+ if self.mail_recipient:
+ send_table(self.mail_sender, self.mail_recipient, subject, table)
def update_counts(self):
now = timezone.now()
@@ -134,5 +128,5 @@ class Command(BaseCommand):
def handle(self, *args, **options):
#self.update_invoices()
self.update_counts()
- self.notify_aging_repairs()
+ #self.notify_aging_repairs()
self.notify_stock_limits()
diff --git a/servo/models/common.py b/servo/models/common.py
index 03942db..2ab81f0 100644
--- a/servo/models/common.py
+++ b/servo/models/common.py
@@ -22,12 +22,14 @@ from django.utils.translation import ugettext_lazy as _
from django.dispatch import receiver
from django.db.models.signals import post_save
-from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
+from django.contrib.contenttypes.fields import GenericForeignKey
from django.core.cache import cache
from servo import defaults
+from servo.lib.utils import empty
+from servo.exceptions import ConfigurationError
from servo.validators import file_upload_validator
@@ -522,6 +524,9 @@ class Configuration(models.Model):
@classmethod
def get_default_sender(cls, user):
+ """
+ Returns the sender address to use for notifications
+ """
conf = cls.conf()
sender = conf.get('default_sender')
@@ -562,6 +567,18 @@ class Configuration(models.Model):
return cls.true('smtp_ssl')
@classmethod
+ def get_smtp_server(cls):
+ host, port = cls.conf('smtp_host'), 25
+
+ if empty(host):
+ raise ConfigurationError('SMTP server not configured')
+
+ if len(host.split(':')) == 2:
+ return host.split(':')
+
+ return host, port
+
+ @classmethod
def get_imap_server(cls):
import imaplib
conf = cls.conf()