Created
January 23, 2017 21:31
-
-
Save dariushazimi/e00375174f99dd763bf2012e00a35e92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RELATIONSHIP_DOWNSTREAM = 1 | |
RELATIONSHIP_UPSTREAM = 2 | |
RELATIONSHIP_STATUSES = ( | |
(RELATIONSHIP_DOWNSTREAM, 'DownStream'), | |
(RELATIONSHIP_UPSTREAM, 'UpStream'), | |
) | |
class Application(TimeStampModel): | |
name = models.CharField(verbose_name='CI Name', max_length=100, unique=True) | |
description = models.TextField(blank=True, help_text="Business purpose of the application") | |
hosted = models.CharField(max_length=50, choices=constants.HOSTING_TYPE.items(), default='Internally') | |
application_type = models.CharField(max_length=100, verbose_name='App Type',blank=True, choices=constants.APPLICATION_TYPE.items(), default='TBD') | |
managed_by = models.CharField(max_length=50, verbose_name='Managed by', blank=True, choices=constants.APPLICATION_IS_MANAGED_BY.items()) | |
data_classification = models.CharField(max_length=100, blank=True, choices=constants.APPLICATION_DATA_CLASSIFICATION.items(), default='Unrestricted') | |
mapping_status = models.CharField(max_length=50,verbose_name='Mapping Status',blank=True, choices=constants.MAPPING_STATUS.items(), default='Pending') | |
slug = models.SlugField(max_length=250, unique_for_date='created_on') | |
tags = TaggableManager(blank =True) | |
manager = models.ForeignKey(User, on_delete=models.CASCADE) | |
hosted_on = models.ManyToManyField(Server, through='ServerApplicationRelationship', blank=True, | |
verbose_name='Installed on') | |
relationships = models.ManyToManyField('self', through='Relationship', | |
symmetrical=False, | |
related_name='related_to') | |
def add_relationship(self, application, status): | |
relationship, created = Relationship.objects.get_or_create( | |
from_application=self, | |
to_application=application, | |
status=status) | |
return relationship | |
def remove_relationship(self, application, status): | |
Relationship.objects.filter( | |
from_application=self, | |
to_application=application, | |
status=status).delete() | |
return | |
def get_relationships(self, status): | |
return self.relationships.filter( | |
to_apps__status=status, | |
to_apps__from_application=self) | |
def get_related_to(self, status): | |
return self.related_to.filter( | |
from_apps__status=status, | |
from_apps__to_application=self) | |
def get_following(self): | |
if not self.get_relationships(RELATIONSHIP_DOWNSTREAM): | |
return "-" | |
return self.get_relationships(RELATIONSHIP_DOWNSTREAM) | |
def get_followers(self): | |
if not self.get_related_to(RELATIONSHIP_DOWNSTREAM): | |
return "-" | |
return self.get_related_to(RELATIONSHIP_DOWNSTREAM) | |
def get_friends(self): | |
if not self.relationships.filter( | |
to_apps__status=RELATIONSHIP_DOWNSTREAM, | |
to_apps__from_application=self, | |
from_apps__status=RELATIONSHIP_DOWNSTREAM, | |
from_apps__to_application=self): | |
return "-" | |
return self.relationships.filter( | |
to_apps__status=RELATIONSHIP_DOWNSTREAM, | |
to_apps__from_application=self, | |
from_apps__status=RELATIONSHIP_DOWNSTREAM, | |
from_apps__to_application=self) | |
def __str__(self): | |
return "{}".format(self.name) | |
def get_app_tags(self): | |
tags = [] | |
for tag in self.tags.all(): | |
tags.append(str(tag)) | |
return ', '.join(tags) | |
@property | |
def get_server_count(self): | |
return u'%s' % ServerApplicationRelationship.objects.filter(app=self).count() | |
get_server_count.fget.short_description = u'srvs' | |
#server_count = property(get_server_count) | |
class Meta: | |
ordering = ('name',) | |
class Relationship(models.Model): | |
from_application = models.ForeignKey(Application, related_name='from_apps') | |
to_application = models.ForeignKey(Application, related_name='to_apps') | |
status = models.IntegerField(choices=RELATIONSHIP_STATUSES) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment