Esempio n. 1
0
 def save(self, user):
     self.user = user
     
     if self.cleaned_data.get("description", False):
         self.instance.description = self.cleaned_data["description"]
     
     if self.cleaned_data.get("status", False):
         self.instance.status = self.cleaned_data["status"]
         
     if self.cleaned_data.get("mapmode", False):
         self.instance.mapmode = self.cleaned_data["mapmode"]
         
     if self.cleaned_data.get("privacy", False):
         self.instance.privacy = self.cleaned_data["privacy"]
         
     self.instance.creator = user
     self.instance.save()
     
     content_type = ContentType.objects.get_for_model(self.instance)
     for category in self.cleaned_data["categories"]:
         item = CategorizedItem.objects.get_or_create(object_id=self.instance.id, content_type=content_type,  category=category)
     
     if self.instance.status != self.instance.STATUS_PUBLIC:
         create_activity_item("created_story", user, self.instance)
         
     return self.instance
Esempio n. 2
0
def edit(request, id, template_name="story/edit.html"):
    story = get_object_or_404(Story, pk=id)
    
    if story.creator == request.user:
        if request.POST:
            create_activity_item("edited_story", request.user, story)
            # save
            
            form = StoryEditForm(request.POST, instance=story)

            if form.is_valid():
                form.save(request.user)
                return HttpResponse(status=200, content="<div class='ajax-message'>Successfully stored Changes</div>")
            else:
                return HttpResponse(status=400, content="Error")
        else:
            return render_to_response(template_name, {
                "story": story,
                "post_form": StoryPostForm(),
                "video_form": StoryVideoForm(),
                "gpx_upload_form": GPXUploadForm(request.user),
                "story_edit_form": StoryEditForm(instance=story),
            }, context_instance=RequestContext(request))
    else:
        return render_to_response("error.html", {
            "title": _("You are not authorized"),
            "text": _("Only the creator of the Story can edit it")
        }, context_instance=RequestContext(request))
Esempio n. 3
0
def start_follow(request, username, success_url=None):
    user = get_object_or_404(User, username=username)
    follower = ActivityFollower(to_user=user, from_user=request.user)
    follower.save()

    create_activity_item("started_following", request.user, user)

    if not success_url:
        success_url = request.META.get('HTTP_REFERER','/')
       
    if notification:
        notification.send([user], "new_follower", {"follower": request.user})
        
    return HttpResponseRedirect(success_url)
Esempio n. 4
0
def start_follow(request, username, success_url=None):
    user = get_object_or_404(User, username=username)
    follower = ActivityFollower(to_user=user, from_user=request.user)
    follower.save()

    create_activity_item("started_following", request.user, user)

    if not success_url:
        success_url = request.META.get('HTTP_REFERER', '/')

    if notification:
        notification.send([user], "new_follower", {"follower": request.user})

    return HttpResponseRedirect(success_url)
Esempio n. 5
0
def set_main_image(request, id=None, photo_id=None):
    story = get_object_or_404(Story, pk=id)
    if story.creator == request.user:
        photo = Image.objects.get(pk=photo_id)
        storyimages = StoryImage.objects.filter(story=story).all()
        if storyimages:
            for image in storyimages:
                image.delete()
        
        story_image.save()

        create_activity_item("edited_story", request.user, story)
        
        return HttpResponseRedirect(reverse("story_edit", args=(story.slug,)))
    else:
        return HttpResponse(status=400, content="Error")
Esempio n. 6
0
    def save(self, user):
        story = super(StoryForm, self).save(commit=False)
        from django.template.defaultfilters import slugify
        story.slug = slugify(story.title)
        story.creator = user
        from misc.utils import make_unique
        story.slug = make_unique(story, lambda x: Story.objects.filter(slug__exact=x.slug).exclude(pk=story.pk).count() == 0)
        story.status = Story.STATUS_DRAFT
        story.save()

        for category in self.cleaned_data["categories"]:
            item = CategorizedItem.objects.create(object=story, category=category)

        if story.status == story.STATUS_PUBLIC:
            create_activity_item("created_story", user, story)
            
        return story
Esempio n. 7
0
    def test_cascaded_delete(self):
        c = Client()
        c.login(username='******', password='******')
        photo = TestSubject.objects.create(test=True)
        photo2 = TestSubject.objects.create(test=True)
        activityItem = create_activity_item("placed", User.objects.get(username="******"), photo)
        activityItem.delete()
        self.assertTrue(TestSubject.objects.get(pk=photo.id))

        activityItem2 = create_activity_item("placed", User.objects.get(username="******"), photo2)
        items = users_activity_stream({}, User.objects.get(username="******"),1000)
        self.assertEquals(len(items['activity_items']), 1)

        photo2.delete()

        items = users_activity_stream({}, User.objects.get(username="******"),1000)
        self.assertEquals(len(items['activity_items']), 0)
Esempio n. 8
0
def book_checkout(request, template_name="ebooks/checkout.html", *args, **kwargs):
    ctx = {}

    book_key = kwargs.get('book_key')
    ownership = get_object_or_404(models.Ownership, key__exact=book_key)
    users = User.objects.all()

    if request.POST:
        if request.POST.has_key('submit'):
            if request.POST['submit'] == "Checkout":
                checkout_form = forms.CheckOutForm(request.POST, users=users)
                if checkout_form.is_valid():
                    recipient_id = checkout_form.cleaned_data["to_who"]

                    checkout = models.CheckOut()
                    checkout.user = User.objects.get(id=recipient_id)
                    checkout.book = ownership
                    checkout.notes = request.POST['notes']
                    checkout.save()
                    ownership.checked_out = checkout
                    ownership.save()
                    
                    #activity stream
                    create_activity_item('checkout', checkout.user, ownership)
                    
            if request.POST['submit'] == "Checkin":
                checkout = ownership.checked_out
                checkout.check_in_time = datetime.now()
                checkout.save()

                ownership.checked_out = None
                ownership.save()
                
                #activity stream
                create_activity_item('checkin', checkout.user, ownership)
                
                checkout_form = forms.CheckOutForm(users=users)
                ctx['checkout_form'] = checkout_form
    else:
        checkout_form = forms.CheckOutForm(users=users)
        ctx['checkout_form'] = checkout_form

    ctx.update({'ownership': ownership})

    return render_to_response(template_name, RequestContext(request, ctx))
Esempio n. 9
0
    def save(self, user):
        if self.is_valid():
            profile = user.get_profile()
            lng = self.cleaned_data["lng"]
            lat = self.cleaned_data["lat"]
            profile.location = Point(lng,lat)
            profile.save()

            if profile.path:
                profile.path.append([lng, lat])
                profile.save()
            else:
                array = [ [lng, lat], [ lng, lat ] ]
                profile.path = LineString(array)
                profile.save()

            create_activity_item("user_location_update", user, user)   
        return False
Esempio n. 10
0
def book_info(request, template_name="ebooks/book_info.html", *args, **kwargs):
    """
    Display the information for the book
    """
    ctx = {}

    book_slug = kwargs.get('book_slug')

    book = get_object_or_404(models.Book, slug=book_slug)

    error = None
    if request.FILES.has_key("book"):
        # We are getting a book uploaded
        filename = request.FILES["book"].name
        ext = os.path.splitext(filename)[1].replace('.','')
        if (ext, ext) not in models.FORMAT_CHOICES:
            error = "invalid filetype: %s" % filename

        if not error:
            try:
                format = models.Format.objects.get(ebook=book, format=ext)
                error = "Format exists: %s" % format.format

            except models.Format.DoesNotExist, e:
                format = models.Format()
                format.ebook = book
                format.format = ext
                format.uploaded_by = request.user

                f = NamedTemporaryFile(delete=False)
                f.write(request.FILES["book"].read())
                f.filename = filename
                f.close()

                format.ebook_file.save(
                    "temp_filename.%s" % ext,
                    File(open(f.name))
                )

                format.save()
                os.unlink(f.name)
                
                #activity stream
                create_activity_item('upload', request.user, format)
Esempio n. 11
0
    def test_batching(self):
        c = Client()
        c.login(username='******', password='******')
        photo = TestSubject.objects.create(test=True)
        photo2 = TestSubject.objects.create(test=True)
        photo.save()
        activityItem1 = create_activity_item("placed", \
                                             User.objects.get(username="******"),\
                                             photo)
        self.assertTrue(activityItem1)
        self.assertEquals(activityItem1.is_batched, False)
        self.assertEquals(activityItem1.subjects.count(), 1)

        activityItem2 = create_activity_item("placed", \
                                             User.objects.get(username="******"), \
                                            photo2)
        self.assertTrue(activityItem2)
        self.assertEquals(activityItem2.is_batched, True)
        self.assertEquals(activityItem2.subjects.count(), 2)
Esempio n. 12
0
    def test_batching(self):
        c = Client()
        c.login(username='******', password='******')
        photo = TestSubject.objects.create(test=True)
        photo2 = TestSubject.objects.create(test=True)
        photo.save()
        activityItem1 = create_activity_item("placed", \
                                             User.objects.get(username="******"),\
                                             photo)
        self.assertTrue(activityItem1)
        self.assertEquals(activityItem1.is_batched, False)
        self.assertEquals(activityItem1.subjects.count(), 1)

        activityItem2 = create_activity_item("placed", \
                                             User.objects.get(username="******"), \
                                            photo2)
        self.assertTrue(activityItem2)
        self.assertEquals(activityItem2.is_batched, True)
        self.assertEquals(activityItem2.subjects.count(), 2)
Esempio n. 13
0
def download_format(request, *args, **kwargs):
    download_key = kwargs.get('download_key')

    book_info = b64decode(download_key).split('::')

    download_url = book_info[0]
    book = models.Book.objects.get(pk__exact=book_info[1])
    book_format = book_info[2]

    # count the download towards the user.
    user_download = UserDownload()
    user_download.user = request.user
    user_download.book =book
    user_download.format = book_format
    user_download.save()
    
    #activity stream
    create_activity_item('download', request.user, user_download)

    return HttpResponseRedirect(download_url)
Esempio n. 14
0
    def test_cascaded_delete(self):
        c = Client()
        c.login(username='******', password='******')
        photo = TestSubject.objects.create(test=True)
        photo2 = TestSubject.objects.create(test=True)
        activityItem = create_activity_item("placed",
                                            User.objects.get(username="******"),
                                            photo)
        activityItem.delete()
        self.assertTrue(TestSubject.objects.get(pk=photo.id))

        activityItem2 = create_activity_item(
            "placed", User.objects.get(username="******"), photo2)
        items = users_activity_stream({}, User.objects.get(username="******"),
                                      1000)
        self.assertEquals(len(items['activity_items']), 1)

        photo2.delete()

        items = users_activity_stream({}, User.objects.get(username="******"),
                                      1000)
        self.assertEquals(len(items['activity_items']), 0)
Esempio n. 15
0
 def test_future_activities(self):
     c = Client()
     c.login(username='******', password='******')
     photo = TestSubject.objects.create(test=False)
     photo.save()
     custom_date = datetime.date.today() + datetime.timedelta(3)
     activityItem = create_activity_item("placed2",\
                                         User.objects.get(username="******"),\
                                         photo, custom_date=custom_date)
     self.assertTrue(activityItem)
     self.assertEquals(activityItem.is_batched, False)
     self.assertEquals(activityItem.subjects.count(), 1)
     items = users_activity_stream({}, User.objects.get(username="******"),1000)
     self.assertEquals(len(items['activity_items']), 0)
     activityItem.delete()
Esempio n. 16
0
 def test_future_activities(self):
     c = Client()
     c.login(username='******', password='******')
     photo = TestSubject.objects.create(test=False)
     photo.save()
     custom_date = datetime.date.today() + datetime.timedelta(3)
     activityItem = create_activity_item("placed2",\
                                         User.objects.get(username="******"),\
                                         photo, custom_date=custom_date)
     self.assertTrue(activityItem)
     self.assertEquals(activityItem.is_batched, False)
     self.assertEquals(activityItem.subjects.count(), 1)
     items = users_activity_stream({}, User.objects.get(username="******"),
                                   1000)
     self.assertEquals(len(items['activity_items']), 0)
     activityItem.delete()
Esempio n. 17
0
def activity_stream_comment(sender, comment, request, *args, **kwargs):
    create_activity_item("comment", request.user, comment)
Esempio n. 18
0
def like(request, id):
    subject = get_object_or_404(ActivityStreamItem, pk=id)
    create_activity_item("likes", request.user, subject)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER','/'))
Esempio n. 19
0
def posted_comment(sender, instance=None, **kwargs):
    if instance is None:
        return
    
    if not isinstance(instance.content_object, ActivityStreamItem):
        create_activity_item("posted_comment", instance.user, instance)
Esempio n. 20
0
def like(request, id):
    subject = get_object_or_404(ActivityStreamItem, pk=id)
    create_activity_item("likes", request.user, subject)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))