from django.db import models
from django.utils import timezone

from sentry.db.models import FlexibleForeignKey, Model, control_silo_only_model, sane_repr
from sentry.models.apiscopes import HasApiScopes


@control_silo_only_model
class ApiAuthorization(Model, HasApiScopes):
    """
    Tracks which scopes a user has authorized for a given application.

    This is used to determine when we need re-prompt a user, as well as track
    overall approved applications (vs individual tokens).
    """

    __include_in_export__ = True

    # users can generate tokens without being application-bound
    application = FlexibleForeignKey("sentry.ApiApplication", null=True)
    user = FlexibleForeignKey("sentry.User")
    date_added = models.DateTimeField(default=timezone.now)

    class Meta:
        app_label = "sentry"
        db_table = "sentry_apiauthorization"
        unique_together = (("user", "application"),)

    __repr__ = sane_repr("user_id", "application_id")
