Skip to content

Commit

Permalink
Make Login + Register return more user information (uselotus#311)
Browse files Browse the repository at this point in the history
Co-authored-by: mnida <[email protected]>
Co-authored-by: Hardik-uselotus <[email protected]>
Co-authored-by: Hardik Agarwal <[email protected]>
Co-authored-by: mnida <[email protected]>
  • Loading branch information
5 people authored Nov 18, 2022
1 parent 00b8f03 commit 2fd4fad
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.0.5 on 2022-11-17 23:54

from django.db import migrations, models
import metering_billing.utils.utils


class Migration(migrations.Migration):

dependencies = [
("metering_billing", "0077_remove_plancomponent_cost_per_batch_and_more"),
]

operations = [
migrations.AddField(
model_name="historicalorganization",
name="organization_id",
field=models.CharField(
default=metering_billing.utils.utils.organization_uuid, max_length=100
),
),
migrations.AddField(
model_name="organization",
name="organization_id",
field=models.CharField(
default=metering_billing.utils.utils.organization_uuid, max_length=100
),
),
]
5 changes: 3 additions & 2 deletions backend/metering_billing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
metric_uuid,
now_plus_day,
now_utc,
organization_uuid,
periods_bwn_twodates,
plan_uuid,
plan_version_uuid,
Expand Down Expand Up @@ -64,6 +65,7 @@


class Organization(models.Model):
organization_id = models.CharField(default=organization_uuid, max_length=100)
company_name = models.CharField(max_length=100, blank=False, null=False)
payment_provider_ids = models.JSONField(default=dict, blank=True, null=True)
created = models.DateField(default=now_utc)
Expand Down Expand Up @@ -671,9 +673,8 @@ def save(self, *args, **kwargs):
and META
and self.cost_due.amount > 0
):
print("track event")
lotus_python.track_event(
customer_id=self.organization.company_name + str(self.organization.pk),
customer_id=self.organization.organization_id,
event_name="create_invoice",
properties={
"amount": float(self.cost_due.amount),
Expand Down
5 changes: 4 additions & 1 deletion backend/metering_billing/serializers/model_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ class Meta:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("username", "password")
fields = ("username", "email", "company_name", "organization_id")

organization_id = serializers.CharField(source="organization.id")
company_name = serializers.CharField(source="organization.company_name")


## CUSTOMER
Expand Down
4 changes: 4 additions & 0 deletions backend/metering_billing/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ def invoice_uuid():
return "inv_" + str(uuid.uuid4())


def organization_uuid():
return "org_" + str(uuid.uuid4())


def date_as_min_dt(date):
return datetime.datetime.combine(date, datetime.time.min, tzinfo=pytz.UTC)

Expand Down
77 changes: 61 additions & 16 deletions backend/metering_billing/views/auth_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
from rest_framework.views import APIView

POSTHOG_PERSON = settings.POSTHOG_PERSON
LOTUS_HOST = settings.LOTUS_HOST
LOTUS_API_KEY = settings.LOTUS_API_KEY
if LOTUS_HOST and LOTUS_API_KEY:
lotus_python.api_key = LOTUS_API_KEY
lotus_python.host = LOTUS_HOST
META = settings.META


class LoginViewMixin(KnoxLoginView):
Expand All @@ -42,24 +38,49 @@ class LogoutViewMixin(KnoxLogoutView):


class LoginView(LoginViewMixin, APIView):
@extend_schema(
request=inline_serializer(
name="LoginRequest",
fields={
"username": serializers.CharField(),
"password": serializers.CharField(),
},
),
responses={
200: inline_serializer(
name="LoginSuccess",
fields={
"detail": serializers.CharField(),
"token": serializers.CharField(),
"user": UserSerializer(),
},
),
400: inline_serializer(
name="LoginFailure",
fields={
"detail": serializers.CharField(),
},
),
},
)
def post(self, request, format=None):
try:
data = json.loads(request.body)
except json.JSONDecodeError:
return JsonResponse(
return Response(
{"detail": "Invalid JSON."}, status=status.HTTP_400_BAD_REQUEST
)

if data is None:
return JsonResponse(
return Response(
{"detail": "No data provided."}, status=status.HTTP_400_BAD_REQUEST
)

username = data.get("username")
password = data.get("password")

if username is None or password is None:
return JsonResponse(
return Response(
{"detail": "Please provide username and password."},
status=status.HTTP_400_BAD_REQUEST,
)
Expand Down Expand Up @@ -146,9 +167,20 @@ def get(self, request, *args, **kwargs):
@extend_schema(
request=RegistrationSerializer,
responses={
201: inline_serializer(
"RegistrationResponse", fields={"detail": serializers.CharField()}
)
200: inline_serializer(
name="RegistrationSuccess",
fields={
"detail": serializers.CharField(),
"token": serializers.CharField(),
"user": UserSerializer(),
},
),
400: inline_serializer(
name="RegistrationFailure",
fields={
"detail": serializers.CharField(),
},
),
},
)
class RegisterView(LoginViewMixin, APIView):
Expand Down Expand Up @@ -196,9 +228,9 @@ def post(self, request, format=None):
company_name=reg_dict["company_name"],
)
token = None
if LOTUS_HOST and LOTUS_API_KEY:
if META:
lotus_python.create_customer(
customer_id=org.company_name + str(org.pk),
customer_id=org.organization_id,
name=org.company_name,
)

Expand Down Expand Up @@ -228,6 +260,7 @@ def post(self, request, format=None):
{
"detail": "Successfully registered.",
"token": AuthToken.objects.create(user)[1],
"user": UserSerializer(user).data,
},
status=status.HTTP_201_CREATED,
)
Expand All @@ -236,9 +269,20 @@ def post(self, request, format=None):
@extend_schema(
request=DemoRegistrationSerializer,
responses={
201: inline_serializer(
"DemoRegistrationResponse", fields={"detail": serializers.CharField()}
)
200: inline_serializer(
name="DemoRegistrationSuccess",
fields={
"detail": serializers.CharField(),
"token": serializers.CharField(),
"user": UserSerializer(),
},
),
400: inline_serializer(
name="DemoRegistrationFailure",
fields={
"detail": serializers.CharField(),
},
),
},
)
class DemoRegisterView(LoginViewMixin, APIView):
Expand Down Expand Up @@ -276,6 +320,7 @@ def post(self, request, format=None):
{
"detail": "Successfully registered.",
"token": AuthToken.objects.create(user)[1],
"user": UserSerializer(user).data,
},
status=status.HTTP_201_CREATED,
)
Loading

0 comments on commit 2fd4fad

Please sign in to comment.