Proper way to share temporary data between two users - python

Here is my case in django,i need to create a match between two users, users take turn, after one of them has finished, i need to send user1 score to user2 and vise versa.
How can i create a match between two players?
How do i store their data(scores) in a way that can be accessed for both users.
Here is what i've tried : user1 sends GET request to get a user2 as a opponent by getting user2 id (i can say i made a match there), then after user1 finished he make another GET request with his id and score to django. and here is where i stuck :
I don't know how to send user1 score to user2.
I don't know how to store the data so both users can access each other score.
I'm quite new to django, so i did all that based on my basic knowledge of the framework, please correct me if i'm wrong and possibly suggest a better approach.

An approach I would take would be to create two models one for Match and the other for turn. Match would contain fields including foreign keys to player_1 and player_2 and the overall score, game active and which players turn it is. The second model I would create would be a turn which has the details of the turn and a foreign key to the match.
Each time a turn occurs it would be a CreateView, see Class based views in the django docs. This will create a new turn, and you should be able to automatically add in the game that it belongs to.
The problem you will have then is then notifying the other user that the new turn has been taken. The simplest way would be to just wait and let the user reload the page but that may lead to a subpar user experience.
The next way would be to create a javascript loop to periodically poll your backend and create an alert when the other player has taken their turn.
Finally if you want to do a push from the backend to the browser you will need to investigate django channels which allows you to setup a websocket connection from the backend to the front end. This will allow you to update your UI as new data is available.
For a new user you might want to progress through the options in the order that has been outlined just to build your experience.

Related

How to create element in another model with django

I have a small question. That is actually making me scratch my head.
So in my Database, I have the following models:
Activity
Bill
Clients
I think you are all seeing the relationship I am trying to create :
A Bill has one client and one or more activities. Here is the trick to make this whole thing user-friendly I am trying to create Bills (with the url: Bill/new) that can be edited manually. So the user is sent to an HTML page with the basic Bill template and he has a table that can add some rows with the activity the time spent and its cost.
There are three things I am trying to achieve.
Generate automatically the ID of the Bill (it should be pk of Bill) but it seems it's not generated until I have pressed on save.
When I save a Bill I want to save also the activities I have entered manually.
When I save the Bill I would like to save it as a Word or PDF document in the database.
Are these possible?
Thanks all for reading and helping I am banging my head to figure out how to do all of this and I am quite a newbie so any help is welcome.
Thanks in advance.
One way would be for the Bill objects to have a boolean field in_preparation. There would be a sequence of forms involved. The first would create a minimal Bill object with in_preparation=True. Then the related objects could be created and linked to this Bill. The final stage would be to display the entire bill and related objects for checking, with options to go back and edit, or "Confirm and issue to customer". This latter would set in_preparation=False and generate the pdf and word files.
If all the necessary information is already available and you are just asking how to use a ModelForm in this circumstance, the answer is obj = form.save( commit=False). It's then up to you to call obj.save() once you have finished updating it or its related objects More information here
If you are saving a bunch of objects at once and want to be sure it's all-or-nothing (the latter if something throws an exception), you need a Django transaction.
Generate automatically the ID of the Bill (it should be pk of Bill) but it seems it's not generated until I have pressed on save.
For bills and any other "real world"-like documents you'd probably better define some custom number generator to detach number from the auto-id and to be able to generate numbers using more complex patterns (eg AB-12345/321).
But in any case whether you choose to use auto-id or a custom generator the most simple way to garanty existence and uniqueness of that id/number is to save bill instance first. Also this approach has some additional pros.
When I save a Bill I want to save also the activities I have entered manually.
You can use django formsets for this (assuming that your bill and activity models have fk or m2m relation)
https://docs.djangoproject.com/en/3.2/topics/forms/formsets/
When I save the Bill I would like to save it as a Word or PDF document in the database.
I'm not really sure if it is a good idea to store pdf or word files in db somehow.
In my opinion there are better ways
Generate pdf (word, excel etc) docs from data on request
Store as files in filesystem.
In this case files shouldn't be stored in publicly accessible
dirs (like media and static) and should be served instead with
FileResponce with proper access checks in the view

Flask - User to Profile Relationships

I'm working with Flask-restplus and I am at a point where I would like to associate each User in my user model to a type of profile, where each user can be associated with one or many profile types. I'm wondering how you guys would go about this. So far, here's what I'm thinking/planning to do. NOTE: I'm not very experienced in web development, so there's a chance I don't know the best way to accomplish this.
Step 1: Create a one-to-many (clients need to also be employees, see below) field (profile_types) relating to a static table that just lists all possible profile options. EXAMPLE:
PK PROFILE TYPE
1 provider
2 employee
3 client
.....
The idea here is to track different information and allow for different views/endpoints for users who are tied to certain profile types. Example, employees would provide a valid login authentication and be directed to page A while a client would be directed to page B, etc. We're also planning on collecting different data points within each profile model.
So an instance of a user might look like this, user1.profile == [client, employee'].
NOTE: This is more important for data collection (ie age of employee, start date, etc) than it is for creating directives based on permissions.
Step 2: Upon creating the new user, a signal fires off the creation of a profile based on the profile_types supplied to the user instance. I've used signals with django in the past, is there a signal library for Flask?
Step 3: An empty profile instance(s) now exists for that user. It will be up to a superuser to upload data in bulk or manually fill out profile information.
Is this a sensible way to go about this? My other though is to create a bunch of Boolean fields in the User model is_provider, is_employee, is_client, etc. All fields that are True get assigned a profile instance. What's the best way to go about this?
Thanks everyone!
Seeing that are you try to validate multiple profile types, you may use
if user.profile_type in ['employee', 'client']
Now, if you want to add an super user I think you can use this
if user.profile_type in ['employee', 'client'] and user.profile_type == 'superuser'
Now, you relationship is more like 'many-to-many', because you are saying that an client also needs to be an employee, if you mean that some endpoints needs to be accessible for employees and clients, then you need to use a 'many-to-one' relationship (an Stackoverflow question which explains what is that)
For your instances, there is Flask Marshmallow, which has an amazing compatibility with Flask SQLAlchemy if you are using an database, but Flask Marshmallow can work alone.

Custom user dashboard in django

I have a system that has two types of users with different privileges, the first user is the admin who can access all objects from the database, the second one is a dealer who can only view information pertaining to them alone.(There are many dealers)
This is how the system iworks: the admin creates a coupon code and issues it to a person (already done) then that person goes to a dealer who is supposed to check if that coupon code exists.
when a dealer logs in he is supposed to be redirected to a dashboard that has the number of items he has sold and to whom. To sell a new item he needs to check if that coupon code exists and if it does then access a form to fill in the item details(I have a model for issued_items)
How would I implement a custom admin page for the dealer without affecting the admin dashboard.
I created a dealer with super-admin and changed his permissions so that he is only able to change specific models, problem is, the models appear with all objects in that model even the ones by other dealers.
I have thought(not tried yet) of creating a view,and a template and redirect login but if i do this then i override the admin
(not so sure)Probably create a new app for the dealer???????
This sounds like a situation where you want the functionality to be loosely coupled to prevent headaches down the road, so I'd go with option 3. Leave the admin for the admins and create a new dealer app for the dealers to go to, with a regular view/model/template that they'll be required to login to see.
I wasn't able to elaborate my question coz am a django newbie, but after a week of trying a lot of different things I found a way out. For the dealers and other non staff users I created a dashboard and also overiding the registration to suit my project.
Its now working fine.

Django rest framework: correctly handle incoming array of model ids

I have a question about REST design in general and specifically what the best way to implement a solution is in Django Rest Framework. Here it the situation:
Say I have an app for keeping track of albums that the user likes. In the browser, the user sees a list of albums and each one has a check box next to it. Checking the box means you like the album. At the bottom of the page is a submit button.
I want the submit button to initiate an AJAX request that sends tp my API endpoint a list of the ids (as in, the Djano model ids) of the albums that are liked by the user.
My question is, is this a standard approach for doing this sort of thing (I am new to web stuff and REST in particular). In other words, is there a better way to handle the transmission of these data than to send an array of ids like this? As a corollary, if this is an alright approach, how does one implement this in Django Rest Framework in a way which is consistent with its intended methodology.
I am keeping this question a little vague (not presenting any code for the album serializer, for example) intentionally because I am looking to learn some fundamentals, not to debug a particular piece of code.
Thanks a lot in advance!
Consider the upvote button to the left. When you click it, a request may be sent to stackoverflow.com/question/12345/upvote. It creates an "action resource" on the db, so later you can go to your user profile and check out the list of actions you took.
You can consider doing the same thing for your application. It may be a better user experience to have immediate action taken like SO, or a "batch" request like with gmail's check boxes.

AppEngine Python - how to ensure that users register with unique uersnames

I am designing a new AppEngine/Python HRD application. In this application, I need to ensure that each user that registers cannot use a name that has already been assigned to another user, and that if two users attempt to register with the same username at the exact same moment, that only one of them will be given the username.
If I were to put all usernames in a single entity group and use transactions to ensure uniqueness, then the writing of the username/object in a transaction could potentially slow down all registration processes. So this approach doesn't seem like a good idea.
Another option would be to use the username as the key, which has the disadvantage of possibly making it difficult for the user to later change their username.
What is the best/standard approach to achieve this in AppEngine?
Ok.
You don't need to stick all the usernames in a big entity group to guarantee consistency.
Make the username the Key of the datastore entity governing the login.
Then inside a transaction.
Try a get using the username as a key (this is a consistent operation)
if you find it then obviously it's not available
If not found then create the new login entity.
As an aside if you used email addresses then it would more than likely mean no clashes ever. and I am not sure why visible nick names need to be unique, but then you probably have a good reason.
Each actual user object can have a system generated unique id, (this is a separate entity to the login entity).
If you are really paranoid, then look at using memcache CAS operations to effectively act as a lock on the username key and prevent simultaneous operations, though I don't think it would be necessary.
Entities might look like
class Login(ndb.Model):
# other stuff neede for authentication etc..
user = ndb.KeyProperty(User)
#ndb.transactional(xg=True)
#classmethod
def create_login(cls,username):
# maybe pass in extra user details
key = ndb.Key(cls, keyname)
login = key.get()
if login:
raise DuplicateUsernameError(username)
login = Login(key_name=username)
user = User.create_user(login=login.key)
login.user = user.key
login.put()
return (login,user)
class User(ndb.Model):
login = ndb.KeyProperty(Login) # this property is only for convenience.
nickname = ndb.StringProperty()
# etc
#classmethod
def create_user(cls,login_key):
# yes you would pass in more user stuff.
user = cls(login=login_key)
user.put()
# the unique user key is system generated.
return user
This means only a single get() is required to fetch a login, and second to get the rest of the user details but is much cheaper/faster than queries. It also means that the login name/code could be changed over time without disrupting the actual user entity if you really wanted such functionality. Or possibly support multiple login methods for a single user - ie facebook and twitter. It also means a login entity can be removed and login re-used by other people over time, and the user entity can stay if system integrity/history is required.
App Engine allows up to 1 write per second within the same entity group. To surpass that barrier you have to have 60 x 60 x 24 = 86.4k registrations on the same day, which I think you can live with.
If you still think you need any faster process you could use sharding (more typically known for counters - https://developers.google.com/appengine/articles/sharding_counters), which is basically having different tables for users (or usernames) so that you can read them all to check for uniqueness (App Engine is good at doing many reads per second) and have as much better write pace as tables for usernames you add.
More of sharding on writes here - https://developers.google.com/appengine/articles/scaling/contention
Hope it helps.

Categories

Resources