relating models to one another using generic views - django-urls

I'm new to Django and programming in general. I'm trying to make a simple site that allows players of a sport sign up for leagues that have been created by the admin. In my models.py, I created two models:
from django.db import models
from django.forms import ModelForm
class League(models.Model):
league_name = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published')
class Info(models.Model):
league = models.ManyToManyField(League)
name = models.CharField(max_length=50)
phone = models.IntegerField()
email = models.EmailField()
def __unicode__(self):
return self.info
class InfoForm (ModelForm):
class Meta:
model = Info
exclude = ('league')
From what I've read, I can probably use the Create/Update/Delete generic views to display a form for the user to sign up for the league. So with my app, I want the user to come to a simple homepage that lists the leagues, be able to click on the league and enter their info to sign up. Here's what my urlconf looks like:
from django.conf.urls.defaults import *
from mysite.player_info.models import League, Info, InfoForm
info_dict = {
'queryset': League.objects.all(),
}
InfoForm = {'form_class' : InfoForm}
urlpatterns = patterns('',
(r'^$', 'django.views.generic.list_detail.object_list', info_dict),
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
url(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='player_info/results.html'), 'league_results'),
(r'^(?P<object_id>\d+)/info/create/$', 'django.views.generic.create_update.create_object', InfoForm),
)
Here's my problem: When I click on a league to sign up for on the homepage with my current setup, I get this error: TypeError at /league/1/info/create.... create_object() got an unexpected keyword argument 'object_id'. What am I doing wrong?

The issue isn't with your models, but rather with the function your "create" URL calls -- the line that calls django.views.generic.create_update.create_object() in urls.py. create_object() doesn't take an object_id argument, but you specified one in your url (r'^(?P<object_id>\d+)/info/create/$'). This makes sense -- you're creating an object, so you don't know its ID yet. create_object() only takes a form_class or model argument, as noted in the docs.
I'm guessing you're trying to create an Info object that is attached to a League object, and in that URL, <object_id> is the ID number of the League object; in which case, you shouldn't name that ID number, and instead should just use r"^\d+/info/create/$" as the URL. I'm not sure how you'll grab the league ID number using Django's create_object() function, though. You might have to write your own view handler. You may be able to use a custom ModelForm and pass it in with the form_class parameter, but I'm not sure.

Related

Groovy script to change all users to uppercase

I'm trying to change all user names with in a magnolia application to uppercase since we are having case sensitivity issues with our login.
I wrote this groovy script, following an example used to reset passwords to "", to capture the users and change them uppercase but it appears the name property is not being set.
https://documentation.magnolia-cms.com/display/WIKI/Reset+all+passwords
import info.magnolia.jcr.util.NodeUtil
import info.magnolia.jcr.predicate.NodeTypePredicate
import info.magnolia.jcr.util.NodeTypes
session = ctx.getJCRSession("users")
users = NodeUtil.collectAllChildren(session.getNode("/public"), new NodeTypePredicate(NodeTypes.User.NAME))
users.each() {
changedName = it.name.toUpperCase();
it.setProperty("name", changedName)
it.save();
println "1 " + changedName;
println "2 " + it.name;
}
session.save();
When I'm checking it.name it is return how they are stored in the mangolia, and not as all uppercase and they are also not being changed in the security app when looking at the username.
import info.magnolia.jcr.util.NodeUtil
import info.magnolia.jcr.predicate.NodeTypePredicate
import info.magnolia.jcr.util.NodeTypes
session = ctx.getJCRSession("users")
users = NodeUtil.collectAllChildren(session.getNode("/admin"), new NodeTypePredicate(NodeTypes.User.NAME))
users.each() {
name = it.name
changedName = it.name.toUpperCase();
it.setProperty("name", changedName)
it.setProperty("jcrName", changedName)
it.save()
NodeUtil.renameNode(it, changedName)
it.getNode("acl_users").getNodes().each { node ->
newPath = node.getProperty("path").getString().replace(name, changedName)
node.setProperty("path", newPath)
node.save()
}
}
session.save()
Hey, maybe this is what u are looking for. You need to change the Node name and the jcrName in my Version I iterate over the acl_users Node and change the path of each. Hope this works for you.
IIRC there's 3 things you need to change.
name is one of them, then there's jcrName property and then you need to change the name of the node itself. At least if you want to see it in security app that way.
For the login itself, what you did should be already sufficient.
Try to use the setProperty method of PropertyUtil.
You have to extract all nodes that you need and then loops through them. Supposing that the variable node is the Node you want to change the name, do this:
String newName = StringUtils.upperCase(PropertyUtil.getString(node, "jcrName"));
PropertyUtil.setProperty(node, "jcrName", newName);
jcrName is the property you need to overwrite. Wrap the code into a try/catch block and here we go.
Hope it helps.

I am trying to bind my FormView to show the Form in djangocms but i am not able to

I am trying to bind my FormView to show the Form in djangocms but I am not able to,
my model is:
class ContactFormView(FormView, CMSPlugin):
template = '/ContactForm/ContactForm.html'
form_class = contact_form
success_url = reverse_lazy('success-page')
def post(self, request, **kwargs):
assert request.is_ajax()
request_data = json.loads(request.body)
form = self.form_class(data=request_data[self.form_class.scope_prefix])
if form.is_valid():
return JsonResponse({'success_url': force_text(self.success_url)})
else:
response_data = {form.form_name: form.errors}
return JsonResponse(response_data, status=422)
cms_plugins.py:
class ContactFormPlugin(CMSPluginBase):
model = ContactForm.ContactFormView
render_template = '/ContactForm/ContactForm.html'
name = 'Contact Form'
allow_children = False
plugin_pool.register_plugin(ContactFormPlugin)
Looking at what you've got there, I'm not sure you've given your plugin a proper model but a view!? And the form class isn't a form class, form_class = contact_form but what is contact_form? You need it to be the class of your form e.g.
class ContactForm(forms.Form):
name = forms.CharField(label='Your name', max_length=100)
email = forms.EmailField(label='Your email', max_length=100)
class ContactFormView(FormView):
template = '/ContactForm/ContactForm.html'
form_class = ContactForm
success_url = reverse_lazy('success-page')
And if you've got a form you want your plugin to render, you need to tell it what to do...
from cms.models.pluginmodel import CMSPlugin
class ContactFormPlugin(CMSPluginBase):
model = CMSPlugin
render_template = '/ContactForm/ContactForm.html'
name = 'Contact Form'
allow_children = False
def render(self, context, instance, placeholder):
context = super(ContactFormPlugin, self).render(context, instance, placeholder)
context['form'] = ContactForm()
return context
However you're working with a CMS plugin, so you don't define a View at all for a plugin. I think you could make this do what you're trying to do but the view doesn't want to inherit CMSPlugin because you just want a view to receive data from the plugin.
If you need to define views then you're building an app which in CMS requires an Apphook, not a plugin. (Technically you could post to a URL without an Apphook, but depends how much CMS integration you want)
There is a great example that starts with a plugin posting to a view then progresses into the app integration which is possible here; http://docs.django-cms.org/en/latest/introduction/plugins.html

Extend Plone-Controlpanel Form

Is it possible to extend the Controlpanel-View with each addon?
For Example
ca.db.core -> Makes basic fieldset/tab for DB Connection Settings
ca.db.person -> If installed, Adds to the "core" settings a new fieldset/tab for Person specific fields/settings
ca.db.schema -> If installed, also adds an new fieldset/tab for schema.org Fields
Yes it's possible, I once discussed this problem with a guy, who wrote the bda.plone.shop addon.
They faced the same problem, and solved it by using a ContextProxy object, which puts the different schema definitions together in one proxy object.
Using a proxy is IMHO a hack, but I don't know a better solution.
The proxy, tries to get/set a attribute from a list of schemas.
Be aware, you need to handle conflicting names, means if you have the same field name in more than one schema.
class ContextProxy(object):
def __init__(self, interfaces):
self.__interfaces = interfaces
alsoProvides(self, *interfaces)
def __setattr__(self, name, value):
if name.startswith('__') or name.startswith('_ContextProxy__'):
return object.__setattr__(self, name, value)
registry = getUtility(IRegistry)
for interface in self.__interfaces:
proxy = registry.forInterface(interface)
try:
getattr(proxy, name)
except AttributeError:
pass
else:
return setattr(proxy, name, value)
raise AttributeError(name)
def __getattr__(self, name):
if name.startswith('__') or name.startswith('_ContextProxy__'):
return object.__getattr__(self, name)
registry = getUtility(IRegistry)
for interface in self.__interfaces:
proxy = registry.forInterface(interface)
try:
return getattr(proxy, name)
except AttributeError:
pass
raise AttributeError(name)
Now you need to use the proxy in your ControlPanel form.
I assume you're using the RegistryEditForm from plone.registry:
class SettingsEditForm(controlpanel.RegistryEditForm):
schema = ISettings
label = _(u"Settings")
description = _(u"")
# IMPORTANT Note 1 - This is where you hook in your proxy
def getContent(self):
interfaces = [self.schema] # Base schema from ca.db.core
interfaces.extend(self.additionalSchemata) # List of additional schemas
return ContextProxy(interfaces)
# IMPORTANT Note 2 - You may get the additional schemas dynamically to extend the Settings Form. For example by name (startswith...)
# In this case they have a separate interface, which marks the relevant interfaces.
#property
def additionalSchemata(self):
registry = getUtility(IRegistry)
interface_names = set(record.interfaceName for record
in registry.records.values())
for name in interface_names:
if not name:
continue
interface = None
try:
interface = resolve(name)
except ImportError:
# In case of leftover registry entries of uninstalled Products
continue
if ISettingsProvider.providedBy(interface):
yield interface
...
You can find the full code here

Openerp override product_id_change function for sale orders

I need to override the product_id_change function defined in sale.order.line, so it doesn't change the unit_price every time I update the quantity.
I understand I have to use super, but I'm not sure how it all works.
Can anyone help?
Thank you.
This is pretty straight forward but you need to do a couple of things (assuming OpenERP 7)
Create your new module.
Create a class that inherits osv.Model
Add an _inherit = 'sale.order.line'
Implement your new product_id_change method.
Install your module and it should work.
A typical pattern is to call super and then process the results.
class MyModel(osv.Model):
_inherit = 'sale.order.line'
def product_id_change(self, cr, uid, ids, ...):
res = super(MyModel, self).product_id_change(cr, uid, ids...)
# do stuff with res.
return res
There is an example of exactly this in the OpenERP sale_margin module in the sale_margin.py file.

How could I render just the form from an autoform?

I'm in the context of a BrowserView. I want to render a form inside the page.
class CommandeView(BrowserView):
...
def render_myform(self):
livraison_form = LivraisonForm(self.context, self.request)
livraison_form.update()
return livraison_form.render()
class ILivraisonForm(interface.Interface):
livraison = schema.Choice(title=_(u"Mode de livraison"),
vocabulary=livraison_vocabulary)
class LivraisonForm(AutoExtensibleForm, form.EditForm):
schema = ILivraisonForm
class LivraisonFormAdapter(object):
component.adapts(ICommande)
interface.implements(ILivraisonForm)
def __init__(self, context):
self.context = context
self.livraison = self.context.livraison
I would like render_myform to render only the form but it return a HTML Plone page.
Any tips are welcomed.
Haven't checked this in-depth, but the form is probably wrapped by the plone FormWrapper class.
Look for a .form attribute on the LivraisonForm instance:
def render_myform(self):
livraison_form = LivraisonForm(self.context, self.request).form
livraison_form.update()
return livraison_form.render()
It may be you need to add an additional interface to the request for this to work (the wrapper normally does this for you. I'm a little low on time right now, so you get this untested:
from plone.z3cform import z2
class CommandeView(BrowserView):
def render_myform(self):
z2.switch_on(self) # Apply layer interface, set up locale, add `getURL` method
livraison_form = LivraisonForm(self.context, self.request).form
livraison_form.update()
return livraison_form.render()
The Plone Knowledgebase uses this same setup to show a form in a viewlet.

Resources