Named groups in Django based off model field? - regex

I was trying to replicate a url regex equivalent to:
url(r'^members/(?P<username>\w+)/$', 'profiles.views.single'),
While I understand I am taking the users that exist within my database from:
from django.contrib.auth.models import User
I am wondering if I can change this to something a little more unique. I've been reading the documentation from: https://docs.djangoproject.com/en/1.8/intro/tutorial04/ but am still lost.
My question is regarding whether or not I can use data from a model as the named group in the regex expression.
Example:
from django.db import models
from django.contrib.auth.models import User
class Address(models.Model):
user = models.ForeignKey(User)
unique_code = models.IntegerField(max_length = 5)
active = models.BooleanField(default = True)
Is it somehow possible to use that 'unique_code' field as a possible url in my regex to map like:
url(r'^members/(?P<unique_code>\w+)/$', 'profiles.views.single'),
I was trying something like this, but it obviously didn't work:
def single(request, unique_id):
try:
uid = get_object_or_404(Address, unique_id=unique_id)
final = uid
except:
raise Http404
return render_to_response('single_user.html', locals(), context_instance=RequestContext(request))

Try to replace unique_id in your function with unique_code
def single(request, unique_code):
try:
uid = get_object_or_404(Address, unique_code=unique_code)
final = uid
except:
raise Http404
return render_to_response('single_user.html', locals(), context_instance=RequestContext(request))

Related

Django Form use of user id

The following code is working nicely:
class SelectTwoTeams(forms.Form):
campaignnoquery = UserSelection.objects.filter(user=349).order_by('-campaignno')[:1]
currentCampaignNo = campaignnoquery[0].campaignno
cantSelectTeams = UserSelection.objects.filter(campaignno=currentCampaignNo)
currentTeams = StraightredTeam.objects.filter(currentteam = 1).exclude(teamid__in=cantSelectTeams.values_list('teamselectionid', flat=True))
team_one = forms.ModelChoiceField(queryset = currentTeams)
team_two = forms.ModelChoiceField(queryset = currentTeams)
However, you can see that the user id is currently hardcoded into the filter as 349. I would like this to be the id of the user logged in. I know in the view I can use:
currentUser = request.user
currentUserID = currentUser.id
But this code does not work within the forms section. If anyone could point me in the correct direction that would be ideal.
When I follow the suggestion below using the following form I get an error saying: NameError: name 'currentUserID' is not defined
# coding=utf-8
from dwad.threadlocals import get_current_user
from django.db.models import Max
from django import forms
from straightred.models import StraightredTeam
from straightred.models import UserSelection
class SelectTwoTeams(forms.Form):
def save(self):
currentUser = get_current_user()
currentUserID = currentUser.id
campaignnoquery = UserSelection.objects.filter(user=currentUserID).order_by('-campaignno')[:1]
currentCampaignNo = campaignnoquery[0].campaignno
cantSelectTeams = UserSelection.objects.filter(campaignno=currentCampaignNo)
currentTeams = StraightredTeam.objects.filter(currentteam = 1).exclude(teamid__in=cantSelectTeams.values_list('teamselectionid', flat=True))
team_one = forms.ModelChoiceField(queryset = currentTeams)
team_two = forms.ModelChoiceField(queryset = currentTeams)
Many thanks, Alan.
One method is to use local.threading. I have used this solution on a number of Django installations to good use.
I know there are a number of different opinions whether this is a good or bad solution. I tend to fall into the category that it can be extremely good in the right circumstances.
To set it up, create a file called threadlocals.py:
try:
from threading import local
except ImportError:
from django.utils._threading_local import local
_thread_locals = local()
def get_current_user():
return getattr(_thread_locals, 'user', None)
class ThreadLocalsMiddleware(object):
def process_request(self, request):
_thread_locals.user = getattr(request, 'user', None)
Then, add this ThreadLocalsMiddleware class to your project's middleware in settings.py:
MIDDLEWARE_CLASSES = [
...
'myproject.threadlocals.ThreadLocalsMiddleware',
...
]
Now, all you need to do is call the method get_current_user() from anywhere in your project.
from myproject.threadlocals import get_current_user
class SelectTwoTeams(forms.Form):
def save(self):
# for example:
currentUser = get_current_user()
currentUserID = currentUser.id
Found this answer at Reddit.
It is very simple and it is working good in my case.
In your view you have to include some code like this:
if form.is_valid():
obj = form.save(commit=False)
obj.user = request.user # logged in user is available on a view func's `request` instance
obj.save() # safe to save w/ user in tow

Slugfield URL implementation in Django

So, I am having some difficulty trying to slugify a title field in my model and still have it return the proper information.
Currently, a user can follow the url, if the list in their account exists under this regular expression:
url(r'^user/(?P<username>\w+)/list/(?P<listname>\w+)/$', mylistpage, name='lists'),
The issue I face is that the user can have a list containing spaces, but the regex bases their url off their list name. I am wanting to implement a slug url, but still have it retrieve the correct model/object information.
I am trying to have a slug field and then pre-populate it based on the list name, but I am lost at how this implementation is supposed to work. Much appreciation in advance from any insight.
Model
class newlist(models.Model):
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100,)
picture = models.ImageField(upload_to='profiles/', default = "/media/profiles/default.jpg")
slugurl = models.SlugField(default = slugurl(self))
def __str__(self):
return self.list_name
def slugurl(self):
return slugify(self.list_name)
Views
def mylistpage(request, username, listname):
context = RequestContext(request)
#make sure that the user is authenticated
if username == request.user.username:
#If the user is authenticated, then perform the following functions to the page
if request.user.is_authenticated():
#Store the current user request object into a variable
user = User.objects.get(username=username)
#Store the list name to the item that starts with the url input
listname = request.user.newlist_set.filter(list_name__iexact=listname)
listitems = request.user.newlist_set.all()
if not listname:
return redirect('/notfound')
else:
return redirect('/notfound')
return render_to_response('listview.html', {'lista': listname}, context)
I have used django-autoslug to great success. You can find a live example here.
SlugField is just a char field with a little syntactic sugar.
You will want to name your slug just slug so django can find it automatically in the URL resolution and passes the right parameter to views.
Your amended code would look like:
from autoslug import AutoSlugField
from django.db import models
class Newlist(models.Model): # Classes start with uppercase names by default
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100,)
picture = models.ImageField(upload_to='profiles/', default = "/media/profiles/default.jpg")
slug = AutoSlugField(populate_from='list_name')
def __str__(self):
return self.list_name
Your View:
def mylistpage(request,username, slug):
context = RequestContext(request)
#make sure that the user is authenticated
if username == request.user.username:
#If the user is authenticated, then perform the following functions to the page
if request.user.is_authenticated():
#Store the current user request object into a variable
user = User.objects.get(username=username)
#Store the list name to the item that starts with the url input
listname = request.user.newlist_set.filter(slug=slug)
listitems = request.user.newlist_set.all()
if not listname:
return redirect('/notfound')
else:
return redirect('/notfound')
return render_to_response('listview.html', {'lista': listname}, context)
urls.py
url(r'^user/(?P<username>\w+)/list/(?P<slug>[\w-]+)/$', mylistpage, name='lists'),

Django serve get-requests

Hey guys I am a bit new in Django. What I want to achieve is a URL which I can access with a GET request from my app, passing some values along with it.
I have a UserProfile model in Django which has a oneToOneField relationship to User. I want to pass email with my GET request and find a Userinstance with this email, then I want to pass two more values which I want to compare with this Users UserProfile attributes. But I don't quite understand how to achieve this. Here is what I have:
in my views.py
def check(request):
try:
email = request.GET.get('email', '')
osusername = request.GET.get('osusername', '')
computername = request.GET.get('computername','')
except TypeError:
return HttpResponseBadRequest()
user = get_object_or_404(User.objects.filter(user__email=email)[0])
in my urls.py
urlpatterns = patterns('',
url(r'^check/$', 'myapp.views.check'),)
But how do I compare for instance computername with User.UserProfile.computername of that user? No matter how I write it its wrong.
My UserProfile model as requested #comments:
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
computername = models.CharField("Computername", max_length=150, blank=False)
osusername = models.CharField("Osusername", max_length=150, blank=False)
So your syntax for get_object_or_404 is wrong. You don't pass it an object: it gets the object for you. So:
user = get_object_or_404(User, email=email)
Now you've got a User instance, and you want to get the relevant profile, so you can just do:
profile = user.userprofile
Alternatively it might be easier to grab the profile directly, if you don't need the actual user instance for anything else:
profile = get_object_or_404(UserProfile, user__email=email)
Now you can check the relevant attributes:
osusername == profile.osusername
computername == profile.computername
You need to retrieve the User instance first by:
try:
a_user = User.objects.get(email=email)
except User.DoesNotExist:
# error handling if the user does not exist
Then, get the corresponding UserProfile object by:
profile = a_user.userprofile
Then, you can get osusername and computername from the UserProfile object:
profile.osusername
profile.computername
As an addition to #daniel-roseman answer.
If checking the relevant attributes is a common task on multiple views it could also be worth creating a method in your UserProfile model which can perform the required validation check.
class UserProfile(object):
# various attributes ...
def check_machine_attributes(self, os_username, computer_name):
if os_username == self.osusername and computername == self.computername:
return True
return False
In your view you can then do:
if profile.check_machine_attributes(osusername, computername):
# ...
else:
# ...

Django: How to make a query for on object based on an M2M field (multiple selections for field on search form)

I need help coming up with an efficient way to do a search query for a set of objects, based on a M2M field. My search form is going to look something like Blue Cross Blue Shield's | eg: this image
Now, let's suppose my model looks like this:
# models.py
class Provider(models.Model)
title = models.CharField(max_length=150)
phone = PhoneNumberField()
services_offered = models.ManyToManyField(ServiceType)
def __unicode__(self):
return self.title
class ServiceCategory(models.Model):
service_category = models.CharField(max_length=30)
def __unicode__(self):
return self.service_category
class Meta(object):
verbose_name_plural = "Service Categories"
class ServiceType(models.Model):
service_type = models.CharField(max_length=30)
service_category = models.ForeignKey(ServiceCategory)
def __unicode__(self):
return u'%s | %s' % (self.service_category, self.service_type
Also, we have to keep in mind that the options that we select are subject to change, since how they display on the form is dynamic (new ServiceCategories and ServiceTypes can be added at anytime). *How should I go about constructing a query for the Provider objects, given that a person using the search form can select multiple Services_Offered?*
This is currently my HIGHLY INEFFICIENT METHOD:
#managers.py
from health.providers.models import *
from django.db.models import Q
class Query:
def __init__(self):
self.provider_objects=Provider.objects.all()
self.provider_object=Provider.objects
self.service_object=ServiceType.objects
self.category_objects=ServiceCategory.objects.all()
def simple_search_Q(self, **kwargs): #matt's learning note: **kwargs passes any dictionary
return self.provider_objects.filter(
Q(services_offered__service_type__icontains=kwargs['service']),
Q(title__icontains=kwargs['title']),
Q(state=kwargs['state']),
).distinct().order_by('title')
====================
#views.py
from django.shortcuts import render_to_response
from health.providers.models import *
from health.search.forms import *
from health.search.managers import Query #location of the query sets
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.template import RequestContext
def simple_search(request):
if request.method == 'POST':
SimpleSearch_form = SimpleSearch(request.POST)
if SimpleSearch_form.is_valid():
request.session["provider_list"] = None
kwargs = {'title': request.POST['title'],
'service': request.POST['service'], 'state': request.POST['state'] }
provider_list = Query().simple_search_Q(**kwargs)
return pagination_results(request, provider_list)
else:
SimpleSearch_form = SimpleSearch()
return render_to_response('../templates/index.html', { 'SimpleSearch_form': SimpleSearch_form},
context_instance=RequestContext(request))
How can I make my query:
Obtain Provider objects based on selecting multiple request.POST['service']
More efficient
Thanks for any help in advanced.
Best Regards,
Matt
1: for multiple request.POST['service'], I assume you mean these are CheckBoxes.
I'd make the CheckBox values ID's, not names, and do a PK lookup.
'services_offered__pk__in': request.POST.getlist('service')
That would return all Provider objects that have ALL of the services selected.
PS: You are also using CapitalCase for instances which is very confusing. If you want your code to be readable, I highly recommend some changes to your style (don't use CapitalCase for instances or variables) and make your variables more descriptive.
SimpleSearch_form = SimpleSearch() # what is SimpleSearch?
simplesearch_form = SimpleSearchForm() # now, it's very clear what the class SimpleSearchForm is
# and the form instance is clearly a for instance.
2: making it more efficient? You could get rid of a lot of code and code separation by remove your whole Query class. Also, I don't know why you are using Q objects since you are not doing anything that would require it (like OR or OR + AND).
def simple_search(request):
if request.method == 'POST':
searchform = SimpleSearchForm(request.POST)
if searchform.is_valid():
request.session['provider_list'] = None
post = request.POST
providers = Provider.objects.filter(services_offered__pk__in=post.getlist('services'),
title=post['title'], state=post['state'])
return pagination_results(request, provider_list)
else:
searchform = SimpleSearchForm()
return direct_to_template(request, '../templates/index.html', { 'searchform': searchform})

Django pagination (get page no. corresponding to the object)

I have a paginate I am trying to get the index page from an object page (sort of pagination in reverse)
The get_paginated_posts returns a paginator for the model Post:
class PostManager(models.Manager):
def get_paginated_posts(self, request=None):
if request and request.user.has_perm('blog.change_post'):
posts = super(PostManager, self).filter(is_update=False)
else:
posts = super(PostManager, self).filter(publish=True, is_update=False)
return Paginator(posts, POSTS_PER_PAGE)
.
.
This is my model
class Post(models.Model):
.
.
.
def get_page(self, request=None):
paginator = Post.objects.get_paginated_posts(request)
for i in range(1, paginator.num_pages+1):
if self in paginator.page(i).object_list:
return i
pass
return False
My concern is the Post.objects.get_paginated_posts call in the get_page function.
Is it right to call Post class from an instance? Is there any other better way to do this possible?
Why cannot I call super(Post, self).objects.get_paginated_posts to do the same?
I understand that self.objects.get_paginated_posts wont work because of absent access for the object to its manager.
Solved
Final code as suggested by Tomasz Elendt:
class PostManager(models.Manager):
def get_paginated_posts(self, user=None):
if user and user.has_perm('blog.change_post'):
posts = super(PostManager, self).filter(is_update=False)
else:
posts = super(PostManager, self).filter(publish=True, is_update=False)
return Paginator(posts, POSTS_PER_PAGE)
class Post(models.Model):
.
def get_page(self, request=None):
return self._default_manager.filter(is_update = False, time__gt=self.time).count()/POSTS_PER_PAGE +1
#Just a one line now :P
It's not the best idea what you're doing. Try to imagine how many queries it'll be translated to -- in the worst case you'd need to retrieve all user's posts from database!
I assume that you have some predefined ordering in your Post model (the one that Paginator uses). Use that to obtain the number of user's posts that precede that specific post record. If you divide that number by the POSTS_PER_PAGE value you'll get your page number.
IMHO using PostManager in Post methods is ok. What's not ok is that you're passing request object to it while I think you should use user_id for that (and permission checking should be really part of a view logic).
EDIT: example
from django.db import models
from django.contrib.auth.models import User
POSTS_PER_PAGE = 10
class Post(models.Model):
"""
>>> from datetime import datetime, timedelta
>>> from django.db import connection
>>> from django.conf import settings
>>>
>>> user = User.objects.create_user("test", "test#domain.com")
>>> for i in xrange(100):
... p = Post.objects.create(author=user,
... pub_date=datetime.now() - timedelta(hours=i))
>>> post = Post.objects.all()[68]
>>> settings.DEBUG = True # monkey-patching settings - ugly
>>> connection.queries = [] # cleaning previous queries
>>> post.get_page()
7
>>> len(connection.queries) # print number of queries of `get_page` call
1
"""
pub_date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(User)
class Meta:
ordering = ["-pub_date"]
def get_page(self):
return self._default_manager.filter(author__id=self.author_id).filter(
pub_date__gt=self.pub_date).count() / POSTS_PER_PAGE + 1