def handle(self, **options): from django.template.defaultfilters import slugify from sentry.models import Project, Team, ProjectKey, User from sentry.db.models import update if options.get('owner'): owner = User.objects.get(username__iexact=options.get('owner')) else: owner = None if owner: print "Assigning ownerless projects to %s" % owner.username # Assign unowned projects for project in Project.objects.filter(owner__isnull=True): update(project, owner=owner) print "* Changed owner of %s" % project # Create teams for any projects that are missing them print "Creating missing teams on projects" for project in Project.objects.filter(team__isnull=True, owner__isnull=False): team = Team( name=project.name, owner=project.owner, ) base_slug = slugify(team.name) slug = base_slug n = 0 while True: if Team.objects.filter(slug=slug).exists(): n += 1 slug = base_slug + '-' + str(n) continue team.slug = slug break team.save() update(project, team=team) print "* Created team %s for %s" % (team, project) # Create missing project keys print "Creating missing project keys" for team in Team.objects.all(): for member in team.member_set.select_related('user'): for project in team.project_set.all(): try: created = ProjectKey.objects.get_or_create( project=project, user=member.user, )[1] except ProjectKey.MultipleObjectsReturned: pass else: if created: print "* Created key for %s on %s" % ( member.user.username, project)
def handle(self, **options): from django.template.defaultfilters import slugify from sentry.models import Project, Team, ProjectKey, User from sentry.db.models import update if options.get('owner'): owner = User.objects.get(username__iexact=options.get('owner')) else: owner = None if owner: print("Assigning ownerless projects to %s" % owner.username) # Assign unowned projects for project in Project.objects.filter(owner__isnull=True): update(project, owner=owner) print("* Changed owner of %s" % project) # Create teams for any projects that are missing them print("Creating missing teams on projects") for project in Project.objects.filter(team__isnull=True, owner__isnull=False): team = Team( name=project.name, owner=project.owner, ) base_slug = slugify(team.name) slug = base_slug n = 0 while True: if Team.objects.filter(slug=slug).exists(): n += 1 slug = base_slug + '-' + str(n) continue team.slug = slug break team.save() update(project, team=team) print("* Created team %s for %s" % (team, project)) # Create missing project keys print("Creating missing project keys") for team in Team.objects.all(): for member in team.member_set.select_related('user'): for project in team.project_set.all(): try: created = ProjectKey.objects.get_or_create( project=project, user=member.user, )[1] except ProjectKey.MultipleObjectsReturned: pass else: if created: print("* Created key for %s on %s" % (member.user.username, project))
def handle(self, **options): from django.db.models import Q from sentry.constants import RESERVED_ORGANIZATION_SLUGS from sentry.models import Organization, Project, Team, ProjectKey from sentry.db.models import update from sentry.db.models.utils import slugify_instance from sentry.utils.query import RangeQuerySetWrapperWithProgressBar print("Correcting data on organizations") queryset = Organization.objects.filter( slug__isnull=True, ) for org in RangeQuerySetWrapperWithProgressBar(queryset): slugify_instance(org, org.name, RESERVED_ORGANIZATION_SLUGS) org.save() # Create teams for any projects that are missing them print("Correcting data on projects") queryset = Project.objects.filter( Q(team__isnull=True) | Q(organization__isnull=True), ).select_related('owner') for project in RangeQuerySetWrapperWithProgressBar(queryset): if not project.team: organization = Organization( name=project.name, owner=project.owner, ) slugify_instance(organization, organization.name, RESERVED_ORGANIZATION_SLUGS) organization.save() team = Team( name=project.name, owner=project.owner, oprganization=organization, ) slugify_instance(team, team.name, RESERVED_ORGANIZATION_SLUGS) team.save() update(project, organization=team.organization, team=team) # Create missing project keys print("Creating missing project keys") queryset = Team.objects.all() for team in RangeQuerySetWrapperWithProgressBar(queryset): for member in team.member_set.select_related('user'): for project in team.project_set.all(): try: created = ProjectKey.objects.get_or_create( project=project, user=member.user, )[1] except ProjectKey.MultipleObjectsReturned: pass
def handle(self, **options): from django.db.models import Q from sentry.constants import RESERVED_ORGANIZATION_SLUGS from sentry.models import Organization, Project, Team, ProjectKey from sentry.db.models import update from sentry.db.models.utils import slugify_instance from sentry.utils.query import RangeQuerySetWrapperWithProgressBar print("Correcting data on organizations") queryset = Organization.objects.filter(slug__isnull=True, ) for org in RangeQuerySetWrapperWithProgressBar(queryset): slugify_instance(org, org.name, RESERVED_ORGANIZATION_SLUGS) org.save() # Create teams for any projects that are missing them print("Correcting data on projects") queryset = Project.objects.filter( Q(team__isnull=True) | Q(organization__isnull=True), ).select_related('owner') for project in RangeQuerySetWrapperWithProgressBar(queryset): if not project.team: organization = Organization( name=project.name, owner=project.owner, ) slugify_instance(organization, organization.name, RESERVED_ORGANIZATION_SLUGS) organization.save() team = Team( name=project.name, owner=project.owner, oprganization=organization, ) slugify_instance(team, team.name, RESERVED_ORGANIZATION_SLUGS) team.save() update(project, organization=team.organization, team=team) # Create missing project keys print("Creating missing project keys") queryset = Team.objects.all() for team in RangeQuerySetWrapperWithProgressBar(queryset): for member in team.member_set.select_related('user'): for project in team.project_set.all(): try: created = ProjectKey.objects.get_or_create( project=project, user=member.user, )[1] except ProjectKey.MultipleObjectsReturned: pass
def forwards(self, orm): from django.template.defaultfilters import slugify from sentry.db.models import update for project in orm['sentry.Project'].objects.all(): if project.slug: continue base_slug = slugify(project.name) slug = base_slug n = 0 while orm['sentry.Project'].objects.filter(slug=slug).exists(): n += 1 slug = base_slug + '-' + str(n) update(project, slug=slug)
def create_team_and_keys_for_project(instance, created, **kwargs): if not created or kwargs.get('raw'): return if not instance.owner: return if not instance.team: team = Team(owner=instance.owner, name=instance.name) slugify_instance(team, instance.slug) team.save() update(instance, team=team) if not ProjectKey.objects.filter(project=instance, user__isnull=True).exists(): ProjectKey.objects.create(project=instance, )
def create_team_and_keys_for_project(instance, created, **kwargs): if not created or kwargs.get('raw'): return if not instance.owner: return if not instance.team: team = Team(owner=instance.owner, name=instance.name) slugify_instance(team, instance.slug) team.save() update(instance, team=team) if not ProjectKey.objects.filter(project=instance, user__isnull=True).exists(): ProjectKey.objects.create( project=instance, )