Django Rest Framework Token Authentication with MongoEngine User - python

I am using a Django installation with MongoEngine to get MongoDB to work as my project's backend.
I've got the implementation to work but am stuck with Django Rest Framework's authentication system. Was just reading the API guide for their authentication chapter and was trying out their TokenAuthentication.
Tokens are created using
t = Token.objects.create(user=..)
and it expects a Django User instance. Since I am using MongoEngine, my database entry in the settings.py file is set to Dummy.
So how do I create a user instance that can be used by Token class.
I tried creating users using MongoEngine's mongoengine.django.auth but the Token class isn't accepting this object.
The resulting error is:
ValueError: Cannot assign "<User: gaurav>": "Token.user" must be a "User" instance.
Please let me know how I can get this to work.

Unfortunately you have to write it for yourself. As a reference you can use a gist I just created: https://gist.github.com/RockingRolli/79ceab04adb72c106cd6
I solved the issue a few weeks ago and it works. The code basically is inheriting the TokenAuthentication and adds Mongoengine specific behaviour.
There are also Django user features provided by Mongoengine: http://docs.mongoengine.org/en/latest/django.html#custom-user-model - IIRC you also need them for MongoTokenAuthentication.
All in all using Django (+Rest Framework) with Mongoengine can be tricky at some point and currently it looks like these issues will not be resolved soon.

Related

Verifying in external Django REST API if a Wordpress user is a paid subscriber or not

I have been working on a program in python that I want to make available to paid subscribers via REST. I'm currently thinking of having the frontend made in Wordpress and then host the backend somewhere else.
In wordpress there are a bunch of plugins to handle paid subscribers and so on and everything seems great but my concern is how do I verify this on the backend that is hosted somewhere else?
If I use Django, is there any way I can make some kind of call to the Wordpress server(?) and verify that the user that is trying to fetch items are an paid subscriber?
I made a Diagram to kind of show what I mean. Basically B should only answer back with items if the caller A is a paid subscriber.
I've read that it is possible to generate an API key that will be needed in order to fetch data from the API, I've also read ways of hiding this call from the frontend to the backend from the user by using some kind of relay on wordpress. Might be wrong here.
Is there any preferred way of doing this?
Is Django REST & Wordpress suitable options to do this?
You can do that using the Django REST framework(DRF) which is used for such purpose for making the rest API's.
As per your query i would suggest you to the DRF to read the data from wordpress database and perform the validation on top of it.
Here are some links that you can use for reference :-
https://pythonrepo.com/repo/istrategylabs-django-wordpress-python-third-party-apis-wrappers
https://www.ianlewis.org/en/administer-wordpress-django-admin
In programming almost anything is possible. However, since wordpress is built in php I would not say that it would be possible to work directly with it. But, (MAYBE) you can connect the wordpress database to django for READ only and create an api.
How I would do it if I had this task:
Connect Django to an existing db (your wordpress db):
Django itself teaches you how to connect with a legacy database from its documentation.
Django comes with a utility called inspectdb that can create models by
introspecting an existing database. You can view the output by running
this command:
$ python manage.py inspectdb
Save this as a file by using standard Unix output redirection:
$ python manage.py inspectdb > models.py
**This feature is meant as a shortcut, not as definitive model generation.**
Since you created the models you can create your endpoints:
Create you serializers and viewsets from the models that was auto-generated by django from your wordpress db.
Display the data you need such as the user data you need to fetch ex:paid_subscriber = True or paid_subscriber = 1. Certainly it will be there.
I think the only issue you will have is to connect with the wordpress database. After you have done this in django nothing can stop you to create endpoints with django-rest-framework displaying the data it has.

How to use Arangodb in Django database?

I am starting developing a website, I want to use Django and Arangodb, but Django's built-in databases backend does not support arango.
django documentation refers to this in database section:
You can use a database backend that doesn’t ship with Django by setting ENGINE to a fully-qualified path (i.e. mypackage.backends.whatever).
but I could not find anything that bridges between Django and Arango. there is this repo in GitHub https://github.com/pablotcarreira/django-arangodb but this does not support new features of Django and Arango.
I would like any thread to lead me to solve this problem.
Navigate this projects:
https://github.com/pablotcarreira/django-arangodb
Build API for Django with Foxx or use ArangoDB Python driver?
You must first create an app for the database, for example dbms, and then implement all the required items such as crud in a .py file yourself and use the query class in the view you want and return it such as list ,dict or ... .
Note ! In the __init __ file, be sure to create an object from the created class

How to use custom Django backend?

I'm developing an app in Django which uses an existing database with created users.
I set my database configuration parameters to a PostgreSQL server and I perform my custom queries through "connections" library.
The problem comes when I want to use my own table to authenticate users. I saw many tutorials and blog posts and I rewritten my authentication backend. But when I want to use my own table to authenticate users and set sessions, Django's Framework only allows me to use User object.
I think these object is linked to Django tables in database and when I want to authenticate an user shows me a message saying the relation "auth_user" doesn't exists. This means that User object is linked to this table.
Does there, exist some method to use my own table with Django Authentication Backend or should I use it?
To use custom Django model with existing Django login backend
AUTH_USER_MODEL = 'myapp.MyUser'
Django docs
To use custom Django authentication backend
AUTHENTICATION_BACKENDS = 'myBackend'
Django docs

Should I save user backend in database in Django?

I'm creating a Django website that supports both local login backend and LDAP login (through django-auth-ldap), and maybe more in the future.
I'm getting into Django login and backends sutff and have a couple of questions - mainly is there any reason Django doesn't keep user creation backend in the database? Shouldn't user A be linked (and by linked I mean a field on User model) with the backend django.contrib.auth.backends.ModelBackend for safety/convince reason?
I'm getting around to creating a custom user model, and was thinking about adding such field. The ability to unambiguously know which backend was/is used to create/login the user sounds logically for me, but the fact that Django doesn't have that by default, and that I can't find anything similar on the Internet has me worried that I didn't think of a really good reason for why it's done the way it is.
Thanks in advance,
Paweł
Django doesn't need that info. Once the user is authenticated, and django has a User model, it doesn't care what backend authenticated it. The User model data is stored in one source. The User model (whether the default or custom) is consistent and has the same attributes, functionality and behaviour across the entire django project and schema. Nothing in the out-of-the-box django deals with different user models.
You may extend this with AbstractBaseUser, but managing really different users across the same project, especially with the core django modules, is a strech.
Django uses the User model a lot, and you will have to manually locate each place it does, and provide your own router to the correct backend. There is no API for this (like, say, db routers), it's going to be a mess of hacks that will probably even messier with each upgrade.
Django does support, in addition to the custom user model, "authentication backends". Some of the functionality your are looking for is available and exposed with this option, in a formal API. So you probably want to stick with that.
see:https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#authentication-backends

Django ModelFormSet with Google app engine

I'm using Django with google app engine. I'm using the google furnished django app engine helper project.
I'm attempting to create a Django modelformset like this:
#MyModel inherits from BaseModel
MyFormSet = modelformset_factory(models.MyModel)
However, it's failing with this error:
'ModelOptions' object has no attribute 'fields'
Apparently modelformset_factory() is expecting MyModel to implement a 'fields' accessor.
Anybody successfully used a modelformset with GAE datastore? Or is this a fundamental incompatibility between Django and GAE?
It is a fundamental incompatibility between Django and GAE, because they do not share the same interface for their models. The django helper does not include a patch for the modelformsets, but django-nonrel probably does, or will eventually.
Since the google team does not spend much time on the django helper any more, you are probably better off looking at django-nonrel http://www.allbuttonspressed.com/projects/django-nonrel unless you want to patch the helper yourself.

Categories

Resources