Skip to content

Commit ad3acd9

Browse files
committed
PEP-008 compat.
1 parent 953fb54 commit ad3acd9

15 files changed

+102
-54
lines changed

mountain/core/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
from mountain.core import registration
2-

mountain/core/admin.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
from django.contrib import admin, messages
22
from django.utils.simplejson import dumps
33

4-
from mountain.core.models import *
4+
from mountain.core.models import Computer, Company, Message
55
from mountain.core.utils import hash_types
66

7+
78
def force_resync(modeladmin, request, queryset):
89
for comp in queryset:
910
Message.objects.create(computer=comp, message=dumps({
1011
# TODO: Fix operation-id
1112
'type': 'resynchronize', 'operation-id': 1}))
12-
messages.info(request, 'Queued resyncronisation for the selected computers.')
13+
messages.info(request,
14+
'Queued resyncronisation for the selected computers.')
15+
1316

1417
def confirm_computer(modeladmin, request, queryset):
1518
for comp in queryset:
16-
plugins = comp.company.activated_plugins.values_list('identifier', flat=True).order_by('identifier')
1719
Message.objects.create(computer=comp, message=dumps({
1820
'type': 'registration-done'}))
1921
queryset.update(confirmed=True)
2022
messages.info(request, 'Queued confirmation for the selected computers.')
2123

24+
2225
def set_intervals(modelsamdin, request, queryset):
2326
for comp in queryset:
2427
Message.objects.create(computer=comp, message=dumps({
@@ -28,11 +31,13 @@ def set_intervals(modelsamdin, request, queryset):
2831
'urgent-exchange': 10}))
2932
messages.info(request, 'Queued set-intervals for the selected computers.')
3033

34+
3135
class ComputerAdmin(admin.ModelAdmin):
3236
actions = [force_resync, confirm_computer, set_intervals]
3337
exclude = ('client_accepted_types_hash',)
3438
readonly_fields = ('confirmed',)
3539

40+
3641
class CompanyAdmin(admin.ModelAdmin):
3742
exclude = ('activated_plugins_hash',)
3843

mountain/core/models.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from taggit.managers import TaggableManager
66

7+
78
class AcceptedTypes(models.Model):
89
"""This models holds the message types which are supported by this server.
910
They are selectable through the company model, so each customer can
@@ -14,14 +15,19 @@ class AcceptedTypes(models.Model):
1415
def __unicode__(self):
1516
return self.identifier
1617

18+
1719
class Company(models.Model):
1820
"""Company is used to define administrator and the activated plugins.
1921
"""
2022
verbose_name = models.CharField(_('verbose name'), max_length=255)
21-
account_name = models.CharField(_('account name'), max_length=255, unique=True)
22-
registration_password = models.CharField(_('registration password'), max_length=255)
23-
administratos = models.ManyToManyField(User, verbose_name=_('administrators'))
24-
activated_plugins = models.ManyToManyField(AcceptedTypes, verbose_name=_('activated plugins'))
23+
account_name = models.CharField(_('account name'), max_length=255,
24+
unique=True)
25+
registration_password = models.CharField(_('registration password'),
26+
max_length=255)
27+
administratos = models.ManyToManyField(User,
28+
verbose_name=_('administrators'))
29+
activated_plugins = models.ManyToManyField(AcceptedTypes,
30+
verbose_name=_('activated plugins'))
2531
activated_plugins_hash = models.CharField(max_length=255)
2632

2733
def __unicode__(self):
@@ -31,6 +37,7 @@ class Meta:
3137
verbose_name = _('Company')
3238
verbose_name_plural = _('Companies')
3339

40+
3441
class Computer(models.Model):
3542
"""Represents one Computer, information is pulled from the client
3643
registration request. Secure id is used to identify the computer,
@@ -42,7 +49,8 @@ class Computer(models.Model):
4249
hostname = models.CharField(_('hostname'), max_length=255, unique=True)
4350
# TODO: uniqness of secureid
4451
secure_id = models.TextField(_('secure id'))
45-
insecure_id = models.CharField(_('insecure id'), max_length=36, unique=True)
52+
insecure_id = models.CharField(_('insecure id'), max_length=36,
53+
unique=True)
4654
client_accepted_types_hash = models.CharField(max_length=255)
4755
confirmed = models.BooleanField(_('confirmed'), default=False)
4856

@@ -59,6 +67,7 @@ class Meta:
5967
verbose_name_plural = _('Computers')
6068
unique_together = ('company', 'computer_title')
6169

70+
6271
class Message(models.Model):
6372
"""Message which is queued and should get send to the client"""
6473
computer = models.ForeignKey(Computer, verbose_name=_('computer'))
@@ -67,5 +76,3 @@ class Message(models.Model):
6776
class Meta:
6877
verbose_name = _('Message')
6978
verbose_name_plural = _('Messages')
70-
71-

mountain/core/registration.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1-
import uuid, string
1+
import string
2+
import uuid
23
from random import choice
34

4-
from django.utils.simplejson import dumps
5-
65
from taggit.utils import parse_tags
76

8-
from mountain.core.models import Computer, Company, Message
7+
from mountain.core.models import Computer, Company
98
from mountain.core.signals import message_available
109
from mountain.core.utils import MessageType, hash_types
1110

1211
CHARS = string.ascii_letters + string.digits + string.punctuation
1312

13+
1414
def handle_registration(sender, computer, request_data, msg_data, **kwargs):
1515
try:
1616
company = Company.objects.get(account_name=msg_data['account_name'])
1717
except Company.DoesNotExist:
18-
return [{'type':'registration', 'info':'unknown-account'}]
18+
return [{'type':'registration', 'info':'unknown-account'}]
1919

2020
if msg_data['registration_password'] != company.registration_password:
2121
return [{'type':'registration', 'info':'unknown-account'}]
2222

2323
comp = Computer()
24-
comp.hostname=msg_data['hostname']
25-
comp.company=company
26-
comp.computer_title=msg_data['computer_title']
27-
comp.secure_id= ''.join([choice(CHARS) for i in range(1600)])
28-
comp.insecure_id=str(uuid.uuid4())
29-
comp.client_accepted_types_hash = hash_types(request_data['client-accepted-types']).encode('hex')
24+
comp.hostname = msg_data['hostname']
25+
comp.company = company
26+
comp.computer_title = msg_data['computer_title']
27+
comp.secure_id = ''.join([choice(CHARS) for i in range(1600)])
28+
comp.insecure_id = str(uuid.uuid4())
29+
comp.client_accepted_types_hash = \
30+
hash_types(request_data['client-accepted-types']).encode('hex')
3031
comp.save()
3132
comp.tags.set(*parse_tags(msg_data['tags']))
3233

3334
return [{'type':'set-id', 'id':comp.secure_id,
3435
'insecure-id':comp.insecure_id}]
3536

37+
3638
message_available.connect(handle_registration, sender=MessageType("register"))

mountain/core/signals.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import django.dispatch
22

3-
message_available = django.dispatch.Signal(providing_args=['computer', 'request_data', 'msg_data'])
3+
message_available = django.dispatch.Signal(providing_args=['computer',
4+
'request_data', 'msg_data'])

mountain/core/utils.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from mountain.core.settings import SERVER_UUID
88
from mountain.core.models import AcceptedTypes
99

10+
1011
def render_messages(messages, computer=None, ping_answer=False):
1112
"""Updates the answer with the server-uuid, pickles it and returns the
1213
HttpReponse.
@@ -16,17 +17,19 @@ def render_messages(messages, computer=None, ping_answer=False):
1617
ret.update({'server-uuid': SERVER_UUID})
1718
if computer:
1819
ret.update({
19-
'client-accepted-types-hash': computer.client_accepted_types_hash.decode('hex'),
20-
'next-expected-sequence': computer.next_client_sequence
21-
})
20+
'client-accepted-types-hash':
21+
computer.client_accepted_types_hash.decode('hex'),
22+
'next-expected-sequence': computer.next_client_sequence})
2223
return HttpResponse(dumps(ret))
2324

25+
2426
def MessageType(type, __instance_cache={}):
2527
"""Use this to register your receiver function to a string.
2628
"""
2729
instance = __instance_cache.setdefault(type, object())
2830
return instance
2931

32+
3033
def hash_types(types):
3134
"""The client only sends the hashed server types, we do the same,
3235
compare them and only send new types if the types difer.
@@ -35,15 +38,15 @@ def hash_types(types):
3538
m.update(";".join(types))
3639
return m.digest()
3740

41+
3842
def register_messagetype(type):
3943
from django.db.models.signals import post_syncdb
4044
from mountain.core import models
4145

4246
def install_type(sender, app, created_models, verbosity=0, **kwargs):
43-
if verbosity>=1:
47+
if verbosity >= 1:
4448
obj, created = AcceptedTypes.objects.get_or_create(identifier=type)
4549
if created:
4650
print "Installed message type %s" % type
4751

4852
post_syncdb.connect(install_type, sender=models, weak=False)
49-

mountain/core/views.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
message_logger = logging.getLogger('mountain.messaging')
1919

20+
2021
@csrf_exempt
2122
def ping(request):
2223
id = request.POST.get('insecure_id')
2324
msg_count = Message.objects.filter(computer__insecure_id=id).count()
2425
return render_messages(bool(msg_count), ping_answer=True)
2526

27+
2628
@csrf_exempt
2729
@transaction.commit_on_success
2830
def message_system(request):
@@ -42,7 +44,8 @@ def message_system(request):
4244
computer = Computer.objects.select_related('company')\
4345
.get(secure_id=secure_id)
4446
company = computer.company
45-
if not all((computer.next_client_sequence, computer.next_server_sequence)):
47+
if not all((computer.next_client_sequence,
48+
computer.next_server_sequence)):
4649
computer.next_client_sequence = data['sequence']
4750
computer.next_server_sequence = data['next-expected-sequence']
4851
computer.save()
@@ -52,13 +55,13 @@ def message_system(request):
5255
# Special case registration, as we could get a request with nothing,
5356
# and without accepting register, we'll never get a registration.
5457
if computer is not None and computer.confirmed:
55-
accepted_types = company.activated_plugins.values_list('identifier', flat=True).order_by('identifier')
58+
accepted_types = company.activated_plugins.values_list('identifier',
59+
flat=True).order_by('identifier')
5660
accepted_types_hash = company.activated_plugins_hash.decode('hex')
5761
else:
5862
accepted_types = ['register']
5963
accepted_types_hash = hash_types(['register'])
6064

61-
6265
# Check if sequence numbers match
6366
if computer is not None and computer.confirmed and \
6467
((data['sequence'] != computer.next_client_sequence) or
@@ -67,7 +70,8 @@ def message_system(request):
6770

6871
# Determine whether we need to notify the client about new/delete types
6972
if data.get('accepted-types') != accepted_types_hash:
70-
return_msgs.append({'type':'accepted-types', 'types':list(accepted_types)})
73+
return_msgs.append({'type': 'accepted-types',
74+
'types': list(accepted_types)})
7175

7276
for msg in received_msgs:
7377
if computer is None and msg['type'] != 'register':
@@ -95,7 +99,6 @@ def message_system(request):
9599
next_client_sequence = \
96100
F('next_client_sequence') + data['total-messages'],
97101
next_server_sequence = \
98-
F('next_server_sequence') + len(return_msgs)
99-
)
102+
F('next_server_sequence') + len(return_msgs))
100103

101104
return render_messages(return_msgs, computer=computer)

mountain/monitor/admin.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
from django.contrib import admin
22

3-
from mountain.monitor.models import ComputerInfo#, ProcessorInfo, HardwarePart
3+
from mountain.monitor.models import ComputerInfo
4+
#, ProcessorInfo, HardwarePart
5+
46

57
class ProcessorInfoAdmin(admin.ModelAdmin):
6-
list_display = ('model', 'vendor', 'processor_id', 'cache_size', 'computer')
8+
list_display = ('model', 'vendor', 'processor_id',
9+
'cache_size', 'computer')
710
list_filter = ('computer',)
811

12+
913
class HardwarePartAdmin(admin.ModelAdmin):
1014
list_display = ('product', 'vendor', 'computer')
1115
list_filter = ('computer',)
1216

17+
1318
class ComputerInfoAdmin(admin.ModelAdmin):
14-
list_display = ('hostname', 'code_name', 'description', 'distributor_id', 'release', 'computer')
19+
list_display = ('hostname', 'code_name', 'description',
20+
'distributor_id', 'release', 'computer')
21+
1522

1623
#admin.site.register(ProcessorInfo, ProcessorInfoAdmin)
1724
#admin.site.register(HardwarePart, HardwarePartAdmin)

mountain/monitor/computerinfo.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
from mountain.core.signals import message_available
2-
from mountain.core.utils import MessageType, register_messagetype
2+
from mountain.core.utils import MessageType
33
from mountain.monitor.models import ComputerInfo
44

5+
56
def handle_computer_info(sender, computer, request_data, msg_data, **kwargs):
67
comp_info, created = ComputerInfo.objects.get_or_create(computer=computer)
78
for i in ['total-memory', 'total-swap', 'hostname']:
8-
setattr(comp_info, i.replace('-','_'), msg_data[i])
9+
setattr(comp_info, i.replace('-', '_'), msg_data[i])
910
computer.hostname = msg_data['hostname']
1011
comp_info.save()
1112
return []
1213

14+
1315
def handle_distribution_info(sender, computer, request_data, msg_data, **kwargs):
1416
dist_info, created = ComputerInfo.objects.get_or_create(computer=computer)
15-
for i in ['code-name', 'description', 'distributor-id', 'release'] :
16-
setattr(dist_info, i.replace('-','_'), msg_data[i])
17+
for i in ['code-name', 'description', 'distributor-id', 'release']:
18+
setattr(dist_info, i.replace('-', '_'), msg_data[i])
1719
dist_info.save()
1820
return []
1921

20-
message_available.connect(handle_computer_info, sender=MessageType('computer-info'))
21-
message_available.connect(handle_distribution_info, sender=MessageType('distribution-info'))
22+
23+
message_available.connect(handle_computer_info,
24+
sender=MessageType('computer-info'))
25+
message_available.connect(handle_distribution_info,
26+
sender=MessageType('distribution-info'))

mountain/monitor/models.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.db import models
22

33
from mountain.core.models import Computer
4-
4+
#
55
#class ProcessorInfo(models.Model):
66
# computer = models.ForeignKey(Computer)
77
# processor_id = models.SmallIntegerField()
@@ -24,6 +24,7 @@
2424
# def __unicode__(self):
2525
# return self.product
2626

27+
2728
class ComputerInfo(models.Model):
2829
computer = models.ForeignKey(Computer)
2930
hostname = models.CharField(max_length=255)

mountain/monitor/processorinfo.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
from mountain.core.utils import MessageType
33
from mountain.monitor.models import ProcessorInfo
44

5+
56
def handle_proc_info(sender, computer, request_data, msg_data, **kwargs):
67
ProcessorInfo.objects.filter(computer=computer).delete()
78
for p in msg_data['processors']:
8-
p_info = ProcessorInfo(computer=computer, processor_id=p['processor-id'],
9+
p_info = ProcessorInfo(computer=computer,
10+
processor_id=p['processor-id'],
911
model=p['model'])
1012
for i in ['cache-size', 'vendor']:
1113
if p.get(i):
12-
setattr(p_info, i.replace('-','_'), p.get(i))
14+
setattr(p_info, i.replace('-', '_'), p.get(i))
1315

1416
p_info.save()
1517

1618
return []
1719

18-
message_available.connect(handle_proc_info, sender=MessageType('processor-info'))
20+
21+
message_available.connect(handle_proc_info,
22+
sender=MessageType('processor-info'))

mountain/packages/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
(3, 'locked'),
2323
)
2424

25+
2526
class PackageManager(models.Manager):
2627
def hashes_in_bulk(self, id_list):
2728
qs = self.model.objects.filter(hash__in=id_list)

0 commit comments

Comments
 (0)