How to use one view by two different users in django? - django

I have a custom user model as below:
class CustomUser(AbstractUser):
username = None
email = EmailField(verbose_name='email address', max_length=255, unique=True, )
first_name = CharField(verbose_name='First Name', max_length=30, null=True, )
middle_name = CharField(verbose_name='Middle Name', max_length=30, null=True, blank=True, )
last_name = CharField(verbose_name='Last Name', max_length=30, null=True, )
phone_number = CharField(verbose_name='Phone Number', max_length=30, null=True, )
is_partner = BooleanField(default=False, )
is_student = BooleanField(default=False, )
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
As shown above, a user can be a student or can be a partner, who will be in charge of a few students and have access to their assets.
The partner model is as below:
class Partner(Model):
user = OneToOneField(CustomUser, on_delete=CASCADE)
partner_name = CharField(max_length=100, )
The student model is as below:
class Student(Model):
user = OneToOneField(CustomUser, on_delete=CASCADE)
student_name = CharField(max_length=100, )
partner = ForeignKey(Partner, on_delete=SET_NULL, null=True)
As shown, in the Student model, partner can be null. The reason is that a student can either directly enroll in the system without having a partner or he/she can be enrolled by a partner. A student might even specify a partner that is optional when he/she is enrolling.
I have a student view and url pattern:
path('student_profile/', student_profile, name='student_profile')
def student_profile(request):
user = request.user
student = get_object_or_404(Student, user=user)
return HttpResponse('The student will do AAAAAA')
I have a url pattern and partner view:
path('partner_profile/', partner_profile, name='partner_profile')
def partner_profile(request):
user = request.user
partner = get_object_or_404(Partner, user=user)
return HttpResponse('The partner will do some stuff such as updating his/her profile')
there is this url pattern and view that allowes a partner to see his/her list of student:
path('partners/<int:partner_pk>/students/', partner_students, name='partner_students')
def partner_students(request):
template_name = 'partners/partner_students.html'
context = {}
partner = get_object_or_404(Partner, user=request.user)
partner_students = Student.objects.filter(partner=partner)
context.update({'partner_students': partner_students})
return render(request, template_name, context)
in template, I can have this url path:
path('students/<int:student_pk>/', partner_student_profile, name='partner_student_profile')
{% for student in partner_students %}
student.pk
{% endfor %}
What I need is that somehow, instead of rewriting a view to do some stuff with the student profile, I use the one that is already written.
I want to use this:
def student_profile(request):
user = request.user
student = get_object_or_404(Student, user=user)
return HttpResponse('The student will do some stuff such as updating his/her profile')
I do not want to write this:
def partner_student_profile(request, student_pk):
student = Student.objects.get(pk=student_pk)
return HttpResponse('The partner will do AAAAAA for his/her student')
I want to reuse the already written "student_profile" view

Have a url like this.
path('profile/<str:profile_type>/<int:pk>', profile_view, name='profile_view')
Now create a view to map to this url.
def profile_view(request, profile_type: str, pk: int):
user = request.user
if profile_type == 'partner':
"""
do something
"""
template_name = 'partner_handler.html'
context = dict()
elif profile_type == 'student':
"""
do something
"""
template_name = 'student_handler.html'
context = dict()
return render(request, template_name, context)
So basically, you are using two url path parameters to check what type of profile you want to work with, in your case it's going to be a student or partner. Depending on the profile type you can choose your own template and build your own context to pass to the chosen template.
Hope it helps!

Related

How to filter post objects in django according to a particular user model field?

I'm currently creating a Social platform with Django. Right now, I'm developing homepage and want to show posts matching a userfield.
This is my Post model:
class Discussion(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Account, on_delete=models.CASCADE)
post = models.TextField()
date = models.DateTimeField(default=timezone.now)
This is user account model:
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name='email', max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
classid = models.CharField(max_length=10)
This is view:
#login_required(login_url='login')
def home(request):
if request.method == 'GET':
discussions = Discussion.objects.get(post=Account.classid).order_by('-date')
form = PostDiscussionForm()
return render(request, 'app/home.html', {'discussions':discussions,'form':form})
else:
form = PostDiscussionForm(request.POST)
newdisscussion = form.save(commit=False)
newdisscussion.author = request.user
newdisscussion.save()
return redirect('home')
I want to show only logged users matching their classid (from user account model)
What I gather from here, is that you want to show which users are currently online/logged in? For that you'll need to store additional information when logging like, add a boolean field in the user model indicating whether or not the user has logged in and then filter based on that field.
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name='email', max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
logged_in = models.BooleanField(default=False)
classid = models.CharField(max_length=10)
Now add additional logic which sets this field to true when the user eventually logs in from your /login route.
Then Filter using:
$ return Account.objects.filter(logged_in==True)
This will return you all users that have currently logged in, if you're not using any serializers, than use value_list for getting the values only rather than the whole user objects.
$ return Account.objects.filter(logged_in==True).values_list('classid', flat=True)

"'int' object has no attribute 'save'" Update models from views.py

I want to update user model in a views.py. Is there a way to do that? What I want to do is minus user point in views.py and update into models without a form. but it gave me " 'int' object has no attribute 'save'"
views.py
def buy_item(request, item_name):
item = ShopItem.objects.get(item_name=item_name)
price = item.item_price
user = request.user
user_point = user.point
if user_point >= price:
user_point = user_point - price
point_left = user_point
point_left.save()
msg = messages.success(request, 'You bought item!')
return redirect('/shop', msg)
else:
msg = messages.success(request, 'You do not have enough point')
context = ({
'point': user_point,
'item': item,
'form': form
})
return render(request, 'item.html', context)
item models.py
class ShopItem(models.Model):
item_name = models.CharField(max_length=16)
item_image = models.ImageField(upload_to='media_items')
item_description = models.CharField(max_length=255)
item_command = models.CharField(max_length=255)
item_price = models.IntegerField(default=0)
def __str__(self):
return self.item_name
def snippet(self):
return self.item_description[:45]
user models.py
class UserProfile(AbstractUser):
username = models.CharField(max_length=16, unique=True)
email = models.EmailField(default='', unique=True)
phone = models.CharField(max_length=10)
point = models.IntegerField(default=0)
password1 = models.CharField(max_length=255)
password2 = models.CharField(max_length=255)
USERNAME_FIELD = 'username'
What I want is to save the point after changed.
What should I do? Please help! I'm really new to this. Sorry for my English.
Sadly, I don't have enough reputation to make a comment, but you need to use save() method in a model, not in an Integer.
So, you should replace
point_left = user_point
point_left.save()
for this
user.point = user_point # Assign the new point to the user
user.save() # Update your user model

How to intercept and control saving a Django POST form?

When the user is required to fill his profile, he picks a city from the Google Places Autocomplete and posts the form, in the view I extract the city Id from the Google API based on the posted text (I use the same id as pk in my db) and try to extract a city from my db.
These are the models:
class City(models.Model):
#extracted from the Google API
city_id = models.CharField(primary_key=True, max_length=150)
name = models.CharField(max_length=128, blank=True)
country = models.CharField(max_length=128, blank=True)
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile', primary_key=True)
city = models.ForeignKey(City, blank=True, null=True)
prof_pic = models.ImageField(blank=True, upload_to='profile_pictures')
This is the view:
def createprofile(request):
if request.method =='POST':
user = User.objects.get(username=request.user.username)
user_form = UserForm(data=request.POST, instance=user)
profile_form = UserProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.save()
profile = profile_form.save(commit=False)
profile.user = user
#brings back the city search result as text
searched_city = request.POST['city']
#brings back city ID from the Google API
searched_city_id = population_script.get_city_json(searched_city.replace(" ", ""))['results'][0]['id']
#If it's a valid city
if searched_city_id != -1:
city = City.objects.get(city_id = searched_city_id)
profile.city = city#this is what I want to happen!
else:
return HttpResponse("There's no such city, please try a different query.")
if 'prof_pic' in request.FILES:#now save the profile pic
profile.prof_pic = request.FILES['prof_pic']
print("PROF PIC IS: " + profile.prof_pic.url)
else:
profile.prof_pic = 'images/anon.png'
profile.save()
if 'next' in request.GET:
return redirect(request.GET['next'])
else:
print (user_form.errors, profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render(request,
'excurj/createprofile.html', {'user_form':user_form, 'profile_form':profile_form})
However, I keep receiving an error that what's been posted is just text while the city needs to be a City object. I can save the profile pic ok though.
Cannot assign "'Dubai - United Arab Emirates'": "UserProfile.city"
must be a "City" instance.
edit: these are the forms:
class UserForm(forms.ModelForm):
first_name = forms.CharField(
label = "First Name:",
max_length = 80,
required = True
)
last_name = forms.CharField(
label = "Last Name:",
max_length = 80,
required = True,
)
class Meta:
model = User
fields = ('first_name', 'last_name')
class UserProfileForm(forms.ModelForm):
city = forms.CharField(
label = "Your Current City:",
max_length = 200,
required = True,
)
class Meta:
model = UserProfile
fields = ('city','prof_pic', 'dob', 'sex', 'education', 'career', 'about_you',
'music_movies_books', )
Please provide a related_name to the city field in the UserProfile.
I worked around this by creating a new UserProfile field called city_search_text which saves the searched text thus it of course does not return any error. I then receive it in the POST request and comfortable pull the proper city in the view.
I handled a similar issue by overriding my forms' clean method. Something like the following will work:
def clean(self):
# fix city problem
if self.cleaned_data.get("city") is not None:
self.cleaned_data['city'] = City.objects.get(id=self.cleaned_data.get("city"))
return self.cleaned_data

Django decorator based on user type

i have the following model:
class Developer(models.Model):
COUNTRIES = (
('U', 'United States'),
('O', 'Other'),
)
user = models.OneToOneField(User, related_name='freelancer_user')
country = models.CharField(max_length=1, choices=COUNTRIES, default='U')
class Customer(models.Model):
user = models.OneToOneField(User, related_name='customer_user')
location = models.CharField(max_length=50, blank=True)
I have the following view:
#login_required
def postjob(request):
job_form = JobCreateForm()
return render(request, 'myapp/postjob.html', {'job_form': job_form})
This works as far as the authentication. But i want only a Customer user to access this view and not a developer. How do i do this?
You need the user_passes_test decorator and a test function:
#user_passes_test(is_customer)
def postjob(request):
job_form = JobCreateForm()
return render(request, 'myapp/postjob.html', {'job_form': job_form})
Where is_customer is something like:
def is_customer(user):
# how do you tell the user is a customer?
if user.is_staff or user.is_superuser:
return False
return True

How to filter a (or all) joined table's rows by the current logged in user in django?

My creation is a basic project about ticketing, users and assets. It's a typical application that companies have to keep a list of who has what and what issues are occurring.
So far I have (models):
**** Tickets ******************
class Ticket(models.Model):
category = models.ForeignKey('TicketCategory')
issue = models.CharField(max_length=100)
user = models.ForeignKey('Users', blank=True,null=True,related_name="tickets")
owner = models.ForeignKey(User)
**** Users *********************
class Users(models.Model):
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
email = models.CharField(max_length=100, blank=True)
business = models.ForeignKey('Business', blank=True, null=True,related_name="users")
owner = models.ForeignKey(User)
class Meta:
db_table = 'users'
verbose_name_plural = "users"
ordering = ["lastname"]
**** Assets *********************
class Assets(models.Model):
serial = models.CharField(unique=False, max_length=100)
brand = models.CharField(max_length=100)
user = models.ForeignKey('Users', blank=True, null=True, related_name="assets")
location = models.ForeignKey('AssetLocation', blank=False, null=False, related_name="assets")
owner = models.ForeignKey(User)
I have stripped them down a bit to exclude the useless info. There are others also like AssetsLocations, Categories etc., all of them in the same pattern - the owner is added in the end as a Foreign Key.
I have created some form of authentication, so every logged in user will have his own tickets, assets, and users (employees actually). Filtering is needed so the data of each user ONLY are displayed after every successful authentication.
I am using CBVs and override the get_queryset to enable filtering by the user currently logged in:
Views.py
class TicketList(ListView):
template_name = 'assets/ticket_list.html'
def get_queryset(self):
user = self.request.user
return Ticket.objects.filter(owner_id=user.id).select_related('user','asset','category')
def get_context_data(self, **kwargs):
context = super(TicketList, self).get_context_data(**kwargs)
context['totalTickets'] = self.get_queryset().count()
context['tickets'] = self.get_queryset().select_related('user','asset','category')
return context
Everything works successfully and only logged_in user's data are shown. Then I am trying to create a new Ticket:
class TicketCreate(CreateView):
fields = [ 'category', 'issue', 'user']
template_name = 'assets/ticket_form.html'
def get_queryset(self):
user = self.request.user
return Ticket.objects.filter(owner_id=user.id)
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.owner_id = self.request.user.id
self.object.save()
return HttpResponseRedirect(self.get_success_url())
success_url = reverse_lazy('ticket_list')
I am getting everything posted in the form template by using {{ form.category }}, {{ form.issue }} etc. The issue I am facing is that the drop-down boxes displayed in the form, for example the {{ form.user }} should be displaying only the users where user.owner = self.request.user.id, in simple words: the users that the owner created. Instead of it, all the users in the database are displayed.
Isn't it obvious which is the question :) ?