Friday, November 29, 2013

Re: Signals not working as expected


You can put signal handling and registration code anywhere you like. However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app's models.py a good place to put registration of signal handlers.

 I think you have to put your connection code in models.py (your `create_stream_item` function can still live in signals.py) in order to be sure connections are made early enough.

2013/11/28 Aamu Padi <aamupadi@gmail.com>
I am trying to create a project for creating feeds/activity feeds of a user with the help of a blog.

These are the models -

    class StreamItem(models.Model):
        user = models.ForeignKey(User)
         content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        pub_date = models.DateTimeField(default=datetime.now)
        content_object = generic.GenericForeignKey('content_type', 'object_id')

        @property
        def content_class(self):
            return self.content_type.model


    class Blog(models.Model):
        user = models.ForeignKey(User)
        title = models.CharField(max_length=300)
        body = models.TextField()
        pub_date = models.DateTimeField(default=datetime.now)
   
   
    class Photo(models.Model):
        user = models.ForeignKey(User)
        title = models.CharField(max_length=200)
        image = models.ImageField(upload_to=get_upload_file_name)
         pub_date = models.DateTimeField(default=datetime.now)

And this is the signals.py:

    from django.db.models import signals
    from django.contrib.contenttypes.models import ContentType
    from django.dispatch import dispatcher
    from blogs.models import Blog
    from picture.models import Photo
    from models import StreamItem

    def create_stream_item(sender, instance, signal, *args, **kwargs):

         # Check to see if the object was just created for the first time

        if 'created' in kwargs:
             if kwargs['created']:
                 create = True

                 # Get the instance's content type

                 ctype = ContentType.object.get_for_model(instance)

                 if create:
                     si = StreamItem.objects.get_or_create(content_type=ctype, object_id=instance.id, pub_date = instance.pub_date)

     # Send a signal on post_save for each of these models

    for modelname in [Blog, Photo]:
        dispatcher.connect(create_stream_item, signal=signals.post_save, sender=modelname)

When I create a blog or upload a photo, the `signal` does not work. But I can manually add items to the `StreamItem` app using the admin, and the StreamItem does work as I want it to be. I think there's problem with the signals.py. Please help me out. Would be much appreciate. Thank you.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHSNPWtGQPpE7eexnis6EpC2-_WtLcf0ZY-uhvuschj7Tg3MVA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOLVUFdryq%3DpKvx6Jm6brPHwfbDOetcJQVgo92dEUDjUhQLiKQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

multi-table inheritance newbie

I'm using multi-table inheritance in project and need some help geeting my mind around this concept

I have parent class 'test' which is a machine test (members : name, version state; methods : add_test, remove_test)

I now create a child class 'result' which inherits from parent class 'test' - 'result' is the result of all the scenarios inside the machine test (multiple scenarios per machine test)

Child class 'result' will have its own members/methods (members : scenario_name, scenario_outsome; methods : add_result, delete_result)

So now I can create an instance of parent 'test' class by calling the child class 'result'
 >>>>testme= result.add_test("""args""")

So what is now in the object "testme" that I just created? I cannot viasualise what this instance holds and how the parent/child relationship works

What would be some useful method calls be to help me get a better understanding of the 'result' object so I could 'see' the influence of the 'parent' class inheritance

thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bb72e0e1-cfe4-44c9-90a0-dfbc825db776%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

django - model for a messaging app between users

How do I go about creating a message app, between users. What's the business logic for creating the model? All I can think of was like this:

models.py

    class Message(models.Model):
        description = models.TextField()
        date_added = models.DateTimeField(default=datetime.now)
        sender = models.ForeignKey(User)
        recipient = models.ForeignKey(User)

I am not very sure if this is the way to go. If you could kindly guide me on how to get started, will be very thank full. Thank you!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHSNPWtZaFs0%2BWSiZKmCnykDRBSo02Uru92_G1yC7ztzjmvjhw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Email Templates and the full website URL

On Fri, Nov 29, 2013 at 2:09 PM, Joseph Mutumi <jjmutumi@gmail.com> wrote:
> That could work but isn't it a bit insecure? I think it will be susceptible
> to a header injection(http://en.wikipedia.org/wiki/HTTP_header_injection). I
> would rather create a setting with the domain name in settings.py and then
> call it from the template or write a custom template tag.

Possibly; if you are using vhosts at all, then the host header is hard
to spoof, as in order to be routed to your application, the request
must already have the appropriate header.

If you don't use vhosts, then it is in any case wise to apply host
name canonicalisation to your website - your site may respond to
'www.foo.com' and 'foo.com', but requests for 'foo.com/blah' are
immediately redirected to 'www.foo.com/blah'. This will aid with SEO
and provide absolute, consistent URLs.

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1JTkO7DNSVN%3Dj7AMR%3DhuS6uET1WcOfC%2B0jOYOuRW0hHWQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Email Templates and the full website URL

2013/11/29 Joseph Mutumi <jjmutumi@gmail.com>
That could work but isn't it a bit insecure? I think it will be susceptible to a header injection(http://en.wikipedia.org/wiki/HTTP_header_injection). I would rather create a setting with the domain name in settings.py and then call it from the template or write a custom template tag.

Check Django's ALLOWED_HOSTS [1] setting, it is supposed to account for this, and Django 1.5+ requires you to explicitly set it in order to run a site with DEBUG=False

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAP5dYfP%2BJgrDnnN_aCNRwdaO8bQbkRB36eUQJef8g4T5BRj_Cg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Filtering by month doesn't work

I think the model is not being saved because no instance is being constructed by the form. Either make call to event_form.is_valid() of event_form.full_clean(), the former being preferred.


On Tue, Nov 26, 2013 at 8:30 PM, Leonardo Giordani <giordani.leonardo@gmail.com> wrote:
Are you sure that filtering using a string is the same as filtering using an int? I think that Django doesn't automatically convert it. So the condition


events = Event.objects.filter(end__month='11')

should be

events = Event.objects.filter(end__month=11)

Indeed documentation does things this way. I see that you succeded in filtering with

filter(end_start='2013')

perhaps end__startswith? In that case you are treating it as a string.
May you please check this and let me know?

Cheers,

Leo



Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile


2013/11/26 Vojtěch Tranta <vojta.tranta@gmail.com>
from django.db import models
from django.contrib.auth.models import User
from student import Student
from eventtype import EventType
from django import forms
import datetime

class Event(models.Model):
user = models.ForeignKey(User)
student = models.ForeignKey(Student)
place = models.CharField(max_length=200, null=True)
type = models.ForeignKey(EventType)
start = models.DateTimeField('event start', default=datetime.datetime.now)
end = models.DateTimeField('event end', default=datetime.datetime.now)
absence = models.NullBooleanField()
note = models.TextField(null=True)
created = models.DateTimeField('created time')
created_by = models.ForeignKey(User, related_name='event creator')
description = models.TextField(null=True)

class Meta:
app_label = 'jvc'

def __unicode__(self):
return self.type.name +' '+ self.student.fullname +' '+self.start.strftime('%d.%m.%Y')

I should mention that I have my models in separated files in "models" folder inside app called "jvc"

Dne úterý, 26. listopadu 2013 8:25:40 UTC+1 Leo napsal(a):
May you please post your Event model?

Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile


2013/11/26 Начаров Михаил <michael....@gmail.com>
Hi Tranta.
In my projects this functionality works fine. 
What version of django did you used? 
Do you sure that field end in Event table contains November dates?

Also, you can use today.month instead of  int(today.strftime('%m')) and today.year instead of  int(today.strftime('%Y')).
And in django docs says that it necessary to validate your form before use it:
if event_form.is_valid():
    event = event_form.save()

вторник, 26 ноября 2013 г., 5:24:21 UTC+6 пользователь Vojtěch Tranta написал:
Hi,
do you have any clue why this does not work?

def index(request):
if request.method == 'POST' and request.POST:
event_form = EventForm(request.POST)
event = event_form.save()

today = datetime.date.today();
year = int(today.strftime('%Y'))
month = int(today.strftime('%m'))
events = Event.objects.filter(end__month='11')
# events = Event.objects.all()
pdb.set_trace()
cal = jvccalendar.HTMLCalendar(jvccalendar.MONDAY).formatmonth(year, month, events)
form = EventForm()
return render_to_response('jvc/index.html', {'cal': cal, 'side_form': form}, context_instance=RequestContext(request))

I am still getting "events" as empty, filtering using filter(end_start='2013') works, but filtering by month does not, I googled as much as I could, but no luck. I copied code from StackOverflow, but no luck.... 
Thank you!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5a50a517-fe4f-48b5-a713-c5042b5cab3b%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEhE%2BOm2eMP0YAgQEbUQj7rtAMjrQWV7nigUnbciBq1FfzfecA%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAN5idp_uPS8R9tRWu1RJzjthxx39UAxZK-trET6dYVghE8M-rA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: Email Templates and the full website URL

That could work but isn't it a bit insecure? I think it will be susceptible to a header injection(http://en.wikipedia.org/wiki/HTTP_header_injection). I would rather create a setting with the domain name in settings.py and then call it from the template or write a custom template tag.


On Fri, Nov 29, 2013 at 2:55 PM, Rafael E. Ferrero <rafael.ferrero@gmail.com> wrote:
Good work!!



2013/11/29 Vibhu Rishi <vibhu.rishi@gmail.com>
Thanks for the links. I had done the google searches and gone through them, but they seemed to me a lot of work to get something simple.

I finally did the following. Any comments welcome if this is not a good way to do.

In my view, I pass a context object of the request to the email template. I need the request as i also want to put in the user's name.

in the email template now I changed it to :
<A href="http://{{request.get_get_host}}{% url "project.views.details" project.id %}">{{ project }}</a>

This seems to be working and quite simple too !

Regards,
Vibhu




On Fri, Nov 29, 2013 at 4:26 PM, Rafael E. Ferrero <rafael.ferrero@gmail.com> wrote:


2013/11/29 Vibhu Rishi <vibhu.rishi@gmail.com>
hi,

I have a setup where I have a project details page, and I can do a "send email" which should send the email with the URL.

Email is working fine.

The problem is that i am getting a relative url in the tempalte

I have the following in the html email template :
<A href="{% url "project.views.details" project.id %}">{{ project }}</a>

This give me a URL in the email as /projects/1 ( 1 being the project id)

How do i prepend the url of the server here ? e.g. I want this to be http://localhost:8000/projects/1

Vibhu

--
Simplicity is the ultimate sophistication. - Leonardo da Vinci
Life is really simple, but we insist on making it complicated. - Confucius

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPiONwn6cHwi51fJ63oFUOLof2QmFqsSeqz2VeOM_Jxk%2BaUYGQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Rafael E. Ferrero

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJJc_8WWUyfwYd1cjxNzvm0xe5LjUTNDjPGDnYaaxE9w3B1C-g%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Simplicity is the ultimate sophistication. - Leonardo da Vinci
Life is really simple, but we insist on making it complicated. - Confucius

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPiONw%3D7Uh9uReNyCCzhGb%3D09WHCzY9rSPp9mYn_eJRsHwmNpw%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Rafael E. Ferrero

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJJc_8WiwQjgNPKX4RZ0eQu%3DkYz%2BH51BywB0rQMVJ4u8XW8hbw%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAN5idp9_g88SHrHBN2YZQVA%2BxbGFJ-F6Ac2PvxD3uLF7Dqa9_w%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.