In Django, how do you give forms in forms.py initial values based on information of the logged in user? - django-forms

I would like to create a form to edit user information. The form's default values will be based on currently registered information of the user logged in. For instance, the phone number field will initially have the user's current phone number.
I am aware of the "initial" attribute, but form objects in form.py cannot accept the request object as a parameter, so it cannot grab information from the logged in user.
I really appreciate your kind help.

Create an appropriate model form and pass it the model pulled from the database.

Related

Make a form View read-only for specific user groups from front-end in Odoo 8

Is it possible to change the attributes of form view for specific user groups in form view tag, like in form tag, readonly="{('user_group','=','some_group')}">
I need to make a form view read-only for s specific user group but only from front-end. Records are updated from code by that user belonging to that specific user group from back-end. and if i disable updating the records of that model by that user group in my security file, that user is not able to modify the records even from back-end.
Best way to do this is by defining a new group that have only read access on that model and add to that user you will save a lot of typing and a lot of your time.
Because what you really asking is to remove edit write for a specific user.

Apps activities - uniquely identify user

Is there any way how to uniquely identify user who caused an event? I want to extract all events from Appsactivity service, which belongs to specific user.
The problem is, that service.activities().list() returns also activities of other users of shared file, even if this request has set userId which indicates the user to return activity for. It returns all visible activities to given user and therefore it contains activities of other users.
I tried to filter list, but it seems to be impossible - events contains simple User object which does not have userId or userEmail.
One way is to compare user's photo url which is avalaible in appactivity User object. Note, that this can be done only if url is not null, otherwise it won't uniquely identify user.

Linking logged in user to object data on Parse.com

I'm new to using Parse.com and I'm trying to understand the general relationship between a logged in user and user-specific data.
I've figured out and understand how to create users and objects but I'm fuzzy on how to connect the two.
Is it as simple as creating a user and then once their logged in, storing an object with their username as the key?
Then when a user signs in successfully, you retrieve the object under their username key?
I just want to make sure I'm approaching this from the right angle, since I plan on having a lot of users and I also want the most secure approach.
I've read through the Parse.com documentation but can't seem to find the connection between the two. Any help is appreciated!
Do you mean when the user submits any details it is recorded with their User ID? If so, then this code will work for you:
ParseUser user = ParseUser.getCurrentUser();
//yourObjectID.put("User", user);
There is no user-specific data (all data is global with respect to the app ID you registered, as Parse is a database), but you can store data inside a ParseUser object. You can also give it access controls (an ACL), so only that user can read/write it. When the user signs in successfully, I don't believe it will be part of the ParseUser object yet, you need to fetch the data. (This is definitely true for object fields, but I'm not sure about simple fields like strings and ints. It deserves testing.)
There is a caveat to this. Depending on which SDK you're using, some of that information may be cached. In Unity 3D, for instance, the ParseUser object will retain all its data between program invocations (and indeed, will remain logged in).

Joomla 2.5 - custom registration field not remembering values

Im using custom profile plugin found at http://library.logicsistemi.it/images/joomla/plg_user_testprofile.zip
I have created several fields, both, optional and required and they all work. But there is a tiny problem.
When I enter some value in that custom fields and submit the form it can happen that some fields dont pass validation (which is good). However values entered are cleared and I must fill all the fields from the beggining.
Demo: http://goo.gl/eH1G2
Enter some (not all) data under the 'legend' named User Registration
Enter some (not all) data under the 'legend' named Company
Informations
Press Register button
Error message will pop in. -
Data entered in the User Registration will be saved
Data entered in the Company Informations (which are custom fields)
will not be saved.
If all required fields are entered, form will submit data to database
(which is ok).
I want that data in the Company Informations are saved, so users dont have to type in all over again.
The problem in your demo is on the template you are using.
For some reason it gives Javascript errors. These errors blocks the Javascript client side validation and your form is submitted with invalid data.
If you want only server side validation you can modify the onContentPrepareData function in your plugin by loading posted information from the user state. To do this you have to permeform a different action when user_id is not set under "Merge the profile data." comment.
I think next week I will write a new tutorial on http://library.logicsistemi.it to explain this. For now try to fix your Javascript code.
Regards

.NET MVC 3 Validation

I use Entity framework for creating model.
I have table hierarchy where User is my base table and I have tables Lecturer and Student which have some specific data in them. Entity framework made model that it isn't suitable so I made my middle layer called modelview where I have student table which wraps all data from both user and student tables. I have made CRUD functionality for students but I only want that admin can create student with some initial password, than admin should not have option to change student password at Edit action.
The problem is that password is required field at student Create action so I set [Required] attribute to it, but at Edit I must not have password field and my ModelState.IsValid is then always false since password field in this case isn't present and therefore is null.
I thought on adding hidden password field, but that would be very bad since someone could look at page source and see password value.
Can I somehow once this field required and another time not required?
If you have any other idea that might help please share with me.
When a user is being edited, you can put in a placeholder hidden field, with the value * (or something like that). This will satisfy the Required attribute - you just have to make sure you don't update the password when the user is edited :)
Maybe you could randomly generate a password and not have it be required at all.
Or you could remove the requred attribute and manually check if it's present at the serverside.
if (string.IsNullOrEmpty(user.Password))
ModelState.AddModelError("Password","A password is required");
To validate clientside, if you're using jquery validation: JQuery Docs
If you have separate views for Student addition and editing, an alternative solution would be:
Create a StudentViewModel class which contains all the properties required for both Student addition and editing, and
Create a StudentAdditionViewModel class (optionally derived from StudentViewModel) which includes a Password property.
You can then use StudentAdditionViewModel as the Add view's model, and StudentViewModel as the edit view's model :)

Resources