Suppose I am getting a list of user from an api in json format and I want to save it in my User model in django. Saving the user might not be a problem but I want that data continuously.
I am getting an api from a system which sends me the list of user who sent emails. These users go on increasing.
Now I am getting a list of users. I want to save these users in my database. But the questioning part is, suppose 10 user has sent messages I am getting list of 10 users from api then I will save it say like
usr = User()
usr.username = data["username"]
usr.save()
Now what when 1 more user sends the email. Now I will be receiving 11 user.
Here I want continuously add the updated user in my database. How to do this ?
I dont know if I have made it clear or not but need help on this
I guess your problem is how to trigger the retrieving of the emails.
A very easy way to do this is to use a timed autoreload - you can do this easily in html. See this answer. Then your django view can check for new emails and respond with the new data.
as per my understanding, you want to check for the new data entries periodically and save the new records into db. Right?
So for periodic tasks Celery is the best utility.
once you get data periodically then get db data into list_db and API data into API_list. Now start comparing both the lists data and store the new one into db.
I hope this will help.
Related
we are developing card game and there we should create a room with one user. All data about user and room must stored in session. Without sending data to database. Anyone has idea's how should i store data. And where should store it. And after 2 player connect to the game. I should it post to database.
Sorry, but i have no idea how to do it. If someone have code examples. Can you share it
Flask has module. You can use something like this:
from flask import session
and then you can use syntax for example:
if "logged" not in session:
etc.
It's hard to give specific advice if you don't share the code you have.
I think first create a flask project and decide on the database you will use. Create a model about users and export it to db. (i.e. migrate) . Then create the game model and determine the relationship between the users table and the game table. The information you share is limited, the most important point that comes to my mind with this information is the session management of the game and the user. The user is always present, the session is active when he enters the game and drops when he exits. You can write a scenario based on your own game about Session. With the alias given to the user's session, you can perform crud operations during the game according to the scenario.
Sample Code For "how to store session"
session['sample_user'] = value
session['sample_user2'] = value
# session keep value as a dict and you can use theese value
session.items()
I have recently started with pyrebase and I am having trouble in storing data depending on user and receiving it. After completing the authentication part I create a user ID as user['idToken']
And then pushed data by using
archer = {"name": "Sterling Archer", "agency": "Figgis Agency"}
db.child("agents").push(archer, user['idToken'])
I assume that each user has different ID token which remains same even if we logout and relogin. but when I am using
all_agents = db.child("agents").get(user['idToken']).val()
print(all_agentes)
It's printing all data stored in realtime database. Even which were stored by other users.
I tried reading all it's documentation, but I was unable to understand how to handle it.
What's wrong that I am doing in here and How can I correct it?
Use user[localId] instead of user[idToken] as it will create database for every different user separately
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)
I am writing a chat bot that uses past conversations to generate its responses. Currently I use text files to store all the data but I want to use a database instead so that multiple instances of the bot can use it at the same time.
How should I structure this database?
My first idea was to keep a main table like create table Sessions (startTime INT,ip INT, botVersion REAL, length INT, tableName TEXT). Then for each conversation I create table <generated name>(timestamp INT, message TEXT) with all the messages that were sent or received during that conversation. When the conversation is over, I insert the name of the new table into Sessions(tableName). Is it ok to programmatically create tables in this manner? I am asking because most SQL tutorials seem to suggest that tables are created when the program is initialized.
Another way to do this is to have a huge create table Messages(id INT, message TEXT) table that stores every message that was sent or received. When a conversation is over, I can add a new entry to Sessions that includes the id used during that conversation so that I can look up all the messages sent during a certain conversation. I guess one advantage of this is that I don't need to have hundreds or thousands of tables.
I am planning on using SQLite despite its low concurrency since each instance of the bot may make thousands of reads before generating a response (which will result in one write). Still, if another relational database is better suited for this task, please comment.
Note: There are other questions on SO about storing chat logs in databases but I am specifically looking for how it should be structured and feedback on the above ideas.
Don't use a different table for each conversation. Instead add a "conversation" column to your single table.
I have a user input form here:
http://www.7bks.com/create (Google login required)
When you first create a list you are asked to create a public username. Unfortuantely currently there is no constraint to make this unique. I'm working on the code to enforce unique usernames at the moment and would like to know the best way to do it.
Tech details: appengine, python, webapp framework
What I'm planning is something like this:
first the /create form posts the data to /inputlist/ (this is the same as currently happens)
/inputlist/ queries the datastore for the given username. If it already exists then redirect back to /create
display the /create page with all the info previously but with an additional error message of "this username is already taken"
My question is:
Is this the best way of handling server side validation?
What's the best way of storing the list details while I verify and modify the username?
As I see it I have 3 options to store the list details but I'm not sure which is "best":
Store the list details in the session cookie (I am using GAEsessions for cookies)
Define a separate POST class for /create and post the list data back from /inputlist/ to the /create page (currently /create only has a GET class)
Store the list in the datastore, even though the username is non-unique.
Thank you very much for your help :)
I'm pretty new to python and coding in general so if I've missed something obvious my apologies.
Tom
PS - I'm sure I can eventually figure it out but I can't find any documentation on POSTing data using the webapp appengine framework which I'd need in order to do solution 2 above :s maybe you could point me in the right direction for that too? Thanks!
PPS - It's a little out of date now but you can see roughly how the /create and /inputlist/ code works at the moment here: 7bks.com Gist
I would use Ajax to do an initial validation. For example as soon as the user name input box loses focus I would in the background send a question to the server asking if the user name is free, and clearly signal the result of that to the user.
Having form validation done through ajax is a real user experience delight for the user if done correctly.
Of course before any of the data was saved I would definitely redo the validation server side to avoid request spoofing.
jQuery has a nice form validation plugin if you are interested. http://docs.jquery.com/Plugins/validation.
In my career, I've never gotten around having to validate server side as well as client side though.
About the storing of the list (before you persist it to the datastore). If you use ajax to validate the user name you could keep the other fields disabled until a valid user name is filled in. Don't allow the form to be posted with an invalid user name!
That would perhaps solve your problem for most cases. There is the remote possibility that someone else steals the user name while your first user is still filling in his list of books. If you want to solve that problem I suggest simply displaying the list as you got it from the request from the user. He just sent it to you, you don't have to save it anywhere else.
Can you use the django form validation functionality (which I think should just abstract all this away from you):
http://code.google.com/appengine/articles/djangoforms.html
Search in that page for "adding an item" - it handles errors automatically (which I think could include non-unique username).
Warning: also a beginner... :)