Hello, i hope i can get some help with this challenge.
I am working on an app that will use vue js for the custom admin and the frontend.
So there will be a dashboard where users can manage all crud operations for all models and everything, currently it is a practice project, where i want to redo my elearning web app that is using normal Django.
So let’s say there is a model named Course, and it has two fields, to make it simple. A course title and a relation to the creator/owner.
class Course(models.Model):
title = models.CharField(max_length=255)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='owners_courses')
def __str__(self):
return self.title
Courses app has schema.py
from ninja import ModelSchema
from .models import Course
class CourseSchema(ModelSchema):
class Meta:
model = Course
fields = ['id','title','owner']
and api.py, then on the router “/courses” we have all our Django ninja paths.
class TokenAuth(HttpBearer):
def authenticate(self, request, token):
return get_object_or_404(User, token=token)
@router.get('/list', response=list[CourseSchema], auth=TokenAuth())
def list_courses(request):
courses = Course.objects.all()
return courses
My custom user model has the token field which gets created automatically, and then i have this TokenAuth for authentication. That is all okay, i think.
Login api
# USER LOGIN
@router.post('/login')
def login_view(request, data:UserLoginSchema):
User = get_user_model()
user = authenticate(
email = data.email,
password = data.password
)
if user is None:
return {
"success":False,
"detail":"Invalid credentials"
}
if user.deleted:
return {
"success":False,
"detail":"Your account is deleted, contact support"
}
#token = user.token
# generate new token
user.token = uuid.uuid4().hex
user.save()
return {
"success": True,
"token": user.token
}
But how do we create and work with Permissions?
I hope somebody can help please
Best regards from Serbia.
Thank you all!