mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2024-11-22 07:11:52 +00:00
TRansX speedup for import + remove fossil profiles
This commit is contained in:
parent
fbf5daff0e
commit
070157eacb
@ -23,6 +23,7 @@ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
|
|||||||
print(" - settings on loading databaseReset.py")
|
print(" - settings on loading databaseReset.py")
|
||||||
|
|
||||||
import django
|
import django
|
||||||
|
|
||||||
print(" - Memory footprint before loading Django: {:.3f} MB".format(resource.getrusage(resource.RUSAGE_SELF)[2]/1024.0))
|
print(" - Memory footprint before loading Django: {:.3f} MB".format(resource.getrusage(resource.RUSAGE_SELF)[2]/1024.0))
|
||||||
try:
|
try:
|
||||||
django.setup()
|
django.setup()
|
||||||
@ -44,8 +45,7 @@ from django.contrib.auth.models import User
|
|||||||
from troggle.core.models import get_process_memory
|
from troggle.core.models import get_process_memory
|
||||||
from troggle.core.models_caves import Cave, Entrance
|
from troggle.core.models_caves import Cave, Entrance
|
||||||
from troggle.parsers.imports import import_caves, import_people, import_surveyscans, \
|
from troggle.parsers.imports import import_caves, import_people, import_surveyscans, \
|
||||||
import_logbooks, import_QMs, import_survex, import_loadpos, import_drawingsfiles, \
|
import_logbooks, import_QMs, import_survex, import_loadpos, import_drawingsfiles
|
||||||
import_subcaves
|
|
||||||
import troggle.logbooksdump
|
import troggle.logbooksdump
|
||||||
|
|
||||||
if os.geteuid() == 0:
|
if os.geteuid() == 0:
|
||||||
|
@ -7,6 +7,7 @@ from django.db import connection, close_old_connections, connections
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
#from django.urls import reverse, resolve
|
#from django.urls import reverse, resolve
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
import troggle.settings
|
import troggle.settings
|
||||||
import troggle.parsers.caves
|
import troggle.parsers.caves
|
||||||
@ -19,37 +20,47 @@ import troggle.parsers.subcaves
|
|||||||
def import_caves():
|
def import_caves():
|
||||||
print("-- Importing Caves to ",end="")
|
print("-- Importing Caves to ",end="")
|
||||||
print(django.db.connections.databases['default']['NAME'])
|
print(django.db.connections.databases['default']['NAME'])
|
||||||
troggle.parsers.caves.readcaves()
|
# wrap the entire import in a transaction
|
||||||
|
with transaction.atomic():
|
||||||
|
troggle.parsers.caves.readcaves()
|
||||||
|
|
||||||
def import_people():
|
def import_people():
|
||||||
print("-- Importing People (folk.csv) to ",end="")
|
print("-- Importing People (folk.csv) to ",end="")
|
||||||
print(django.db.connections.databases['default']['NAME'])
|
print(django.db.connections.databases['default']['NAME'])
|
||||||
troggle.parsers.people.LoadPersonsExpos()
|
with transaction.atomic():
|
||||||
|
troggle.parsers.people.LoadPersonsExpos()
|
||||||
|
|
||||||
def import_surveyscans():
|
def import_surveyscans():
|
||||||
print("-- Importing Survey Scans")
|
print("-- Importing Survey Scans")
|
||||||
troggle.parsers.surveys.LoadListScans()
|
with transaction.atomic():
|
||||||
|
troggle.parsers.surveys.LoadListScans()
|
||||||
|
|
||||||
def import_logbooks():
|
def import_logbooks():
|
||||||
print("-- Importing Logbooks")
|
print("-- Importing Logbooks")
|
||||||
troggle.parsers.logbooks.LoadLogbooks()
|
with transaction.atomic():
|
||||||
|
troggle.parsers.logbooks.LoadLogbooks()
|
||||||
|
|
||||||
def import_QMs():
|
def import_QMs():
|
||||||
print("-- Importing old QMs for 161, 204, 234 from CSV files")
|
print("-- Importing old QMs for 161, 204, 234 from CSV files")
|
||||||
troggle.parsers.QMs.Load_QMs()
|
with transaction.atomic():
|
||||||
|
troggle.parsers.QMs.Load_QMs()
|
||||||
|
|
||||||
def import_survex():
|
def import_survex():
|
||||||
# when this import is moved to the top with the rest it all crashes horribly
|
# when this import is moved to the top with the rest it all crashes horribly
|
||||||
import troggle.parsers.survex
|
with transaction.atomic():
|
||||||
|
import troggle.parsers.survex
|
||||||
print("-- Importing Survex and Entrance Positions")
|
print("-- Importing Survex and Entrance Positions")
|
||||||
print(" - Survex Blocks")
|
print(" - Survex Blocks")
|
||||||
troggle.parsers.survex.LoadSurvexBlocks()
|
with transaction.atomic():
|
||||||
|
troggle.parsers.survex.LoadSurvexBlocks()
|
||||||
print(" - Survex entrances x/y/z Positions")
|
print(" - Survex entrances x/y/z Positions")
|
||||||
troggle.parsers.survex.LoadPositions()
|
with transaction.atomic():
|
||||||
|
troggle.parsers.survex.LoadPositions()
|
||||||
|
|
||||||
def import_loadpos():
|
def import_loadpos():
|
||||||
# when this import is moved to the top with the rest it all crashes horribly
|
# when this import is moved to the top with the rest it all crashes horribly
|
||||||
import troggle.parsers.survex
|
with transaction.atomic():
|
||||||
|
import troggle.parsers.survex
|
||||||
print(" - Survex entrances x/y/z Positions")
|
print(" - Survex entrances x/y/z Positions")
|
||||||
troggle.parsers.survex.LoadPositions()
|
troggle.parsers.survex.LoadPositions()
|
||||||
|
|
||||||
|
@ -1,47 +1,49 @@
|
|||||||
"""
|
# # url(r'^profiles/', include('profiles.urls')), # not used ? Delete this entire app then.
|
||||||
URLConf for Django user profile management.
|
|
||||||
|
|
||||||
Recommended usage is to use a call to ``include()`` in your project's
|
# """
|
||||||
root URLConf to include this URLConf for any URL beginning with
|
# URLConf for Django user profile management.
|
||||||
'/profiles/'.
|
|
||||||
|
|
||||||
If the default behavior of the profile views is acceptable to you,
|
# Recommended usage is to use a call to ``include()`` in your project's
|
||||||
simply use a line like this in your root URLConf to set up the default
|
# root URLConf to include this URLConf for any URL beginning with
|
||||||
URLs for profiles::
|
# '/profiles/'.
|
||||||
|
|
||||||
(r'^profiles/', include('profiles.urls')),
|
# If the default behavior of the profile views is acceptable to you,
|
||||||
|
# simply use a line like this in your root URLConf to set up the default
|
||||||
|
# URLs for profiles::
|
||||||
|
|
||||||
But if you'd like to customize the behavior (e.g., by passing extra
|
# (r'^profiles/', include('profiles.urls')),
|
||||||
arguments to the various views) or split up the URLs, feel free to set
|
|
||||||
up your own URL patterns for these views instead. If you do, it's a
|
|
||||||
good idea to keep the name ``profiles_profile_detail`` for the pattern
|
|
||||||
which points to the ``profile_detail`` view, since several views use
|
|
||||||
``reverse()`` with that name to generate a default post-submission
|
|
||||||
redirect. If you don't use that name, remember to explicitly pass
|
|
||||||
``success_url`` to those views.
|
|
||||||
|
|
||||||
"""
|
# But if you'd like to customize the behavior (e.g., by passing extra
|
||||||
|
# arguments to the various views) or split up the URLs, feel free to set
|
||||||
|
# up your own URL patterns for these views instead. If you do, it's a
|
||||||
|
# good idea to keep the name ``profiles_profile_detail`` for the pattern
|
||||||
|
# which points to the ``profile_detail`` view, since several views use
|
||||||
|
# ``reverse()`` with that name to generate a default post-submission
|
||||||
|
# redirect. If you don't use that name, remember to explicitly pass
|
||||||
|
# ``success_url`` to those views.
|
||||||
|
|
||||||
from django.conf.urls import *
|
# """
|
||||||
from django.urls import *
|
|
||||||
|
|
||||||
from profiles import views
|
# from django.conf.urls import *
|
||||||
|
# from django.urls import *
|
||||||
|
|
||||||
|
# from profiles import views
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
# urlpatterns = [
|
||||||
url(r'^select/$',
|
# url(r'^select/$',
|
||||||
views.select_profile,
|
# views.select_profile,
|
||||||
name='profiles_select_profile'),
|
# name='profiles_select_profile'),
|
||||||
url(r'^create/$',
|
# url(r'^create/$',
|
||||||
views.create_profile,
|
# views.create_profile,
|
||||||
name='profiles_create_profile'),
|
# name='profiles_create_profile'),
|
||||||
url(r'^edit/$',
|
# url(r'^edit/$',
|
||||||
views.edit_profile,
|
# views.edit_profile,
|
||||||
name='profiles_edit_profile'),
|
# name='profiles_edit_profile'),
|
||||||
url(r'^(?P<username>\w+)/$',
|
# url(r'^(?P<username>\w+)/$',
|
||||||
views.profile_detail,
|
# views.profile_detail,
|
||||||
name='profiles_profile_detail'),
|
# name='profiles_profile_detail'),
|
||||||
url(r'^$',
|
# url(r'^$',
|
||||||
views.profile_list,
|
# views.profile_list,
|
||||||
name='profiles_profile_list'),
|
# name='profiles_profile_list'),
|
||||||
]
|
# ]
|
||||||
|
@ -1,52 +1,52 @@
|
|||||||
"""
|
# """
|
||||||
Utility functions for retrieving and generating forms for the
|
# Utility functions for retrieving and generating forms for the
|
||||||
site-specific user profile model specified in the
|
# site-specific user profile model specified in the
|
||||||
``AUTH_PROFILE_MODULE`` setting.
|
# ``AUTH_PROFILE_MODULE`` setting.
|
||||||
|
|
||||||
"""
|
# """
|
||||||
|
|
||||||
from django import forms
|
# from django import forms
|
||||||
from django.conf import settings
|
# from django.conf import settings
|
||||||
#from django.contrib.auth.models import SiteProfileNotAvailable
|
# #from django.contrib.auth.models import SiteProfileNotAvailable
|
||||||
|
|
||||||
try:
|
# try:
|
||||||
from django.contrib.auth.models import SiteProfileNotAvailable
|
# from django.contrib.auth.models import SiteProfileNotAvailable
|
||||||
except ImportError: # django >= 1.7
|
# except ImportError: # django >= 1.7
|
||||||
SiteProfileNotAvailable = type('SiteProfileNotAvailable', (Exception,), {})
|
# SiteProfileNotAvailable = type('SiteProfileNotAvailable', (Exception,), {})
|
||||||
|
|
||||||
from django.db import models
|
# from django.db import models
|
||||||
#from django.db.models import get_model
|
# #from django.db.models import get_model
|
||||||
|
|
||||||
|
|
||||||
def get_profile_model():
|
# def get_profile_model():
|
||||||
"""
|
# """
|
||||||
Return the model class for the currently-active user profile
|
# Return the model class for the currently-active user profile
|
||||||
model, as defined by the ``AUTH_PROFILE_MODULE`` setting. If that
|
# model, as defined by the ``AUTH_PROFILE_MODULE`` setting. If that
|
||||||
setting is missing, raise
|
# setting is missing, raise
|
||||||
``django.contrib.auth.models.SiteProfileNotAvailable``.
|
# ``django.contrib.auth.models.SiteProfileNotAvailable``.
|
||||||
|
|
||||||
"""
|
# """
|
||||||
if (not hasattr(settings, 'AUTH_PROFILE_MODULE')) or \
|
# if (not hasattr(settings, 'AUTH_PROFILE_MODULE')) or \
|
||||||
(not settings.AUTH_PROFILE_MODULE):
|
# (not settings.AUTH_PROFILE_MODULE):
|
||||||
raise SiteProfileNotAvailable
|
# raise SiteProfileNotAvailable
|
||||||
profile_mod = models.get_model(*settings.AUTH_PROFILE_MODULE.split('.'))
|
# profile_mod = models.get_model(*settings.AUTH_PROFILE_MODULE.split('.'))
|
||||||
if profile_mod is None:
|
# if profile_mod is None:
|
||||||
raise SiteProfileNotAvailable
|
# raise SiteProfileNotAvailable
|
||||||
return profile_mod
|
# return profile_mod
|
||||||
|
|
||||||
|
|
||||||
def get_profile_form():
|
# def get_profile_form():
|
||||||
"""
|
# """
|
||||||
Return a form class (a subclass of the default ``ModelForm``)
|
# Return a form class (a subclass of the default ``ModelForm``)
|
||||||
suitable for creating/editing instances of the site-specific user
|
# suitable for creating/editing instances of the site-specific user
|
||||||
profile model, as defined by the ``AUTH_PROFILE_MODULE``
|
# profile model, as defined by the ``AUTH_PROFILE_MODULE``
|
||||||
setting. If that setting is missing, raise
|
# setting. If that setting is missing, raise
|
||||||
``django.contrib.auth.models.SiteProfileNotAvailable``.
|
# ``django.contrib.auth.models.SiteProfileNotAvailable``.
|
||||||
|
|
||||||
"""
|
# """
|
||||||
profile_mod = get_profile_model()
|
# profile_mod = get_profile_model()
|
||||||
class _ProfileForm(forms.ModelForm):
|
# class _ProfileForm(forms.ModelForm):
|
||||||
class Meta:
|
# class Meta:
|
||||||
model = profile_mod
|
# model = profile_mod
|
||||||
exclude = ('user',) # User will be filled in by the view.
|
# exclude = ('user',) # User will be filled in by the view.
|
||||||
return _ProfileForm
|
# return _ProfileForm
|
||||||
|
@ -1,361 +1,361 @@
|
|||||||
"""
|
# """
|
||||||
Views for creating, editing and viewing site-specific user profiles.
|
# Views for creating, editing and viewing site-specific user profiles.
|
||||||
|
|
||||||
"""
|
# """
|
||||||
from django.contrib.auth.decorators import login_required
|
# from django.contrib.auth.decorators import login_required
|
||||||
from django.contrib.auth.models import User
|
# from django.contrib.auth.models import User
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
# from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.urls import reverse, resolve
|
# from django.urls import reverse, resolve
|
||||||
from django.http import Http404
|
# from django.http import Http404
|
||||||
from django.http import HttpResponseRedirect
|
# from django.http import HttpResponseRedirect
|
||||||
from django.shortcuts import get_object_or_404
|
# from django.shortcuts import get_object_or_404
|
||||||
from django.shortcuts import render_to_response
|
# from django.shortcuts import render_to_response
|
||||||
from django.template import RequestContext
|
# from django.template import RequestContext
|
||||||
from django.views.generic.list import ListView
|
# from django.views.generic.list import ListView
|
||||||
from django import forms
|
# from django import forms
|
||||||
|
|
||||||
from troggle.core.models import Person
|
# from troggle.core.models import Person
|
||||||
|
|
||||||
from profiles import utils
|
# from profiles import utils
|
||||||
|
|
||||||
from django.conf import settings
|
# from django.conf import settings
|
||||||
|
|
||||||
class SelectPersonForm(forms.Form): #This and the select_profile view
|
# class SelectPersonForm(forms.Form): #This and the select_profile view
|
||||||
person = forms.ModelChoiceField(queryset=Person.objects.all())
|
# person = forms.ModelChoiceField(queryset=Person.objects.all())
|
||||||
|
|
||||||
def select_profile(request):
|
# def select_profile(request):
|
||||||
|
|
||||||
if request.method == 'POST':
|
# if request.method == 'POST':
|
||||||
form = SelectPersonForm(request.POST)
|
# form = SelectPersonForm(request.POST)
|
||||||
if form.is_valid():
|
# if form.is_valid():
|
||||||
profile_obj=form.cleaned_data['person']
|
# profile_obj=form.cleaned_data['person']
|
||||||
profile_obj.user=request.user
|
# profile_obj.user=request.user
|
||||||
profile_obj.save()
|
# profile_obj.save()
|
||||||
return HttpResponseRedirect(profile_obj.get_absolute_url())
|
# return HttpResponseRedirect(profile_obj.get_absolute_url())
|
||||||
else:
|
# else:
|
||||||
form = SelectPersonForm()
|
# form = SelectPersonForm()
|
||||||
context = RequestContext(request)
|
# context = RequestContext(request)
|
||||||
return render_to_response('profiles/select_profile.html', {
|
# return render_to_response('profiles/select_profile.html', {
|
||||||
'form':form,},
|
# 'form':form,},
|
||||||
context_instance=context
|
# context_instance=context
|
||||||
)
|
# )
|
||||||
|
|
||||||
|
|
||||||
def create_profile(request, form_class=None, success_url=None,
|
# def create_profile(request, form_class=None, success_url=None,
|
||||||
template_name='profiles/create_profile.html',
|
# template_name='profiles/create_profile.html',
|
||||||
extra_context=None):
|
# extra_context=None):
|
||||||
"""
|
# """
|
||||||
Create a profile for the current user, if one doesn't already
|
# Create a profile for the current user, if one doesn't already
|
||||||
exist.
|
# exist.
|
||||||
|
|
||||||
If the user already has a profile, as determined by
|
# If the user already has a profile, as determined by
|
||||||
``request.user.get_profile()``, a redirect will be issued to the
|
# ``request.user.get_profile()``, a redirect will be issued to the
|
||||||
:view:`profiles.views.edit_profile` view. If no profile model has
|
# :view:`profiles.views.edit_profile` view. If no profile model has
|
||||||
been specified in the ``AUTH_PROFILE_MODULE`` setting,
|
# been specified in the ``AUTH_PROFILE_MODULE`` setting,
|
||||||
``django.contrib.auth.models.SiteProfileNotAvailable`` will be
|
# ``django.contrib.auth.models.SiteProfileNotAvailable`` will be
|
||||||
raised.
|
# raised.
|
||||||
|
|
||||||
**Optional arguments:**
|
# **Optional arguments:**
|
||||||
|
|
||||||
``extra_context``
|
# ``extra_context``
|
||||||
A dictionary of variables to add to the template context. Any
|
# A dictionary of variables to add to the template context. Any
|
||||||
callable object in this dictionary will be called to produce
|
# callable object in this dictionary will be called to produce
|
||||||
the end result which appears in the context.
|
# the end result which appears in the context.
|
||||||
|
|
||||||
``form_class``
|
# ``form_class``
|
||||||
The form class to use for validating and creating the user
|
# The form class to use for validating and creating the user
|
||||||
profile. This form class must define a method named
|
# profile. This form class must define a method named
|
||||||
``save()``, implementing the same argument signature as the
|
# ``save()``, implementing the same argument signature as the
|
||||||
``save()`` method of a standard Django ``ModelForm`` (this
|
# ``save()`` method of a standard Django ``ModelForm`` (this
|
||||||
view will call ``save(commit=False)`` to obtain the profile
|
# view will call ``save(commit=False)`` to obtain the profile
|
||||||
object, and fill in the user before the final save). If the
|
# object, and fill in the user before the final save). If the
|
||||||
profile object includes many-to-many relations, the convention
|
# profile object includes many-to-many relations, the convention
|
||||||
established by ``ModelForm`` of using a method named
|
# established by ``ModelForm`` of using a method named
|
||||||
``save_m2m()`` will be used, and so your form class should
|
# ``save_m2m()`` will be used, and so your form class should
|
||||||
also define this method.
|
# also define this method.
|
||||||
|
|
||||||
If this argument is not supplied, this view will use a
|
# If this argument is not supplied, this view will use a
|
||||||
``ModelForm`` automatically generated from the model specified
|
# ``ModelForm`` automatically generated from the model specified
|
||||||
by ``AUTH_PROFILE_MODULE``.
|
# by ``AUTH_PROFILE_MODULE``.
|
||||||
|
|
||||||
``success_url``
|
# ``success_url``
|
||||||
The URL to redirect to after successful profile creation. If
|
# The URL to redirect to after successful profile creation. If
|
||||||
this argument is not supplied, this will default to the URL of
|
# this argument is not supplied, this will default to the URL of
|
||||||
:view:`profiles.views.profile_detail` for the newly-created
|
# :view:`profiles.views.profile_detail` for the newly-created
|
||||||
profile object.
|
# profile object.
|
||||||
|
|
||||||
``template_name``
|
# ``template_name``
|
||||||
The template to use when displaying the profile-creation
|
# The template to use when displaying the profile-creation
|
||||||
form. If not supplied, this will default to
|
# form. If not supplied, this will default to
|
||||||
:template:`profiles/create_profile.html`.
|
# :template:`profiles/create_profile.html`.
|
||||||
|
|
||||||
**Context:**
|
# **Context:**
|
||||||
|
|
||||||
``form``
|
# ``form``
|
||||||
The profile-creation form.
|
# The profile-creation form.
|
||||||
|
|
||||||
**Template:**
|
# **Template:**
|
||||||
|
|
||||||
``template_name`` keyword argument, or
|
# ``template_name`` keyword argument, or
|
||||||
:template:`profiles/create_profile.html`.
|
# :template:`profiles/create_profile.html`.
|
||||||
|
|
||||||
"""
|
# """
|
||||||
try:
|
# try:
|
||||||
profile_obj = request.user.get_profile()
|
# profile_obj = request.user.get_profile()
|
||||||
return HttpResponseRedirect(profile_obj.get_absolute_url())
|
# return HttpResponseRedirect(profile_obj.get_absolute_url())
|
||||||
except ObjectDoesNotExist:
|
# except ObjectDoesNotExist:
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
#
|
# #
|
||||||
# We set up success_url here, rather than as the default value for
|
# # We set up success_url here, rather than as the default value for
|
||||||
# the argument. Trying to do it as the argument's default would
|
# # the argument. Trying to do it as the argument's default would
|
||||||
# mean evaluating the call to reverse() at the time this module is
|
# # mean evaluating the call to reverse() at the time this module is
|
||||||
# first imported, which introduces a circular dependency: to
|
# # first imported, which introduces a circular dependency: to
|
||||||
# perform the reverse lookup we need access to profiles/urls.py,
|
# # perform the reverse lookup we need access to profiles/urls.py,
|
||||||
# but profiles/urls.py in turn imports this module.
|
# # but profiles/urls.py in turn imports this module.
|
||||||
#
|
# #
|
||||||
|
|
||||||
if success_url is None:
|
# if success_url is None:
|
||||||
success_url = reverse('profiles_profile_detail',
|
# success_url = reverse('profiles_profile_detail',
|
||||||
kwargs={ 'username': request.user.username })
|
# kwargs={ 'username': request.user.username })
|
||||||
if form_class is None:
|
# if form_class is None:
|
||||||
form_class = utils.get_profile_form()
|
# form_class = utils.get_profile_form()
|
||||||
if request.method == 'POST':
|
# if request.method == 'POST':
|
||||||
form = form_class(data=request.POST, files=request.FILES)
|
# form = form_class(data=request.POST, files=request.FILES)
|
||||||
if form.is_valid():
|
# if form.is_valid():
|
||||||
profile_obj = form.save(commit=False)
|
# profile_obj = form.save(commit=False)
|
||||||
profile_obj.user = request.user
|
# profile_obj.user = request.user
|
||||||
profile_obj.save()
|
# profile_obj.save()
|
||||||
if hasattr(form, 'save_m2m'):
|
# if hasattr(form, 'save_m2m'):
|
||||||
form.save_m2m()
|
# form.save_m2m()
|
||||||
return HttpResponseRedirect(success_url)
|
# return HttpResponseRedirect(success_url)
|
||||||
else:
|
# else:
|
||||||
form = form_class()
|
# form = form_class()
|
||||||
|
|
||||||
if extra_context is None:
|
# if extra_context is None:
|
||||||
extra_context = {}
|
# extra_context = {}
|
||||||
context = RequestContext(request)
|
# context = RequestContext(request)
|
||||||
for key, value in list(extra_context.items()):
|
# for key, value in list(extra_context.items()):
|
||||||
context[key] = callable(value) and value() or value
|
# context[key] = callable(value) and value() or value
|
||||||
|
|
||||||
return render_to_response(template_name,
|
# return render_to_response(template_name,
|
||||||
{ 'form': form, 'settings':settings },
|
# { 'form': form, 'settings':settings },
|
||||||
context_instance=context)
|
# context_instance=context)
|
||||||
create_profile = login_required(create_profile)
|
# create_profile = login_required(create_profile)
|
||||||
|
|
||||||
def edit_profile(request, form_class=None, success_url=None,
|
# def edit_profile(request, form_class=None, success_url=None,
|
||||||
template_name='profiles/edit_profile.html',
|
# template_name='profiles/edit_profile.html',
|
||||||
extra_context=None):
|
# extra_context=None):
|
||||||
"""
|
# """
|
||||||
Edit the current user's profile.
|
# Edit the current user's profile.
|
||||||
|
|
||||||
If the user does not already have a profile (as determined by
|
# If the user does not already have a profile (as determined by
|
||||||
``User.get_profile()``), a redirect will be issued to the
|
# ``User.get_profile()``), a redirect will be issued to the
|
||||||
:view:`profiles.views.create_profile` view; if no profile model
|
# :view:`profiles.views.create_profile` view; if no profile model
|
||||||
has been specified in the ``AUTH_PROFILE_MODULE`` setting,
|
# has been specified in the ``AUTH_PROFILE_MODULE`` setting,
|
||||||
``django.contrib.auth.models.SiteProfileNotAvailable`` will be
|
# ``django.contrib.auth.models.SiteProfileNotAvailable`` will be
|
||||||
raised.
|
# raised.
|
||||||
|
|
||||||
**Optional arguments:**
|
# **Optional arguments:**
|
||||||
|
|
||||||
``extra_context``
|
# ``extra_context``
|
||||||
A dictionary of variables to add to the template context. Any
|
# A dictionary of variables to add to the template context. Any
|
||||||
callable object in this dictionary will be called to produce
|
# callable object in this dictionary will be called to produce
|
||||||
the end result which appears in the context.
|
# the end result which appears in the context.
|
||||||
|
|
||||||
``form_class``
|
# ``form_class``
|
||||||
The form class to use for validating and editing the user
|
# The form class to use for validating and editing the user
|
||||||
profile. This form class must operate similarly to a standard
|
# profile. This form class must operate similarly to a standard
|
||||||
Django ``ModelForm`` in that it must accept an instance of the
|
# Django ``ModelForm`` in that it must accept an instance of the
|
||||||
object to be edited as the keyword argument ``instance`` to
|
# object to be edited as the keyword argument ``instance`` to
|
||||||
its constructor, and it must implement a method named
|
# its constructor, and it must implement a method named
|
||||||
``save()`` which will save the updates to the object. If this
|
# ``save()`` which will save the updates to the object. If this
|
||||||
argument is not specified, this view will use a ``ModelForm``
|
# argument is not specified, this view will use a ``ModelForm``
|
||||||
generated from the model specified in the
|
# generated from the model specified in the
|
||||||
``AUTH_PROFILE_MODULE`` setting.
|
# ``AUTH_PROFILE_MODULE`` setting.
|
||||||
|
|
||||||
``success_url``
|
# ``success_url``
|
||||||
The URL to redirect to following a successful edit. If not
|
# The URL to redirect to following a successful edit. If not
|
||||||
specified, this will default to the URL of
|
# specified, this will default to the URL of
|
||||||
:view:`profiles.views.profile_detail` for the profile object
|
# :view:`profiles.views.profile_detail` for the profile object
|
||||||
being edited.
|
# being edited.
|
||||||
|
|
||||||
``template_name``
|
# ``template_name``
|
||||||
The template to use when displaying the profile-editing
|
# The template to use when displaying the profile-editing
|
||||||
form. If not specified, this will default to
|
# form. If not specified, this will default to
|
||||||
:template:`profiles/edit_profile.html`.
|
# :template:`profiles/edit_profile.html`.
|
||||||
|
|
||||||
**Context:**
|
# **Context:**
|
||||||
|
|
||||||
``form``
|
# ``form``
|
||||||
The form for editing the profile.
|
# The form for editing the profile.
|
||||||
|
|
||||||
``profile``
|
# ``profile``
|
||||||
The user's current profile.
|
# The user's current profile.
|
||||||
|
|
||||||
**Template:**
|
# **Template:**
|
||||||
|
|
||||||
``template_name`` keyword argument or
|
# ``template_name`` keyword argument or
|
||||||
:template:`profiles/edit_profile.html`.
|
# :template:`profiles/edit_profile.html`.
|
||||||
|
|
||||||
"""
|
# """
|
||||||
try:
|
# try:
|
||||||
profile_obj = request.user.get_profile()
|
# profile_obj = request.user.get_profile()
|
||||||
except ObjectDoesNotExist:
|
# except ObjectDoesNotExist:
|
||||||
return HttpResponseRedirect(reverse('profiles_create_profile'))
|
# return HttpResponseRedirect(reverse('profiles_create_profile'))
|
||||||
|
|
||||||
#
|
# #
|
||||||
# See the comment in create_profile() for discussion of why
|
# # See the comment in create_profile() for discussion of why
|
||||||
# success_url is set up here, rather than as a default value for
|
# # success_url is set up here, rather than as a default value for
|
||||||
# the argument.
|
# # the argument.
|
||||||
#
|
# #
|
||||||
|
|
||||||
if success_url is None:
|
# if success_url is None:
|
||||||
success_url = reverse('profiles_profile_detail',
|
# success_url = reverse('profiles_profile_detail',
|
||||||
kwargs={ 'username': request.user.username })
|
# kwargs={ 'username': request.user.username })
|
||||||
if form_class is None:
|
# if form_class is None:
|
||||||
form_class = utils.get_profile_form()
|
# form_class = utils.get_profile_form()
|
||||||
if request.method == 'POST':
|
# if request.method == 'POST':
|
||||||
form = form_class(data=request.POST, files=request.FILES, instance=profile_obj)
|
# form = form_class(data=request.POST, files=request.FILES, instance=profile_obj)
|
||||||
if form.is_valid():
|
# if form.is_valid():
|
||||||
form.save()
|
# form.save()
|
||||||
return HttpResponseRedirect(success_url)
|
# return HttpResponseRedirect(success_url)
|
||||||
else:
|
# else:
|
||||||
form = form_class(instance=profile_obj)
|
# form = form_class(instance=profile_obj)
|
||||||
|
|
||||||
if extra_context is None:
|
# if extra_context is None:
|
||||||
extra_context = {}
|
# extra_context = {}
|
||||||
context = RequestContext(request)
|
# context = RequestContext(request)
|
||||||
for key, value in list(extra_context.items()):
|
# for key, value in list(extra_context.items()):
|
||||||
context[key] = callable(value) and value() or value
|
# context[key] = callable(value) and value() or value
|
||||||
|
|
||||||
return render_to_response(template_name,
|
# return render_to_response(template_name,
|
||||||
{ 'form': form,
|
# { 'form': form,
|
||||||
'profile': profile_obj, },
|
# 'profile': profile_obj, },
|
||||||
context_instance=context)
|
# context_instance=context)
|
||||||
edit_profile = login_required(edit_profile)
|
# edit_profile = login_required(edit_profile)
|
||||||
|
|
||||||
def profile_detail(request, username, public_profile_field=None,
|
# def profile_detail(request, username, public_profile_field=None,
|
||||||
template_name='profiles/profile_detail.html',
|
# template_name='profiles/profile_detail.html',
|
||||||
extra_context=None):
|
# extra_context=None):
|
||||||
"""
|
# """
|
||||||
Detail view of a user's profile.
|
# Detail view of a user's profile.
|
||||||
|
|
||||||
If no profile model has been specified in the
|
# If no profile model has been specified in the
|
||||||
``AUTH_PROFILE_MODULE`` setting,
|
# ``AUTH_PROFILE_MODULE`` setting,
|
||||||
``django.contrib.auth.models.SiteProfileNotAvailable`` will be
|
# ``django.contrib.auth.models.SiteProfileNotAvailable`` will be
|
||||||
raised.
|
# raised.
|
||||||
|
|
||||||
If the user has not yet created a profile, ``Http404`` will be
|
# If the user has not yet created a profile, ``Http404`` will be
|
||||||
raised.
|
# raised.
|
||||||
|
|
||||||
**Required arguments:**
|
# **Required arguments:**
|
||||||
|
|
||||||
``username``
|
# ``username``
|
||||||
The username of the user whose profile is being displayed.
|
# The username of the user whose profile is being displayed.
|
||||||
|
|
||||||
**Optional arguments:**
|
# **Optional arguments:**
|
||||||
|
|
||||||
``extra_context``
|
# ``extra_context``
|
||||||
A dictionary of variables to add to the template context. Any
|
# A dictionary of variables to add to the template context. Any
|
||||||
callable object in this dictionary will be called to produce
|
# callable object in this dictionary will be called to produce
|
||||||
the end result which appears in the context.
|
# the end result which appears in the context.
|
||||||
|
|
||||||
``public_profile_field``
|
# ``public_profile_field``
|
||||||
The name of a ``BooleanField`` on the profile model; if the
|
# The name of a ``BooleanField`` on the profile model; if the
|
||||||
value of that field on the user's profile is ``False``, the
|
# value of that field on the user's profile is ``False``, the
|
||||||
``profile`` variable in the template will be ``None``. Use
|
# ``profile`` variable in the template will be ``None``. Use
|
||||||
this feature to allow users to mark their profiles as not
|
# this feature to allow users to mark their profiles as not
|
||||||
being publicly viewable.
|
# being publicly viewable.
|
||||||
|
|
||||||
If this argument is not specified, it will be assumed that all
|
# If this argument is not specified, it will be assumed that all
|
||||||
users' profiles are publicly viewable.
|
# users' profiles are publicly viewable.
|
||||||
|
|
||||||
``template_name``
|
# ``template_name``
|
||||||
The name of the template to use for displaying the profile. If
|
# The name of the template to use for displaying the profile. If
|
||||||
not specified, this will default to
|
# not specified, this will default to
|
||||||
:template:`profiles/profile_detail.html`.
|
# :template:`profiles/profile_detail.html`.
|
||||||
|
|
||||||
**Context:**
|
# **Context:**
|
||||||
|
|
||||||
``profile``
|
# ``profile``
|
||||||
The user's profile, or ``None`` if the user's profile is not
|
# The user's profile, or ``None`` if the user's profile is not
|
||||||
publicly viewable (see the description of
|
# publicly viewable (see the description of
|
||||||
``public_profile_field`` above).
|
# ``public_profile_field`` above).
|
||||||
|
|
||||||
**Template:**
|
# **Template:**
|
||||||
|
|
||||||
``template_name`` keyword argument or
|
# ``template_name`` keyword argument or
|
||||||
:template:`profiles/profile_detail.html`.
|
# :template:`profiles/profile_detail.html`.
|
||||||
|
|
||||||
"""
|
# """
|
||||||
user = get_object_or_404(User, username=username)
|
# user = get_object_or_404(User, username=username)
|
||||||
try:
|
# try:
|
||||||
profile_obj = user.get_profile()
|
# profile_obj = user.get_profile()
|
||||||
except ObjectDoesNotExist:
|
# except ObjectDoesNotExist:
|
||||||
raise Http404
|
# raise Http404
|
||||||
if public_profile_field is not None and \
|
# if public_profile_field is not None and \
|
||||||
not getattr(profile_obj, public_profile_field):
|
# not getattr(profile_obj, public_profile_field):
|
||||||
profile_obj = None
|
# profile_obj = None
|
||||||
|
|
||||||
if extra_context is None:
|
# if extra_context is None:
|
||||||
extra_context = {}
|
# extra_context = {}
|
||||||
context = RequestContext(request)
|
# context = RequestContext(request)
|
||||||
for key, value in list(extra_context.items()):
|
# for key, value in list(extra_context.items()):
|
||||||
context[key] = callable(value) and value() or value
|
# context[key] = callable(value) and value() or value
|
||||||
|
|
||||||
return render_to_response(template_name,
|
# return render_to_response(template_name,
|
||||||
{ 'profile': profile_obj },
|
# { 'profile': profile_obj },
|
||||||
context_instance=context)
|
# context_instance=context)
|
||||||
|
|
||||||
def profile_list(request, public_profile_field=None,
|
# def profile_list(request, public_profile_field=None,
|
||||||
template_name='profiles/profile_list.html', **kwargs):
|
# template_name='profiles/profile_list.html', **kwargs):
|
||||||
"""
|
# """
|
||||||
A list of user profiles.
|
# A list of user profiles.
|
||||||
|
|
||||||
If no profile model has been specified in the
|
# If no profile model has been specified in the
|
||||||
``AUTH_PROFILE_MODULE`` setting,
|
# ``AUTH_PROFILE_MODULE`` setting,
|
||||||
``django.contrib.auth.models.SiteProfileNotAvailable`` will be
|
# ``django.contrib.auth.models.SiteProfileNotAvailable`` will be
|
||||||
raised.
|
# raised.
|
||||||
|
|
||||||
**Optional arguments:**
|
# **Optional arguments:**
|
||||||
|
|
||||||
``public_profile_field``
|
# ``public_profile_field``
|
||||||
The name of a ``BooleanField`` on the profile model; if the
|
# The name of a ``BooleanField`` on the profile model; if the
|
||||||
value of that field on a user's profile is ``False``, that
|
# value of that field on a user's profile is ``False``, that
|
||||||
profile will be excluded from the list. Use this feature to
|
# profile will be excluded from the list. Use this feature to
|
||||||
allow users to mark their profiles as not being publicly
|
# allow users to mark their profiles as not being publicly
|
||||||
viewable.
|
# viewable.
|
||||||
|
|
||||||
If this argument is not specified, it will be assumed that all
|
# If this argument is not specified, it will be assumed that all
|
||||||
users' profiles are publicly viewable.
|
# users' profiles are publicly viewable.
|
||||||
|
|
||||||
``template_name``
|
# ``template_name``
|
||||||
The name of the template to use for displaying the profiles. If
|
# The name of the template to use for displaying the profiles. If
|
||||||
not specified, this will default to
|
# not specified, this will default to
|
||||||
:template:`profiles/profile_list.html`.
|
# :template:`profiles/profile_list.html`.
|
||||||
|
|
||||||
Additionally, all arguments accepted by the
|
# Additionally, all arguments accepted by the
|
||||||
:view:`django.views.generic.list_detail.object_list` generic view
|
# :view:`django.views.generic.list_detail.object_list` generic view
|
||||||
will be accepted here, and applied in the same fashion, with one
|
# will be accepted here, and applied in the same fashion, with one
|
||||||
exception: ``queryset`` will always be the ``QuerySet`` of the
|
# exception: ``queryset`` will always be the ``QuerySet`` of the
|
||||||
model specified by the ``AUTH_PROFILE_MODULE`` setting, optionally
|
# model specified by the ``AUTH_PROFILE_MODULE`` setting, optionally
|
||||||
filtered to remove non-publicly-viewable proiles.
|
# filtered to remove non-publicly-viewable proiles.
|
||||||
|
|
||||||
**Context:**
|
# **Context:**
|
||||||
|
|
||||||
Same as the :view:`django.views.generic.list_detail.object_list`
|
# Same as the :view:`django.views.generic.list_detail.object_list`
|
||||||
generic view.
|
# generic view.
|
||||||
|
|
||||||
**Template:**
|
# **Template:**
|
||||||
|
|
||||||
``template_name`` keyword argument or
|
# ``template_name`` keyword argument or
|
||||||
:template:`profiles/profile_list.html`.
|
# :template:`profiles/profile_list.html`.
|
||||||
|
|
||||||
"""
|
# """
|
||||||
profile_model = utils.get_profile_model()
|
# profile_model = utils.get_profile_model()
|
||||||
queryset = profile_model._default_manager.all()
|
# queryset = profile_model._default_manager.all()
|
||||||
if public_profile_field is not None:
|
# if public_profile_field is not None:
|
||||||
queryset = queryset.filter(**{ public_profile_field: True })
|
# queryset = queryset.filter(**{ public_profile_field: True })
|
||||||
kwargs['queryset'] = queryset
|
# kwargs['queryset'] = queryset
|
||||||
return object_list(request, template_name=template_name, **kwargs)
|
# return object_list(request, template_name=template_name, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user