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

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.

Related

Django DB Design and API Guidance

This is my first Django app, and also my first time building an app, and I am seeking some DB and API guidance. I have an app that functions as an online directory and marketplace. What I aim to do is to provide the app to many different organizations such that the organizations have their own online directory's and marketplace's where they can manage it fully on their own, but that their data is still linked to my database so that I can then operate on the data for machine learning purposes. This is a pretty standard/routine practice, I realize, but I am trying to wrap my head around how best to make it work. For each organization, would there just be an instance of the app which would then be a separate app or interface that connects to the original? From my newbie understanding, that is essentially what an API is, correct?
Since it's an API, I wouldn't separate the DBs or the instances, neither make several Django apps, but I would have a dedicated clients model/table in the DB, which would store encoded API keys for each of your organizations.
Each client makes its request to your API by authenticating and using their API key, and the Django views answer only with the data linked to the client, as other DB tables have a foreign key to clients.
Sounds pretty standard and RESTful to me.
Some Django / Django REST framework modules like "Django REST Framework API Key" could probably do most of this work for you.

How to programatically post to Django Rest Framework endpoint?

I've got an endpoint built with Django Rest Framework. I now want to separate the ingress and the parsing. So I created an endpoint which basically writes the received string of data to a textfield in. I now create a management command which continuously loops over that table and parses the contents.
So I get the I raw string from the ingress table, and I now want to post this to the DRF endpoint programmatically. I've done this many times in my unit tests. But I'm unsure how I can do this from the management command. I can use the same code as I do for test requests, but it seems weird to use testing code in production.
People might suggest to manually use the serializer, but there's also things happening in the viewset which I need.
Does anybody have a good idea how I would be able to do this?

Can I use an external database table for the login process in Django?

So I'm starting a new Django project that essentially requires the login & registration process be routed through an EXTERNAL & ALREADY created database.
Is it possible to have the User model use an EXTERNAL database table ONLY when Django is:
Logging in a user, to check if the login is valid
Registering a user, inserting data for that user in the external database
I would like for the rest of the Django server to use a local database.
If so, could someone either provide examples or guide me to documentation on the subject?
Easiest way to use multiple database with Django is to use a database routing. By default Django stick to single database, however, if you want to implement more interesting database routing system, you can define and install your own database routers.
Database routers are installed using the DATABASE_ROUTERS setting. You have to specify this setting in your settings.py file
What you have to do is write one AuthRouter as described Django documentation Django Multiple Database
"Yes, but"
What you are looking for in the docs is called "database router".
There is even an example for the auth app in the docs there.
But, there is s serious drawback to consider with this approach:
We cannot have cross-database relationships in the models. If auth tables are in a separate database, this means that any otehr app that needs a foreign key to User model is going to run into problems. You might be able to "fake" the relationships using a db that doesn't enforce relationship checks (SQLite or MyISAM/MySQL).
Out of the box, such apps are: session, authtoken, and admin (and probably more).
Alternatively, a single-sign-on solution might do a better job: django-sso, or django-mama-cas + django-cas-ng, or the commercial Stormpath.

Importing user data into django-allauth from a legacy database

I have a web application that is currently written in rather messy PHP with a messy database that I'm in the process of migrating to Django. I have set it up in my project as a legacy database and have generated models using inspectdb, so I think I can fairly easily write a script to translate old database to new, but I'm not sure how to tackle the user side.
Originally the app had some sort of Facebook integration, but this broke a few years ago when Facebook changed their side. The current users table has Facebook IDs for some users, but no other Facebook data (some users have also set email addresses and local passwords too).
I have a basic install of django-allauth with the Facebook integration set up and working, so can I import all of the users somehow? Those with the local alternative login-credentials (whether with a Facebook ID or not) are probably more important than the ones that only have a Facebook ID, so should I just use the standard Django create user methods and let users re-enable their own Facebook integration at a later time?

What is the fastest way to generate CRUD webpages for Google App Engine using Python?

I have created a set of Models for my database using Python. Now I'd like to rapidly load them with some data -- manually. If this were a .NET app, I'd use one of the nifty controls that come with Visual Studio to rapidly connect to the database and bind a grid to it. Then go to town adding data.
What's the matching way to do this in Python using Google App Engine?
In ASP.NET MVC, they have this new "scaffolding" stuff (part of Entity Framework) that will generate CRUD pages for you. Is there anything like that given a bunch of Model objects in GAE?
P.S. using the handy, dandy command line options --use_sqlite and --datastore_path, I can quickly backup my database in my dev environment once I do this.
If you are using Django on GAE then you can use the Django Administration site:
So what’s Django’s approach to these boring, repetitive tasks? It does
it all for you—in just a couple of lines of code, no less. With
Django, building an admin interface is a solved problem.
It automatically builds CRUD based HTML forms for managing the model.
Have a look to the appengine admin project.
Appengine Admin is simple python package that you can use for creating
automatic admin interface for your Google Appengine application.
Here is a screenshot:
and here is the quickstart tutorial.
After creating your models, simply adding this line of code:
# Register to admin site
appengine_admin.register(..your list of class Models definition)
and after have defined the proper route to the admin with:
(r'^(/admin)(.*)$', appengine_admin.Admin)
you can access to the customized admin that offers the following features:
List records for each registered model
Create new records
Update/edit records
Delete records
I'm still something of a python and GAE newbie, but I've been working with it a lot over the past few months so you might find that this works:
You can use Model.properties() to get the list of properties for the model in question and save it to a list. You can then add the list to the context dictionary for use in your template. In your template iterate over the loop to generate a basic list of input fields with the names matching each property.
{% for tItem in list %}
<input type="text" name="{{ tItem }}" />
{% endfor %}
Then you can post back to the same page, where you can use Request.arguments() to pair your object properties to your model for saving into the datastore.
To my knowledge there's not a much more elegant solution than that, at least not comparable to the ASP.NET MVC scaffolding that you're talking about.
(disclaimer: I've not actually tried this so there is likely a problem or two that needs to be sorted)
The trouble with Django on App Engine is you can't use the GAE datastore and ndb models (so the Django admin is not available), or you have to start using a hacked version of Django: http://django-nonrel.org/
Well probably for most apps you're better off using Cloud SQL anyway, which is basically MySQL so no prob with Django.
If you need to use the GAE datastore try this framework, which provides a CRUD admin:
http://ferris-framework.appspot.com/docs/index.html
You can check out Ferris Framework
which is tightly integrated to Google App engine and datastore.
Ferris Framework also has the scaffolding component for creating CRUD actions in a breeze.
http://ferris-framework.appspot.com/docs/users_guide/scaffolding.html?highlight=scaffolding

Categories

Resources