Tricky MYSQL query dilema - python

So I have a tricky SQL query.
I have four tables:
User table which contain information on my users
Recipe table which contains information on a recipe such as the name, uploading user etc (not the ingredients)
Recipe-user associative table to link all my ingredients to the recipes
Ingredients table which holds all the ingredients that can be used
I want to create a function which downloads all this information in one go to display the users profile. This profile will consist of the users information - such as username and total recipes, with a tile view of the meals they have uploaded in a fashion similar to instagram.
So if you image the instagram user profile view:
you have the user information at the top and the images (in my case the recipes) below and when you tap a recipe you will see the list of ingredients. I want this however to be very responsive with no loading lag, hence the upfront loading of all the informaion.
Using joins this is of course not an overly difficult task, however what is produced is a messy list the length of the total number of ingredients.
I am using python with the pymysql module by the way.
I wonder if anyone has an ideas on the best possible way to go about this task?

Related

How to give all users the ability to view a base data set. And allowing them to add their own data entries to view and share to users

Just a heads up, I am new to using web frameworks. My only experience so far comes from completing the VSCode, Django and Mozilla MDN tutorials. But I'm making my way through these along with my own project.
I'm creating a web app that has an "official" database table that all website/app users can view. But, I want to be able to let them add their own data entries to the table, which only they can view and edit. And would be able to grant other users/friends access to their created data entries, to expand the total number of entries made available. Without making everyone using the site having to work out which data entries are the "official" and which are user created.
Hopefully a better way of understanding what I'm planning
So, what would be the best method for setting up user accounts to have access to the main database table and their own data set, which they can grant access for others to view?
Would this mean creating a table for each user, and if so how can this be set up automatically upon account creation?
I've read that creating a new table in the database can be cumbersome later on if lots of accounts with their own tables of data are created.
I've looked through the Django documentation, but it seems to be more focussed on user account creation and authorisation. And regarding databases, I can't find any questions/posts that relate to what I'm trying to make. Especially with creating a personal list of data entries for each user upon account creation.
Thank you, for taking the time to read this, even if you don't have an answer!

How to store and access user data in database in Django?

I'm a newbie in Django and I'm building this web app that allows three different types of users to login. A customer, operator and an accountant. When the customer logs in, he is asked to upload two jpeg documents. When he is done, these documents will be converted into editable text(I'm using Google's Tesseract engine for Optical character recognition for this) and this data is stored in three columns. The first two columns are non editable but the third is editable. In the third column, the user makes changes if the converted text has any errors(since OCR is not 100 % accurate).
At this point an email has to be sent to the operator. The operator logs in and checks whether the customer has uploaded the the right documents or not. If there are any errors, he edits them and hits the save button. At this stage an email is sent to the accountant and he logs in to verify the data for the second time. If he confirms, an email is sent to the customer saying his documents have been verified.
As of now, my app is taking an image and converting it into editable text and displaying it in an HTML template. I need to know how to store this text in a table of three columns and make it available for the operator and accountant to edit. And also, I need to know how to make three different types of logins for three different users.
Please help. I will really appreciate it.
You could've edited your question better but still, I'll try to answer as much as I understood:
Firstly let's start with the login. So, what you want is role-based login which you can easily achieve through Django auth_user and user_group. In this, you'll create a user through Django built-in auth system (django authentication) and after this assign a group to every user you create so that when you log in a user you can redirect him accordingly.
Next, you mentioned that you wanted to save data in DB. For that, you'll need to connect a DB through Django settings (my preference PostgreSQL) and then you have to create models according to your need (django models).
Lastly, for data read and write operations in DB you can look at Django ORM (django ORM)

Database Design for tracking user flow

I want to design a user tracking system between different models. There is a Module table which is related to sections and sections are related to sub-sections. Each sub-section can relate to different independent tables. The independent tables store different items (slides, videos, texts) which the user can go through in a linear flow just like a Coursera course. The flow will be predetermined by us but can be changed by us. So, the linear flow between the sub-sections can't be hardcoded. I also need to keep track of the user's progress through these sub-sections.
For Example:
A sub-section can point to game table where the user_info, game_score, game_completion_date_time will be stored or there might be case where the sub-section is pointing to slides table storing slide_text, slide_url and user_info.
I want to keep track of changes in those table so what should be my approach. Below i have posted an image in which i am approaching to the problem.
You might make a 'courseflows' table with the order being a sequence of comma-separated integers for ids in the proper order, then parse through that sequence when presenting sections to the user. Add whatever other information you need, and a timestamp so that any new users are assigned the most recent flow. By linking this, you can also preserve any user's flow in case you change it while users are halfway through.
I would also consider using different secondary tables to mark user completion of each module, section, and subsection instead of just one.

Solr & User data

Let's assume I am developing a service that provides a user with articles. Users can favourite articles and I am using Solr to store these articles for search purposes.
However, when the user adds an article to their favourites list, I would like to be able to figure out out which articles the user has added to favourites so that I can highlight the favourite button.
I am thinking of two approaches:
Fetch articles from Solr and then loop through each article to fetch the "favourite-status" of this article for this specific user from MySQL.
Whenever a user favourites an article, add this user's ID to a multi-valued column in Solr and check whether the ID of the current user is in this column or not.
I don't know the capacity of the multivalued column... and I also don't think the second approach would be a "good practice" (saving user-related data in index).
What other options do I have, if any? Is approach 2 a correct approach?
I'd go with a modified version of the first one - it'll keep user specific data that's not going to be used for search out of the index (although if you foresee a case where you want to search for favourite'd articles, it would probably be an interesting field to have in the index) for now. For just display purposes like in this case, I'd take all the id's returned from Solr, fetch them in one SQL statement from the database and then set the UI values depending on that. It's a fast and easy solution.
If you foresee that "search only in my fav'd articles" as a use case, I would try to get that information into the index as well (or other filter applications against whether a specific user has added the field as a favourite). I'd try to avoid indexing anything more than the user id that fav'd the article in that case.
Both solutions would however work, although the latter would require more code - and the required response from Solr could grow large if a large number of users fav's an article, so I'd try to avoid having to return a set of userid's if that's the case (many fav's for a single article).

Dynamic database tables in django

I am working on a project which requires me to create a table of every user who registers on the website using the username of that user. The columns in the table are same for every user.
While researching I found this Django dynamic model fields. I am not sure how to use django-mutant to accomplish this. Also, is there any way I could do this without using any external apps?
PS : The backend that I am using is Mysql
An interesting question, which might be of wider interest.
Creating one table per user is a maintenance nightmare. You should instead define a single table to hold all users' data, and then use the database's capabilities to retrieve only those rows pertaining to the user of interest (after checking permissions if necessary, since it is not a good idea to give any user unrestricted access to another user's data without specific permissions having been set).
Adopting your proposed solution requires that you construct SQL statements containing the relevant user's table name. Successive queries to the database will mostly be different, and this will slow the work down because every SQL statement has to be “prepared” (the syntax has to be checked, the names of table and columns has to be verified, the requesting user's permission to access the named resources has to be authorized, and so on).
By using a single table (model) the same queries can be used repeatedly, with parameters used to vary specific data values (in this case the name of the user whose data is being sought). Your database work will move along faster, you will only need a single model to describe all users' data, and database management will not be a nightmare.
A further advantage is that Django (which you appear to be using) has an extensive user-based permission model, and can easily be used to authenticate user login (once you know how). These advantages are so compelling I hope you will recant from your heresy and decide you can get away with a single table (and, if you planning to use standard Django logins, a relationship with the User model that comes as a central part of any Django project).
Please feel free to ask more questions as you proceed. It seems you are new to database work, and so I have tried to present an appropriate level of detail. There are many pitfalls such as this if you cannot access knowledgable advice. People on SO will help you.
This page shows how to create a model and install table to database on the fly. So, you could use type('table_with_username', (models.Model,), attrs) to create a model and use django.core.management to install it to the database.

Categories