-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.py
155 lines (134 loc) · 5.2 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from django.contrib.auth import get_user_model
from django.db.models import Q, BooleanField, Case, When, Value
from django.urls import reverse
from rest_framework.views import APIView
from rest_framework import serializers, status
from rest_framework.response import Response
from rest_framework.authentication import (
BasicAuthentication,
SessionAuthentication,
TokenAuthentication,
)
from oauth2_provider.contrib.rest_framework import OAuth2Authentication
from webapp.apps.publish.views import GetProjectMixin
from .permissions import StrictRequiresActive
User = get_user_model()
class UserSerializer(serializers.ModelSerializer):
username = serializers.CharField(required=True, min_length=1)
class Meta:
model = User
fields = ("username",)
read_only = ("username",)
class UsersAPIView(APIView):
permission_classes = (StrictRequiresActive,)
authentication_classes = (
SessionAuthentication,
BasicAuthentication,
TokenAuthentication,
)
queryset = User.objects.order_by("username").all()
def get(self, request, *args, **kwargs):
search_term = request.query_params.get("username", None)
if not search_term:
return Response([], status=status.HTTP_200_OK)
suggested = (
User.objects.filter(username__icontains=search_term)
.annotate(
exact_match=Case(
When(username__iexact=search_term, then=Value(True)),
default=Value(False),
output_field=BooleanField(),
),
startswith_match=Case(
When(username__istartswith=search_term, then=Value(True)),
default=Value(False),
output_field=BooleanField(),
),
)
.order_by("-exact_match", "-startswith_match")
)[:10]
results = UserSerializer(suggested, many=True)
return Response(results.data, status=status.HTTP_200_OK)
class MeAPI(GetProjectMixin, APIView):
authentication_classes = (
SessionAuthentication,
TokenAuthentication,
OAuth2Authentication,
)
def get(self, request, *args, **kwargs):
user = request.user
user_resp = {}
if user.is_authenticated and user.profile:
user_resp = {"username": user.username, "status": user.profile.status}
else:
user_resp = {"username": None, "status": "anon"}
if kwargs:
project = self.get_object(**kwargs)
user_resp.update(
{"project": str(project), "project_role": project.role(user)}
)
return Response(user_resp)
class AccessStatusAPI(GetProjectMixin, APIView):
authentication_classes = (
SessionAuthentication,
TokenAuthentication,
)
def get(self, request, *args, **kwargs):
user = request.user
plan = {
"name": "free",
"plan_duration": None,
"cancel_at": None,
"trial_end": None,
}
remaining_private_sims = {}
if user.is_authenticated and user.profile:
user_status = user.profile.status
username = user.username
if getattr(user, "customer", None) is not None:
plan = user.customer.current_plan()
else:
user_status = "anon"
username = None
if kwargs:
project = self.get_object(**kwargs)
if plan["name"] == "free" and user.is_authenticated:
remaining_private_sims = user.profile.remaining_private_sims(
project=project
)
exp_cost, exp_time = project.exp_job_info(adjust=True)
if user.is_authenticated and user.profile:
can_run = user.profile.can_run(project)
can_write_project = project.has_write_access(user)
else:
can_run = False
can_write_project = False
return Response(
{
"is_sponsored": project.is_sponsored,
"sponsor_message": project.sponsor_message,
"user_status": user_status,
"can_run": can_run,
"can_write_project": can_write_project,
"server_cost": project.server_cost,
"exp_cost": exp_cost,
"exp_time": exp_time,
"api_url": reverse("access_project", kwargs=kwargs),
"username": username,
"plan": plan,
"remaining_private_sims": remaining_private_sims,
"project": str(project),
}
)
else:
if plan["name"] == "free" and user.is_authenticated:
remaining_private_sims = user.profile.remaining_private_sims()
return Response(
{
"user_status": user_status,
"api_url": reverse("access_status"),
"username": username,
"plan": plan,
"remaining_private_sims": remaining_private_sims,
}
)