Skip to content

Commit

Permalink
Merge pull request #17 from jamalex/instance_id_fix
Browse files Browse the repository at this point in the history
Make platform detection more robust, and add optional system_id input
  • Loading branch information
jamalex authored Jun 30, 2017
2 parents ca9e5b5 + aa1e5a0 commit ef260d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 20 additions & 0 deletions morango/migrations/0006_instanceidmodel_system_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2017-06-30 00:15
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('morango', '0005_auto_20170629_2139'),
]

operations = [
migrations.AddField(
model_name='instanceidmodel',
name='system_id',
field=models.CharField(blank=True, max_length=100),
),
]
12 changes: 10 additions & 2 deletions morango/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class InstanceIDModel(UUIDModelMixin):
as well as its counter with all the records that were serialized at the time.
"""

uuid_input_fields = ("platform", "hostname", "sysversion", "macaddress", "database_id", "db_path")
uuid_input_fields = ("platform", "hostname", "sysversion", "macaddress", "database_id", "db_path", "system_id")

platform = models.TextField()
hostname = models.TextField()
Expand All @@ -69,18 +69,26 @@ class InstanceIDModel(UUIDModelMixin):
counter = models.IntegerField(default=0)
current = models.BooleanField(default=True)
db_path = models.CharField(max_length=1000)
system_id = models.CharField(max_length=100, blank=True)

@staticmethod
def get_or_create_current_instance():
"""Get the instance model corresponding to the current system, or create a new
one if the system is new or its properties have changed (e.g. OS from upgrade)."""

# on Android, platform.platform() barfs, so we handle that safely here
try:
plat = platform.platform()
except:
plat = "Unknown (Android?)"

kwargs = {
"platform": platform.platform(),
"platform": plat,
"hostname": platform.node(),
"sysversion": sys.version,
"database": DatabaseIDModel.objects.get(current=True),
"db_path": os.path.abspath(settings.DATABASES['default']['NAME']),
"system_id": os.environ.get("MORANGO_SYSTEM_ID", ""),
}

# try to get the MAC address, but exclude it if it was a fake (random) address
Expand Down

0 comments on commit ef260d2

Please sign in to comment.