# Path: holodeck/utils.py
# def get_widget_type_choices():
#     """
#     Generates Django model field choices based on widgets
#     in holodeck.widgets.
#     """
#     choices = []
#     for name, member in inspect.getmembers(widgets, inspect.isclass):
#         if member != widgets.Widget:
#             choices.append((
#                 "%s.%s" % (member.__module__, member.__name__),
#                 member.name
#             ))
#     return choices
# 
# def load_class_by_string(class_path):
#     """
#     Returns a class when given its full name in
#     Python dot notation, including modules.
#     """
#     parts = class_path.split('.')
#     module_name = '.'.join(parts[:-1])
#     class_name = parts[-1]
#     __import__(module_name)
#     mod = sys.modules[module_name]
#     return getattr(mod, class_name)


import uuid
import xlwt
from django.contrib.auth.models import User
from django.db import models
from holodeck.utils import get_widget_type_choices, load_class_by_string


class Dashboard(models.Model):
    name = models.CharField(max_length=255)
    owner = models.ForeignKey(User, null=True)
    share_key = models.CharField(
        max_length=32,
        unique=True,
        blank=True,
        null=True
    )

    def save(self, *args, **kwargs):
        if not self.share_key:
            self.share_key = generate_key()
        super(Dashboard, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.name


class Metric(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField(
        blank=True,
        null=True,
    )
    dashboard = models.ForeignKey('holodeck.Dashboard')
    widget_type = models.CharField(
        max_length=64,

# Path: holodeck/utils.py
# def get_widget_type_choices():
#     """
#     Generates Django model field choices based on widgets
#     in holodeck.widgets.
#     """
#     choices = []
#     for name, member in inspect.getmembers(widgets, inspect.isclass):
#         if member != widgets.Widget:
#             choices.append((
#                 "%s.%s" % (member.__module__, member.__name__),
#                 member.name
#             ))
#     return choices
# 
# def load_class_by_string(class_path):
#     """
#     Returns a class when given its full name in
#     Python dot notation, including modules.
#     """
#     parts = class_path.split('.')
#     module_name = '.'.join(parts[:-1])
#     class_name = parts[-1]
#     __import__(module_name)
#     mod = sys.modules[module_name]
#     return getattr(mod, class_name)


# Path: holodeck/models.py
import uuid
import xlwt
from django.contrib.auth.models import User
from django.db import models
from holodeck.utils import get_widget_type_choices, load_class_by_string



class Dashboard(models.Model):
    name = models.CharField(max_length=255)
    owner = models.ForeignKey(User, null=True)
    share_key = models.CharField(
        max_length=32,
        unique=True,
        blank=True,
        null=True
    )

    def save(self, *args, **kwargs):
        if not self.share_key:
            self.share_key = generate_key()
        super(Dashboard, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.name


class Metric(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField(
        blank=True,
        null=True,
    )
    dashboard = models.ForeignKey('holodeck.Dashboard')
    widget_type = models.CharField(
        max_length=64,

        choices=get_widget_type_choices()