error creating a new user inside coverage test for django - django-models

I need to create a user inside a coverage test of a view and I am trying this but it is not working:
request.user = User.objects.create_(
username='admin', email='admin#sura.com', password='eagle0002')
What did I miss?

Try this :
request.user = User.objects.create_user(
username='admin', email='admin#sura.com', password='eagle0002')
Hope it helps

Related

Login via rest using Restangular

The senario is that I just want to do a simple login using
var test = {login: 'testuser', password: 'asd'};
Restangular.one('/auth/login').post(test);
it makes a request to "auth/login/[object%20Object]" and I get a 404?
using this code it works just fine:
xmlhttp.send('{"login":"testuser", "password":"asd"}’);
Any ideas?
You can use customPOST(test) and the data should be passed appropriately.
The code was actually working.. error was in the API!

NDB Query not returning most recently added object

I have a GAE website. The home page displays a list of project objects and a form for adding more projects. When the submit button is pressed, an ajax request is made which runs the 'create_userstoryproject()' method. This method adds the new project and returns a list of all projects, including the one that was just added. For some reason, the query in the 'create_userstoryproject()' method does not return the project just added. However, if I refresh the page, causing the 'get()' method to run, the newly added project shows up just fine. I haven't simplified this code--it's cut and pasted. Anyone have any idea why the newly created project doesn't show up until I refresh the page?
class Home(BaseApp): #BaseApp is derived from webapp2.RequestHandler
def get(self):
self.context['userstoryprojects'] = UserStoryProject.query().order(UserStoryProject.Order)
self.context['userstorystatuses'] = UserStoryStatus.query().order(UserStoryStatus.Order)
self.render('/userstories')
def create_userstoryproject(self):
description = self.request.get('userstoryproject[description]', default_value=None)
if description:
userstoryproject = UserStoryProject()
userstoryproject.Description = description
userstoryproject.put()
self.context['userstoryprojects'] = UserStoryProject.query().order(UserStoryProject.Order)
self.render('/userstoryprojects')
else:
self.write("fail.")
This is because of eventual consistency.

How do I test an HTML Email Template on CakePHP?

To confirm a purchase I must send an e-mail to the customer with her shopping cart details. I'd like to test the HTML template to use the appropiate CSS, and of course to check the data that arrives to it.
What code should I use instead of setting the Email parameters, to watch how the template will render on the Email?
I'm all new on CakePHP, so your help will be very much appreciated.
Thanks in advance.
~José
This is CakePHP 1.3, but I have a feeling it may work well with 2.0 as well. While there may be other ways to do it, I do by creating a test action in any controller, then return a render call of the email template. Check this out:
function email_test()
{
$this->layout = 'email/html/default';
$user = $this->User->findById(1);
$this->set('name', $user['User']['firstname']);
$this->set('email_heading', 'Welcome to My App');
return $this->render('/elements/email/html/welcome');
}
This action will now render out your email in the browser.
Use the debug transport for testing.
If you want to make it more comfortable write your own transport that creates a new html file in for example APP/tmp/email/.html See the debug transport class as a reference, it's dead easy to do this http://api20.cakephp.org/view_source/debug-transport#l-34
See also the book -> http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#using-transports
Here is the simple method I use to show html/plain text content of email in browser that I want to send using cakephp email class i.e. App::uses('CakeEmail', 'Network/Email');.
Just do an exit at the end of email template file or (email layout file) and then try to send email. it will render the email content.
HTH.
$this->Email->to = $user_id_array[0]['User']['email'];
$this->Email->subject = $arrTemplate[0]['EmailTemplate']['subject'];
$this->Email->replyTo = 'Admin<admin#indianic.com>';
$this->Email->from = 'Admin<admin#indianic.com>';
$this->Email->sendAs = 'html';
$this->Email->template = '/elements/email/html/warning_message';
$this->set('email_message',$arrTemplate[0]['EmailTemplate']['body']);
$this->Email->send();
in this way you can set template for email
If you want to print your email template on localhost for testing then you can use this code.
$this->Email->send();
print_r($this->Email->htmlMessage);

Django Models "IndexError: list index out of range" Pydev

I have a Django project in Eclipse PyDev.
I have a file views.py which has the line:
from models import ingredient2
In models.py I have:
from django.db import models
class ingredient2(models.Model):
ingredient = models.CharField(max_length=200)
When I try to run the app I get the following error:
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 54, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range
I did sync the database and started the server running.
I went into base.py and added 2 print statements (yes, I probably should not edit Django's files):
if getattr(meta, 'app_label', None) is None:
# Figure out the app_label by looking one level up.
# For 'django.contrib.sites.models', this would be 'sites'.
model_module = sys.modules[new_class.__module__]
print model_module #ADDED
print model_module.__name__ #ADDED
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
They print out:
<module 'models' from 'C:\Users\Tine\workspace\slangen\slangen2\bolig\models.pyc'>
models
manage.py is contained within the bolig folder. I think the correct app label would be "bolig". The app worked several months ago and now, when I come back to it, something is not right. I have been creating other projects in PyDev.
Add a meta class with an app_label inside your model class definition:
class Foo:
id = models.BigIntegerField(primary_key=True)
class Meta:
app_label = 'foo'
I had something similar
instead of
from models import ingredient2
try :
from your_app_name.models import ingredient2
Well, not really an answer, but... I ended up creating a new django project and then copying in my code. That fixed the problem.
I was also getting the kwargs = {"app_label": model_module.__name__.split('.')[-2]} error when using PyDev. In my case, the project wasn't refreshed before I tried to run it. As soon as I refreshed it, all was well again.
I ran into this problem using Eclipse, Django and PyDev. I needed to have the application (instead of some .py file for example) selected in the PyDev Package Explorer (left panel) before clicking Run for everything to work properly.
in my case, models.py contains models
when I import models to other .py, say views.py it doesn't raise error when I run views.py
but when I run models.py, it raise the same error.
so I will just don't run in the models.py

Google App Engine logout url

I am having problems getting the logout link work in GAE (Python).
This is the page I am looking at.
In my template, I create a link
<p>Logout</p>
But when I click on it I get "broken link" message from Chrome. The url for the link looks like this:
http://localhost:8085/users.create_logout_url(
My questions:
Can anybody explain how this works in general?
What is the correct url for the dev server?
What is the correct url for the app server?
What is the ("/") in the logout url?
Thanks.
EDIT
This link works; but I don't know why:
<p>Logout</p>
What sort of templates are you using? It's clear from the output that you're not escaping your code correctly.
Seems to me that you want to do this instead:
self.response.out.write("This is the url: %s", users.create_logout_url("/"))
You could also pass it to your template, using GAEs implemented django templates.
from google.appengine.ext.webapp import template
...
...
(inside your request handler)
class Empty: pass
data = Empty()
data.logout = users.create_logout_url("/")
self.response.out.write(template.render(my_tmpl, {'data': data})
A useful approach is to add all sorts of info to a BaseRequestHandler and then use this as base class for all of your other request handler classes.
from google.appengine.ext import webapp
...
class BaseRequestHandler(webapp.RequestHandler):
def __init__(self):
webapp.RequestHandler.__init__(self) # extend the base class
class Empty: pass
data = Empty()
data.foo = "bar"
Then your new classes will have access to all the data you provided in the base class.
class OtherHandler(BaseRequestHandler):
def get(self):
self.response.out.write("This is foo: %s" % self.data.foo) # passes str "bar"
Hope it helps.
A.
Hi following more or less what this article is showing for the user account stuff. In gwt I store server side the logout/login url and I pass them to the client
http://www.dev-articles.com/article/App-Engine-User-Services-in-JSP-3002

Resources