Example #1
0
def edit(request, id, form_class=BlogForm, template_name="blog/edit.html"):
    post = get_object_or_404(Post, id=id)

    if request.method == "POST":
        if post.author != request.user:
            request.user.message_set.create(message="You can't edit posts that aren't yours")
            return HttpResponseRedirect(blog.get_absolute_url())
        blog_form = form_class(request.user, request.POST, request.FILES, instance=post)
    
        if blog_form.is_valid():
            old_title = post.title
            blog = blog_form.save(commit=False)
            if old_title != blog.title:
                from django.template.defaultfilters import slugify
                blog.slug = slugify(blog.title)
                from misc.utils import make_unique 
                blog.slug = make_unique(blog, lambda x: Post.objects.filter(slug__exact=x.slug).exclude(id=x.id).count() == 0)

            blog.save()
            request.user.message_set.create(message=_("Successfully updated post '%s'") % blog.title)
            #if notification:
                #if blog.status == 2: # published
                    #if friends: # @@@ might be worth having a shortcut for sending to all friends
                        #notification.send((x['friend'] for x in Friendship.objects.friends_for_user(blog.author)), "blog_friend_post", {"post": blog})
            
            return HttpResponseRedirect(blog.get_absolute_url())
    else:
        blog_form = form_class(instance=post)

    return render_to_response(template_name, {
        "blog_form": blog_form,
        "post": post,
    }, context_instance=RequestContext(request))
Example #2
0
 def save(self, user):
     self.user = user
     if self.cleaned_data.get("title", False):
         self.instance.title = self.cleaned_data["title"]
         from django.template.defaultfilters import slugify
         self.instance.slug = slugify(self.cleaned_data["title"])
         from misc.utils import make_unique
         self.instance.slug = make_unique(self.instance, lambda x: Story.objects.filter(slug__exact=x.slug).exclude(pk=x.id).count() == 0)
     return self.instance
Example #3
0
def init_blog_post(blog, request):
    blog.author = request.user
    from django.template.defaultfilters import slugify
    blog.slug = slugify(blog.title)

    from misc.utils import make_unique
    blog.slug = make_unique(blog, lambda x: Post.objects.filter(slug__exact=x.slug).exclude(id=x.id).count() == 0)
    
    if settings.BEHIND_PROXY:
        blog.creator_ip = request.META["HTTP_X_FORWARDED_FOR"]
    else:
        blog.creator_ip = request.META['REMOTE_ADDR']
Example #4
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
def store_user(data):
    
    #
    # User
    #
    firstname = data['firstname']
    if not firstname:
        firstname = " "
    lastname = data['lastname']
    if not lastname:
        lastname = " "
    user = User(username=data['name'], first_name=firstname,
                last_name=lastname, email=data['email'], password="******"+data["pass"])
    user.save(force_insert=True)
    
    print "User: "******" "
        
    #profile = Profile(user=user,
    #                    first_name=firstname,
    #                    last_name=lastname,
    #                    city=city,
    #                    about=data['description'])
    #profile.save()
    #account = Account(user=user)
    #account.save()
    
    # avatar
    
    #
    # stories - do them first, since they are containers that we want to add items to
    #
    stories = get_stories(data["user_id"])
    for story in stories:
        print "-Story: "+str(story['story_id'])
        title = story['title']
        if not title:
            title = " "
            
        new_story = Story(title=title,
                          slug=slugify(title),
                      creator=user,
                      description=story['text'],
                      mapmode=convert_mapmode(story['mapmode']),
                      creation_date=convert_time(story['timestamp']),
                      update_date=convert_time(story['timestamp']),
                      privacy=get_privacy_of_old_privacy(story['right_view']),
                      status=get_status_of_old_privacy(story['right_view']))
        
        new_story.slug = make_unique(new_story, lambda x: Story.objects.filter(slug__exact=x.slug).count() == 0)
        
        new_story.save()
        migrated_post = MigratedItem(type="story", old_id=story["story_id"],
                                 new_id=new_story.pk)
        migrated_post.save()
        
        store_tags_of_item(new_story, story['story_id'])
        
        set_hit_count(new_story, story['view_count'])
    
    
    
    
    # lines
    lines = get_lines(data["user_id"])
    if lines:
        for line in lines:
            print "-Line: "+str(line['line_id'])
            linestring = make_linestring_of_string(line["controlpoints"], line["start_marker_id"], line["end_marker_id"])
            if linestring:
                line_post = get_post_of_line(line['line_id'])
                if not line_post:
                    title = " "
                    description = " "
                else:
                    if line_post['title']:
                        title = line_post['title']
                    else:
                        title = " "
                    
                    if line_post['text']:
                        description = line_post['text']
                    else:
                        description = " "
                    
                new_line = GeoLineTag(creator=user,
                                title=title,
                                description=description,
                                line=linestring,
                                )
                
                # add to story
                line_w_story = get_story_of_line(line['line_id'])
                if line_w_story and line_w_story['story_id']:
                    try:
                        new_story = Story.objects.get(pk=get_new_id("story",line_w_story['story_id']))
                        print "Adding line %s (%s) to story %s (%s)"%(line['line_id'], new_line.id, line_w_story['story_id'], new_story.id)
                        new_line.content_object = new_story
                    except:
                        print "Could not add Story"
                    
                new_line.save()
                migrated_line = MigratedItem(type="line", old_id=line["line_id"],
                                         new_id=new_line.pk)
                migrated_line.save()
            
                
    
    
    # video
    videos = get_videos(data["user_id"])
    for video in videos:
        print "-Video: "+str(video['video_id'])
        if video['medialocation']:
            import re
            video_id = re.findall(r"/v/(.+)", video['medialocation'])[0]
            import_url = "http://www.youtube.com/watch?v="+video_id
            comment = video['text']
            if not comment:
                comment = " "
                
            new_video = Video(creator=user,
                            title=video['title'],
                            comment=comment,
                            was_uploaded=False,
                            import_url=import_url,
                            thumbnail_url=video['thumb1'],
                            safetylevel=get_privacy_of_old_privacy(video['right_view']),
                            last_modified=convert_time(video['timestamp']),
                            date_added=convert_time(video['timestamp']),
                            )
            new_video.save()
            migrated_video = MigratedItem(type="video", old_id=video["video_id"],
                                     new_id=new_video.pk)
            migrated_video.save()
            
            # add to story
            if video["story_id"]:
                story = Story.objects.get(pk=get_new_id("story", video["story_id"]))
                
                count = StoryLineItem.objects.filter(creator=user, story=story, timestamp_start__lte=convert_time(video['timestamp'])).count()
                print "Adding video %s (%s) to story %s (%s) at position: %s "%(video['video_id'], new_video.id, video["story_id"], story.id, count)
                
                text = video['text']
                if not text:
                    text = " "
                    
                story_line_item = StoryLineItem(creator=user, story=story, text=text,
                                         timestamp_start=convert_time(video['timestamp']), timestamp_end=convert_time(video['timestamp']))
                story_line_item.save()
                story_line_item_media = StoryLineItemMedia(storylineitem=story_line_item, content_object=new_video)
                story_line_item_media.save()
                
                # geotag
                if video['mapobject_id']:
                    geotag_item(video["video_id"], story_line_item, user)
            
            if video['mapobject_id']:
                geotag_item(video["video_id"], new_video, user)
        
            store_tags_of_item(new_video, video["video_id"])
            set_hit_count(new_video, video['view_count'])
            
    # posts
    posts = get_posts(data["user_id"])
    for post in posts:
        print "-Post: "+str(post['post_id'])
        if post['story_id']:
            post_text = post['text']
            if not post_text:
                post_text = " "
            
            try:
                post_story = Story.objects.get(pk=get_new_id("story", post['story_id']))
                count = StoryLineItem.objects.filter(creator=user, story=story, timestamp_start__lte=convert_time(post['timestamp'])).count()
                print "Adding post %s to story %s (%s) at position: %s "%(post['post_id'], post["story_id"], post_story.id, count)
                new_post = StoryLineItem(creator=user, story=post_story, text=post_text,
                                         timestamp_start=convert_time(post['timestamp']),
                                         timestamp_end=convert_time(post['timestamp']))
                new_post.save()
                migrated_post = MigratedItem(type="post", old_id=post["post_id"],
                                         new_id=new_post.pk)
                migrated_post.save()
            
                if post['mapobject_id']:
                    geotag_item(post["post_id"], new_post, user)
                    
                store_tags_of_item(new_post, post["post_id"])
                set_hit_count(new_post, post['view_count'])
            except:
                print "Could not add to Story"
    
    
    # images
    images = get_images(data["user_id"])
    if images:
        for image in images:
            print "-Image: "+str(image['image_id'])
            caption = image['text']
            if not caption:
                caption = " "
            
            title = image['title']
            if not title:
                title = " "
                
            new_image = Image(member=user,
                            title=title,
                            caption=caption,
                            date_added=convert_time(image['timestamp']),
                            )
            
            
            try:
                new_image.image.save(os.path.basename(image["filename"]+".jpg"), ContentFile(open(path_to_images+image["filename"]+".jpg", "r").read()))
                migrated_image = MigratedItem(type="image", old_id=image["image_id"],
                                         new_id=new_image.pk)
                new_image.save()
                migrated_image.save()
                
                if image["story_id"]:
                    story = Story.objects.get(pk=get_new_id("story", image["story_id"]))
                    count = StoryLineItem.objects.filter(creator=user, story=story, timestamp_start__lte=convert_time(image['timestamp'])).count()
                    print "Adding image %s (%s) to story %s (%s) at position: %s "%(image['image_id'], new_image.id, image["story_id"], story.id, count)
                    story_line_item = StoryLineItem(creator=user, story=story, text=caption,
                                             position=count,
                                             timestamp_start=convert_time(image['timestamp']),
                                             timestamp_end=convert_time(image['timestamp']))
                    story_line_item.save()
                    story_line_item_media = StoryLineItemMedia(storylineitem=story_line_item, content_object=new_image)
                    story_line_item_media.save()
                    
                    if image['mapobject_id']:
                        geotag_item(image['image_id'], story_line_item, user)
            
                if image['mapobject_id']:
                    geotag_item(image['image_id'], new_image, user)
                    
                store_tags_of_item(new_image, image["image_id"])
                set_hit_count(new_image, image['view_count'])
            except:
                print "Could not store image: %s filename: %s"%(image['image_id'], image['filename'])
                
    # story image
    
    
    # store user
    migrated_user = MigratedItem(type="user", old_id=data["user_id"],
                                 new_id=user.pk)
    migrated_user.save()
    make_avatar(data)
    
    # flickr images
    # TODO:
    
Example #6
0
def new(request, event_id=None,
        form_class=EventForm, 
        template_name="events/new_event.html"):
    
    event = None
    
    if request.method == "POST":
        event_form = form_class(request.user, request.POST, request.FILES)
        if event_id:
            event = Event.objects.get(pk=event_id)
            event.pk = None
            event.id = None
            event_form = form_class(request.user, request.POST, request.FILES,
                                    instance=event)
        if event_form.is_valid():
            event = event_form.save(commit=False)
            event.author = request.user
            
            from django.template.defaultfilters import slugify
            event.slug = slugify(event.title)

            from misc.utils import make_unique
            event.slug = make_unique(event.slug,
                    lambda x: Event.objects.filter(slug__exact=x).count() == 0)

            if not event.image:
                template_event_id = request.GET.get("template_event_id", False)
                if template_event_id:
                    template_event = Event.objects.get(pk=template_event_id)
                    event.image = template_event.image

            if not event.location and \
                not (event.location_name or event.location_adress or event.location_city \
                 or event.location_zip_code or event.location_country):
                if request.POST.get("custom_location_name", False) and \
                   request.POST.get("custom_location_adress", False) and \
                   request.POST.get("custom_location_zip_code", False) and \
                   request.POST.get("custom_location_city", False):
                    event.location_name = request.POST.get("custom_location_name")
                    event.location_adress = request.POST.get("custom_location_adress")
                    event.location_city = request.POST.get("custom_location_city")
                    event.location_zip_code = request.POST.get("custom_location_zip_code")
                    event.location_country = request.POST.get("custom_location_country")
                else:
                    event_form.fields['location'].widget.errors = \
                                            [_(u"Venue has to be specified")]
                    return render_to_response(template_name, {
                        "event_form": event_form
                    }, context_instance=RequestContext(request))
            event.save()
            
            

                    
            if notification:
                notification.send([event.author], 
                                  trigger_names[event.type]['notification'], 
                                  {"event": event})

            
            return HttpResponseRedirect(reverse('event_created',
                                                kwargs={'event_id': event.id}))
            
        else:
            return render_to_response(template_name, {
                "event_form": event_form
            }, context_instance=RequestContext(request))

    template_event_id = request.GET.get("template_event_id", False)
    if template_event_id:
        template_event = Event.objects.get(pk=template_event_id)
        event_form = form_class(request.user, instance=template_event)
    elif event_id:
        event = Event.objects.get(pk=event_id)
        event.pk = None
        event.id = None
        event_form = form_class(request.user, instance=event)
    else:
        event_form = form_class(request.user)

    return render_to_response(template_name, {
        "event_form": event_form,
        "template_event_id": template_event_id,
    }, context_instance=RequestContext(request))
Example #7
0
def new(request,
        event_id=None,
        form_class=EventForm,
        template_name="events/new_event.html"):

    event = None

    if request.method == "POST":
        event_form = form_class(request.user, request.POST, request.FILES)
        if event_id:
            event = Event.objects.get(pk=event_id)
            event.pk = None
            event.id = None
            event_form = form_class(request.user,
                                    request.POST,
                                    request.FILES,
                                    instance=event)
        if event_form.is_valid():
            event = event_form.save(commit=False)
            event.author = request.user

            from django.template.defaultfilters import slugify
            event.slug = slugify(event.title)

            from misc.utils import make_unique
            event.slug = make_unique(
                event.slug,
                lambda x: Event.objects.filter(slug__exact=x).count() == 0)

            if not event.image:
                template_event_id = request.GET.get("template_event_id", False)
                if template_event_id:
                    template_event = Event.objects.get(pk=template_event_id)
                    event.image = template_event.image

            if not event.location and \
                not (event.location_name or event.location_adress or event.location_city \
                 or event.location_zip_code or event.location_country):
                if request.POST.get("custom_location_name", False) and \
                   request.POST.get("custom_location_adress", False) and \
                   request.POST.get("custom_location_zip_code", False) and \
                   request.POST.get("custom_location_city", False):
                    event.location_name = request.POST.get(
                        "custom_location_name")
                    event.location_adress = request.POST.get(
                        "custom_location_adress")
                    event.location_city = request.POST.get(
                        "custom_location_city")
                    event.location_zip_code = request.POST.get(
                        "custom_location_zip_code")
                    event.location_country = request.POST.get(
                        "custom_location_country")
                else:
                    event_form.fields['location'].widget.errors = \
                                            [_(u"Venue has to be specified")]
                    return render_to_response(
                        template_name, {"event_form": event_form},
                        context_instance=RequestContext(request))
            event.save()

            if notification:
                notification.send([event.author],
                                  trigger_names[event.type]['notification'],
                                  {"event": event})

            return HttpResponseRedirect(
                reverse('event_created', kwargs={'event_id': event.id}))

        else:
            return render_to_response(template_name,
                                      {"event_form": event_form},
                                      context_instance=RequestContext(request))

    template_event_id = request.GET.get("template_event_id", False)
    if template_event_id:
        template_event = Event.objects.get(pk=template_event_id)
        event_form = form_class(request.user, instance=template_event)
    elif event_id:
        event = Event.objects.get(pk=event_id)
        event.pk = None
        event.id = None
        event_form = form_class(request.user, instance=event)
    else:
        event_form = form_class(request.user)

    return render_to_response(template_name, {
        "event_form": event_form,
        "template_event_id": template_event_id,
    },
                              context_instance=RequestContext(request))