Skip to content

Commit

Permalink
Merge branch '1.8' of github.com:nadineproject/nadine into 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
abkruse committed Jun 16, 2017
2 parents 457c6bc + 3597a8f commit 9fd6002
Show file tree
Hide file tree
Showing 24 changed files with 83 additions and 87 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Office Nomads - http://officenomads.com
Vancity Community Foundation - http://vancitycommunityfoundation.ca
Kanawha Design Studio - https://kanawha.design

Jacob Sayles - https://github.com/jsayles/
Trevor F. Smith - https://github.com/TrevorFSmith
Expand Down
5 changes: 3 additions & 2 deletions arpwatch/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.admin.views.decorators import staff_member_required
from django.http import HttpResponse, Http404, HttpResponseServerError, HttpResponseRedirect, HttpResponsePermanentRedirect
from django.utils import timezone
from django.utils.timezone import localtime, now


import arp
from arpwatch.models import UserDevice, ArpLog
Expand All @@ -33,7 +34,7 @@ class ActivityModel(object):
'''

def __init__(self):
now = timezone.localtime(timezone.now())
now = localtime(now())
midnight = now - timedelta(seconds=now.hour * 60 * 60 + now.minute * 60 + now.second) - timedelta(minutes=1)
# These are the values which are directly exposed via the ActivityModel
active_members = User.helper.active_members()
Expand Down
6 changes: 3 additions & 3 deletions arpwatch/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.contrib.auth.signals import user_logged_in
from django.db import connection
from django.db.models import Min, Max
from django.utils import timezone
from django.utils.timezone import localtime, now

from nadine.models.membership import Membership
from nadine.utils import network
Expand Down Expand Up @@ -51,7 +51,7 @@ def __str__(self):

# Signal to create a new UserRemoreAddr when people login
def register_user_ip(sender, user, request, **kwargs):
logtime = timezone.localtime(timezone.now())
logtime = localtime(now())
ip = network.get_addr(request)
if ip:
ip_log = UserRemoteAddr.objects.create(logintime=logtime, user=user, ip_address=ip)
Expand Down Expand Up @@ -93,7 +93,7 @@ def for_user(self, username, day_start, day_end):
DeviceLog = namedtuple('DeviceLog', 'start, end, diff')
logs = ArpLog.objects.filter(device__user=user, runtime__range=(day_start, day_end)).order_by('runtime')
for arp_log in logs:
local_time = timezone.localtime(arp_log.runtime)
local_time = localtime(arp_log.runtime)
key = local_time.date()
if key in device_logs:
start = device_logs[key].start
Expand Down
39 changes: 13 additions & 26 deletions arpwatch/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,48 @@
from django.contrib.auth.models import User
from django.conf import settings
from django.db import IntegrityError
from django.utils import timezone
from django.utils.timezone import localtime, now

import arp
from arpwatch.models import *


class ArpWatchTest(TestCase):

def test_user_device(self):
def test_duplicate_device(self):
MAC = "AA:AA:AA:AA:AA:AA"
device1 = UserDevice.objects.create(mac_address=MAC)
with self.assertRaises(IntegrityError):
device2 = UserDevice.objects.create(mac_address=MAC)

def test_dir_lock(self):
arp.unlock_import_dir()
self.assertFalse(arp.import_dir_locked())
arp.lock_import_dir()
self.assertTrue(arp.import_dir_locked())
arp.unlock_import_dir()
self.assertFalse(arp.import_dir_locked())

def test_log_message(self):
arp.unlock_import_dir()
arp.log_message("testing")
# self.assertFalse(arp.import_dir_locked())

# def test_arpwatch(self):
#mac1 = MACAddress.objects.create(value="90:A2:DA:00:EE:5D")
#ip1 = IPAddress.objects.create(value="127.0.0.1")
#mac2 = MACAddress.objects.create(value='AA:AA:AA:AA:AA:AA')
#ip2 = IPAddress.objects.create(value='192.168.1.1')
#mac3 = MACAddress.objects.create(value='BB:BB:BB:BB:BB:BB')
#ip3 = IPAddress.objects.create(value='172.16.5.1')

#arp_entries = []
#arp_entries.append(ArpLogEntry.objects.create(mac=mac1, ip=ip1))
#arp_entries.append(ArpLogEntry.objects.create(mac=mac2, ip=ip2))
#arp_entries.append(ArpLogEntry.objects.create(mac=mac3, ip=ip3))

#arp_log = ArpLog.objects.create()
#arp_log.entries = arp_entries;

#self.assertEqual("90:A2:DA:00:EE:5D", mac1.value)
#self.assertEqual("127.0.0.1", ip1.value)
self.assertFalse(arp.import_dir_locked())

def test_arpwatch_for_user(self):
# Register user1 with device1
user1 = User.objects.create(username='member_one', first_name='Member', last_name='One')
device1 = UserDevice.objects.create(user=user1, device_name="Device One", mac_address="90:A2:DA:00:EE:5D", ignore=False)
ip1 = "127.0.0.1"
now = timezone.now()
tz = timezone.get_current_timezone()
s = start = datetime(2015, 5, 3, 11, 0, 0, tzinfo=tz)
end = start + timedelta (hours=5)

# Create 5 hours of logs with device1
s = start = localtime(now()) - timedelta(hours=6)
end = localtime(now()) - timedelta(hours=1)
while (s < end):
ArpLog.objects.create(runtime=s, device=device1, ip_address=ip1)
s = s + timedelta(minutes=5)

# Pull the logs for user1 and make sure we have 5 hours worth
logs = ArpLog.objects.for_user(user1, start, end)
self.assertEqual(1, len(logs))
five_hours_in_seconds = 5 * 60 * 60
self.assertEqual(logs[0].diff.seconds, five_hours_in_seconds)
14 changes: 7 additions & 7 deletions arpwatch/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404, HttpResponseServerError, HttpResponseRedirect, HttpResponsePermanentRedirect
from django.contrib.admin.views.decorators import staff_member_required
from django.utils import timezone
from django.utils.timezone import localtime, now, make_aware, get_current_timezone

from arpwatch.forms import *
from arpwatch.models import *
Expand Down Expand Up @@ -59,15 +59,15 @@ def device(request, id):

@staff_member_required
def device_logs_today(request):
now = timezone.localtime(timezone.now())
return device_logs_by_day(request, str(now.year), str(now.month), str(now.day))
today = localtime(now())
return device_logs_by_day(request, str(today.year), str(today.month), str(today.day))


@staff_member_required
def device_logs_by_day(request, year, month, day):
log_date = date(year=int(year), month=int(month), day=int(day))
start = datetime(year=int(year), month=int(month), day=int(day), hour=0, minute=0, second=0, microsecond=0)
start = timezone.make_aware(start, timezone.get_current_timezone())
start = make_aware(start, get_current_timezone())
end = start + timedelta(days=1)
device_logs = ArpLog.objects.for_range(start, end)
context = {'device_logs': device_logs, 'day': log_date,
Expand All @@ -78,14 +78,14 @@ def device_logs_by_day(request, year, month, day):

@staff_member_required
def logins_today(request):
now = timezone.localtime(timezone.now())
return logins_by_day(request, str(now.year), str(now.month), str(now.day))
today = localtime(now())
return logins_by_day(request, str(today.year), str(today.month), str(today.day))


def logins_by_day(request, year, month, day):
log_date = date(year=int(year), month=int(month), day=int(day))
start = datetime(year=int(year), month=int(month), day=int(day), hour=0, minute=0, second=0, microsecond=0)
start = timezone.make_aware(start, timezone.get_current_timezone())
start = make_aware(start, get_current_timezone())
end = start + timedelta(days=1)
logs = UserRemoteAddr.objects.filter(logintime__gt=start, logintime__lt=end)
context = {'logs': logs, 'day': log_date,
Expand Down
2 changes: 1 addition & 1 deletion comlink/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from email.Utils import parseaddr

UPLOAD_TO = getattr(settings, "MAILGUN_UPLOAD_TO", "attachments/")
UPLOAD_TO = getattr(settings, "COMLINK_UPLOAD_TO", "attachments/")

logger = logging.getLogger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion comlink/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
def router(sender, **kwargs):
# Pull our email object and convert it to the mailgun_data we need`
email = kwargs['instance']
mailgun_data = email.get_mailgun_data(stripped=True, footer=True)
strip_emails = getattr(settings, "COMLINK_STRIP_EMAILS", False)
mailgun_data = email.get_mailgun_data(stripped=strip_emails, footer=True)
logger.debug("In Router: ")
logger.debug(mailgun_data)

Expand Down
2 changes: 1 addition & 1 deletion comlink/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

API_KEY = getattr(settings, "MAILGUN_API_KEY", "")

VERIFY_SIGNATURE = getattr(settings, "MAILGUN_VERIFY_INCOMING", not settings.DEBUG)
VERIFY_SIGNATURE = getattr(settings, "COMLINK_VERIFY_INCOMING", not settings.DEBUG)


##########################################################################
Expand Down
4 changes: 2 additions & 2 deletions interlink/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, mailing_list, _logger=None):

def fetch_mail(self):
"""Pops mail from the pop server and writes them as IncomingMail"""
print("Checking mail for: %s" % self.mailing_list.name)
self.logger.debug("Checking mail for: %s" % self.mailing_list.name)
pop_client = poplib.POP3_SSL(self.mailing_list.pop_host, self.mailing_list.pop_port)
try:
response = pop_client.user(self.mailing_list.username)
Expand All @@ -40,7 +40,7 @@ def fetch_mail(self):
return []

results = []
self.logger.debug("Processing %d mails" % stats[0])
self.logger.info("Processing %d %s messages" % (stats[0], self.mailing_list.name))
for i in range(stats[0]):
try:
response, mail, _size = pop_client.retr(i + 1)
Expand Down
3 changes: 2 additions & 1 deletion interlink/management/commands/process_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Command(BaseCommand):
requires_system_checks = True

def handle(self, *labels, **options):
MailingList.objects.fetch_all_mail(logger)
# MailingList.objects.fetch_all_mail(logger)
MailingList.objects.fetch_all_mail()
IncomingMail.objects.process_incoming()
OutgoingMail.objects.send_outgoing()

Expand Down
1 change: 1 addition & 0 deletions nadine/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class EmailOrUsernameModelBackend(object):

def authenticate(self, username=None, password=None):
try:
username = username.lower()
if '@' in username:
user = User.helper.by_email(username)
else:
Expand Down
2 changes: 1 addition & 1 deletion nadine/management/commands/backup_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from django.core.management.base import BaseCommand, CommandError

from staff.backup import BackupManager
from nadine.utils.backup import BackupManager


class Command(BaseCommand):
Expand Down
3 changes: 1 addition & 2 deletions nadine/management/commands/backup_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import datetime

from django.core.management.base import BaseCommand, CommandError
from staff.backup import BackupManager
from nadine.utils.backup import BackupManager

class Command(BaseCommand):
help = "Export a CSV of active members"
Expand All @@ -15,4 +15,3 @@ class Command(BaseCommand):
def handle(self, *labels, **options):
manager = BackupManager()
manager.export_active_users()

3 changes: 2 additions & 1 deletion nadine/management/commands/backup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from staff.backup import BackupManager

from nadine.utils.backup import BackupManager


class Command(BaseCommand):
Expand Down
17 changes: 9 additions & 8 deletions nadine/models/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.conf import settings
from django.utils import timezone
from django.utils.timezone import localtime, now

from nadine.models.profile import UserProfile, FileUpload
from nadine.models.membership import Membership
Expand Down Expand Up @@ -47,11 +47,11 @@ def unresolved(self, key, active_only=True):

def trigger_periodic_check(self):
# Check for exiting members in the coming week
exit_date = timezone.now() + timedelta(days=5)
exit_date = localtime(now()) + timedelta(days=5)
exiting_members = User.helper.exiting_members(exit_date)
for u in exiting_members:
# Only trigger exiting membership if no exit alerts were created in the last week
start = timezone.now() - timedelta(days=5)
start = localtime(now()) - timedelta(days=5)
if MemberAlert.objects.filter(user=u, key__in=MemberAlert.PERSISTENT_ALERTS, created_ts__gte=start).count() == 0:
self.trigger_exiting_membership(u, exit_date)

Expand All @@ -75,7 +75,7 @@ def trigger_periodic_check(self):

def trigger_exiting_membership(self, user, day=None):
if day == None:
day = timezone.now()
day = localtime(now())

new_alerts = False
open_alerts = user.profile.alerts_by_key(include_resolved=False)
Expand Down Expand Up @@ -277,14 +277,14 @@ def description(self):
return None

def resolve(self, user, note=None):
self.resolved_ts = timezone.now()
self.resolved_ts = localtime(now())
self.resolved_by = user
if note:
self.note = note
self.save()

def mute(self, user, note=None):
self.muted_ts = timezone.now()
self.muted_ts = localtime(now())
self.muted_by = user
if note:
self.note = note
Expand Down Expand Up @@ -313,8 +313,9 @@ def membership_callback(sender, **kwargs):
if created:
MemberAlert.objects.trigger_new_membership(membership.user)
else:
# If this membership is older then a week we'll go straight to exiting member logic
window_start = timezone.now() - timedelta(days=5)
# If this membership is older then a week
# we'll go straight to exiting member logic
window_start = localtime(now()) - timedelta(days=5)
if membership.end_date and membership.end_date < window_start.date():
MemberAlert.objects.trigger_exiting_membership(membership.user)
post_save.connect(membership_callback, sender=Membership)
8 changes: 3 additions & 5 deletions nadine/models/membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
from django.core.urlresolvers import reverse
from django.contrib.sites.models import Site

from monthdelta import MonthDelta, monthmod

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -188,13 +186,13 @@ def is_change(self):
def prev_billing_date(self, test_date=None):
if not test_date:
test_date = date.today()
day_difference = monthmod(self.start_date, test_date)[1]
return test_date - day_difference
difference = relativedelta(test_date, self.start_date)
return self.start_date + relativedelta(years=difference.years, months=difference.months)

def next_billing_date(self, test_date=None):
if not test_date:
test_date = date.today()
return self.prev_billing_date(test_date) + MonthDelta(1)
return self.prev_billing_date(test_date) + relativedelta(months=1)

def get_allowance(self):
if self.paid_by:
Expand Down
5 changes: 2 additions & 3 deletions nadine/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from django.core.urlresolvers import reverse
from django.contrib.sites.models import Site

from monthdelta import MonthDelta, monthmod
from taggit.managers import TaggableManager
from taggit.models import TaggedItemBase

Expand Down Expand Up @@ -155,7 +154,7 @@ def expired_slack_users(self):
return expired_users

def stale_member_date(self):
three_months_ago = timezone.now() - MonthDelta(3)
three_months_ago = timezone.now() - relativedelta(months=3)
return three_months_ago

def stale_members(self):
Expand Down Expand Up @@ -395,7 +394,7 @@ def activity_this_month(self, test_date=None):
month_start = membership.prev_billing_date(test_date)
else:
# Just go back one month from this date since there isn't a membership to work with
month_start = test_date - MonthDelta(1)
month_start = test_date - relativedelta(months=1)

activity = []
for h in [self.user] + self.guests():
Expand Down

0 comments on commit 9fd6002

Please sign in to comment.