Extending an existing web2py database - python

I have an existing web2py application. Now I need to create a new registration form using a db Table that includes a Field that requires a Row from a different Table.
This should be similar to what you commonly see with Country Fields in registration forms, except I want people to be able to add values to the 'Country' Table if the value doesn't already exist.

Making a small improvement to the previous response:
# create auth
auth = Auth(db)
# create the country table
db.define_table('country',
Field('name'),
Field('desc'),
format = '%(name)s')
# say you want to add it to auth_user table (not yet created)
auth.settings.extra_fields['auth_user']=[Field('country','reference country')]
# ask auth to make the auth tables, including auth_user
auth.define_tables()
JMax is right. We are more responsive on the web2py mailing list.

You can use a one to many relation (cf. the book):
db.define_table('country',
Field('name'),
Field('desc'))
db.define_table('user',
Field('name'),
Field('origin'), db.country))
Btw, you can ask your questions on the web2py Googlegroup where Massimo will probably be more reactive

Related

How to add to google app engine flask website the user registration ability if it wasn't scheduled in application architecture

I created website-app for myself and it become really useful and many peoples wont also use it. But user management was not scheduled in application architecture. Is there any way to easy add user registration so each user would have his own google database tables but with same name?
example of one "table":
class Settings(db.Model):
email = db.StringProperty()
link = db.LinkProperty()
rating = db.StringProperty()
How can I separate data from this "table" between different users? I search for some kind of wrapper so I don't need to change current architecture.
You have to remember there is no concept of tables with the datastore so you can't have a separate set of tables for each user as such.
You have a few choices, the two I would investigate are
create a separate app with the existing code base and each user runs their own site. You may not need to do any code changes at all
If you want complete separation of data for each user in a single app then look at namespaces (thats how multi-tenancy is normally implemented.)
However you haven't really provided a clear definition of how you want to separate the users etc.. so there a probably other approaches you can take.
Short of having a copy of the database for each user; you'll have to implement some code changes.
The easiest one you can do is add a foreign key to your data tables that points to your user table; then you filter all records based on this foreign key.
Once you have that change, you can write a view decorator that will automatically filter the records for you.

Simple REST API not based on a particular predefined model

Well, I do my first steps with Django and Django REST framework. The problem I face is that all examples throughout the whole Internet are based on hard-coded models. But the whole concept of models frustrates me a little bit, because I'm used to deal with different data which comes from numerous sources (various relational databases and nosql - all that stuff). So, I do not want to stick to a particular model with a fixed number of predefined fields, but I want to specify them just at the moment when a user goes to a particular page of my app.
Let's say I have a table or a collection in one of my databases, which stores information about users - it has any kinds of fields (not just email, name and likewise - all those fields as in all those examples throughout the web). So when a user goes to /users/ I connect to my datebase, get my table, set my cursor and populate my resultant dictionary with all rows and all fields I need. And REST API does all the rest.
So, I need a "first-step" example wich starts from data, not from a model: you have a table "items" in your favorite database, when a user goes to /items/, he or she gets all data from that table. To make such simplistic api, you should do this and this... I need this kind of example.
I think the key is to use the models differently. If you use onetomany or foreignkey references in your model construction you can more dynamically link different types of data together, then access that from the parent object.
For example, for your user, you could create a basic user model and reference that in many other models such as interests, occupation, and have those models store very dynamic data.
When you have the root user model object, you can access it's foreign key objects by either iterating through the dictionary of fields returned by the object or accessing the foreign key references directly with model.reference_set.all()

Adding field to auth_user table that can have only selective values

I am creating a Web2py application for a retail store. The requirement is that I use the auth_user table and there are two types of Users - 'Normal' and 'Admin'.
My question is how do I add a field to the auth_user table which has the constraint that it can contain only two values? What I would want ideally is that when the login page is rendered using SQLLFORM, the field for User_type should appear as a dropdown containing the two values (i.e, Normal and Admin).
I have googled it and could not find anything satisfactory. Any help would be awesome. :)
Thanks in advance. :)
After defining the auth object but before calling auth.define_tables, do something like:
auth.settings.extra_fields['auth_user'] = [
Field('role', requires=IS_IN_SET(['Normal', 'Admin']))]
For more details, see the documentation on customizing Auth and the IS_IN_SET validator.

significance of Django Default tables

Django comes with default tables like AuthGroup, AuthGroupPermissions , AuthPermission, AuthUser, AuthUserGroups, AuthUserUserPermissions, DjangoAdminLog, DjangoContentType, DjangoSession and DjangoSite. What is the significance of each table?
I know that these tables comes from the apps included in the settings.py file, but I really dont understand the need to use some of the following tables above, such as Permissions and Groups. Where will I exactly use these tables?
The significance of the tables:
AuthGroup: Contains your groups, just id and name
AuthPermission: Contains the permissions of your project id, codename and a ForeignKey to the ContentType (Model) they belong to
AuthGroupPermissions: Table to keep the many to many relation between AuthGroup and AuthPermission (which permissions each group has)
AuthUser: Your users - username is the primary key
AuthUserGroups: Table to keep the many to many relation between AuthGroup and
AuthUser (which users belong to each group)
AuthUserPermissions: Table to keep the many to many relation between AuthUser and AuthPermission (which permissions each user has)
DjangoAdminLog: Records actions (insert/delete/update) your admin users do
DjangoContentType: Contains the content types of your project -- a content type is actually a Model in general - more info here https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
DjangoSession: Contains session information (session key, data and when it expires), more info here https://docs.djangoproject.com/en/dev/topics/http/sessions/
DjangoSite: Contains the sites your application can be used on - more information here: https://docs.djangoproject.com/en/dev/ref/contrib/sites/
Now, you answer your other question, you don't actually need to use these tables yourself. You will use the django ORM to create Users, Groups, Permissions etc and these tables will be updated through the ORM.

Django custom user VS user profile

I'm currently using Django 1.5.1 and using a custom user as described in the official documentation. I realized everything is stored under one table, the auth_user one.
My question is, why is it better to have everything in one big table, instead of having 2 tables like it used to be prior to 1.5 by using a user_profile table for all additional data? It seems smarter the way it used to be, in case we want to add 20 new fields for information about the user, it is weird to have everything in auth_user.
In my case, for now I have class MyUser(AbstractUser) with 2 additional fields gender and date_of_birth, so it's all good with this, but now I would like to have many other information (text fields) like "favorite movies", "favorite books", "hobbies", "5 things I could not live without", etc. etc., to have way more information about my user. So I was just wondering if I should put that under MyUser class, or should I define a UserProfile one? And why?
Thanks!
When you have it all in one table, then database access is faster. With the old way you had to join on the auxiliary table to get all the information of the user.
Usually when you see a One-to-One relation, it would be better just to merge them in one table.
But the new custom User model solves also another problem, which is what atributes a User should have? What attributes are essential for your application? Is an email required? Should the email be also the username with which a user logs in?
You couldn't do these stuff before this feature was introduced.
Regarding your question about where to put additional user information like "hobbies" and such, it really depends on how often you will query/need this attributes. Are they gonna be only on the user's profile page? Well then you could have them in a seperate table and there wouldn't be much problem or performance hit. Otherwise prefer to store them on the same table as the User.

Categories

Resources