import os

from django.conf import settings
from django.db import models

from libs.models import DiffModel
from projects.models import Project
from repos import git


class RepoMixin(object):
    @property
    def user_path(self):
        return os.path.join(settings.REPOS_ROOT, self.project.user.username)

    @property
    def project_path(self):
        return os.path.join(self.user_path, self.project.name)

    @property
    def path(self):
        """We need to nest the git path inside the project path to make it easier
        to create docker images."""
        return os.path.join(self.project_path, self.project.name)

    @property
    def git(self):
        return git.get_git_repo(repo_path=self.path)

    @property
    def last_commit(self):
        """Returns a tuple (hash, and commit object)"""
        return git.get_last_commit(repo_path=self.path)

    def get_tmp_tar_path(self):
        return os.path.join(self.path, '{}_new.tar.gz'.format(self.project.name))


class Repo(DiffModel, RepoMixin):
    """A model that represents a repository containing code."""
    project = models.OneToOneField(
        Project,
        on_delete=models.CASCADE,
        related_name='repo')
    is_public = models.BooleanField(default=True, help_text='If repo is public or private.')


class ExternalRepo(DiffModel, RepoMixin):
    """A model that represents an external repository containing code."""
    project = models.ForeignKey(
        Project,
        on_delete=models.CASCADE,
        related_name='external_repos')
    git_url = models.URLField()
    is_public = models.BooleanField(default=True, help_text='If repo is public or private.')

    class Meta:
        unique_together = (('project', 'git_url'),)

    @property
    def name(self):
        git_name = self.git_url.split('/')[-1]
        return git_name.split('.git')[0]

    @property
    def path(self):
        """We need to nest the git path inside the project path to make it easier
        to create docker images."""
        return os.path.join(self.project_path, self.name)


class CodeReference(DiffModel):
    """A model that represents a reference to repo and code commit."""
    repo = models.ForeignKey(
        Repo,
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
        related_name='references')
    external_repo = models.ForeignKey(
        ExternalRepo,
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
        related_name='references')
    commit = models.CharField(
        max_length=40,
        blank=True,
        null=True)

@@ -1293,16 +1293,88 @@
 ate.')%0A%0A
+    def __str__(self):%0A        return '%7B%7D %3Crepo%3E'.format(self.project)%0A%0A
 %0Aclass E
@@ -1800,16 +1800,97 @@
 rl'),)%0A%0A
+    def __str__(self):%0A        return '%7B%7D - %7B%7D'.format(self.project, self.name)%0A%0A
     @pro
@@ -2735,8 +2735,88 @@
 l=True)%0A
+%0A    def __str__(self):%0A        return '%7B%7D %3C%7B%7D%3E'.format(self.repo, self.commit)%0A

Add str representation for repos and code reference