Exemplo n.º 1
0
def diary_create(request):
    form = DiaryForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.user = request.user
        if request.user.is_superuser:
            instance.is_public = True
        instance.save()
        messages.success(request, "Successfully Created")
        return HttpResponseRedirect('/')
    context = {
        "form": form,
    }
    return render(request, "diary_form.html", context)
Exemplo n.º 2
0
def DiaryManageAddView(request, username=''):

    user = User.objects.get(username=username)
    form = DiaryForm(request.POST or None)
    if form.is_valid():
        thisTitle = form.cleaned_data['title']
        thisContent = form.cleaned_data['content']
        thisUser = User.objects.get(username=username)

        diary = Diary(user=thisUser, title=thisTitle, content=thisContent)
        diary.save()

        return HttpResponseRedirect(URL_DASHBOARD(user.username))

    return HttpResponseRedirect(URL_DASHBOARD(username))
Exemplo n.º 3
0
def DiaryManageAddView(request, username=''):

    user = User.objects.get(username=username)
    form = DiaryForm(request.POST or None)
    if form.is_valid():
        thisTitle = form.cleaned_data['title']
        thisContent = form.cleaned_data['content']
        thisUser = User.objects.get(username=username)

        diary = Diary(user=thisUser, title=thisTitle, content=thisContent)
        diary.save()

        return HttpResponseRedirect(URL_DASHBOARD(user.username))

    return HttpResponseRedirect(URL_DASHBOARD(username))
Exemplo n.º 4
0
def edit(request, diary_id):
    """Edit an existing diary entry.

    Checks and adds errors for setting a non-draft diary to a draft diary.

    """
    entry = get_object_or_404(DiaryEntry, pk=diary_id)
    if entry.creator != request.user:
        raise PermissionDenied
    was_draft = entry.is_draft
    if request.method == 'POST':
        data = request.POST
        if request.is_ajax():
            data = data.copy()
            data['is_draft'] = True
        form = DiaryForm(creator=entry.creator, data=data,
                         instance=entry)
        if form.is_valid():
            entry = form.save()
            u = request.user
            entries = list(u.diaries.get_all(visitor=u, creator=u)[:10])
            u.diaries.invalidate(*entries)
            # Publishing a diary entry. Notify favorites.
            if was_draft and not entry.is_draft:
                process_diary(entry)
                u.profile.update(
                    coins=u.profile.coins +
                        ucs.ACTIONS_COINS['diary']['daily'])
                update_rewards(request.user, 'diary')
                process_mojo_action(request.user, 'diary_entry')
            if request.is_ajax():
                data = {'is_valid': True,
                        'timestamp': entry.updated.strftime("%s")}
                return JSONResponse(data)
            return HttpResponseRedirect(reverse(
                'diary.single', kwargs=get_kwargs_for_diary_id(entry.pk)))
    else:
        form = DiaryForm(creator=entry.creator, instance=entry)
    c_year, c_month = clean_diary_date(entry.created_day.year,
                                       entry.created_day.month)
    entries = request.user.diaries.get_for_month(visitor=request.user,
                                                 creator=request.user,
                                                 year=c_year, month=c_month)
    calendar_data = get_diary_calendar(entries, c_year, c_month)
    data = {'form': form, 'creator': request.user, 'entry': entry,
            'calendar_data': calendar_data}
    return jingo.render(request, 'diary/edit.html', data)
Exemplo n.º 5
0
def new(request):
    """Create a new diary entry for today."""
    diaries = request.user.diaries.filter(created_day=datetime.date.today())
    # If they already have a diary for today, redirect them to edit
    if diaries:
        return HttpResponseRedirect(reverse('diary.edit',
                                            args=[diaries[0].pk]))
    if request.method == 'POST':
        data = request.POST
        if request.is_ajax():
            data = data.copy()
            data['is_draft'] = True
        form = DiaryForm(creator=request.user, data=data)
        if form.is_valid():
            entry = form.save()
            u = request.user
            entries = list(u.diaries.get_all(visitor=u, creator=u)[:10])
            u.diaries.invalidate(*entries)
            # Register user to receive email when a comment is posted.
            NewCommentEvent.notify(request.user, entry)
            # Send news feed item to creator's favorites.
            if not entry.is_draft:
                process_diary(entry)
                u.profile.update(
                    coins=u.profile.coins +
                        ucs.ACTIONS_COINS['diary']['daily'])
                update_rewards(request.user, 'diary')
                process_mojo_action(request.user, 'diary_entry')
            if request.is_ajax():
                data = {'is_valid': True,
                        'is_new': True,
                        'url': reverse('diary.edit', args=[entry.pk]),
                        'timestamp': entry.updated.strftime("%s")}
                return JSONResponse(data)
            return HttpResponseRedirect(reverse('diary.list_all',
                                        args=[request.user.username]))
    else:   # request.method == 'GET'
        form = DiaryForm(creator=request.user)
    c_year, c_month = clean_diary_date()
    entries = request.user.diaries.get_for_month(visitor=request.user,
                                                 creator=request.user,
                                                 year=c_year, month=c_month)
    calendar_data = get_diary_calendar(entries, c_year, c_month)
    data = {'form': form, 'creator': request.user,
            'calendar_data': calendar_data}
    return jingo.render(request, 'diary/new.html', data)
Exemplo n.º 6
0
def DiaryManageUpdateView(request, username='', diaryid=''):
 
    diary = Diary.objects.get(id=diaryid)
    user = User.objects.get(username=username)

    form = DiaryForm(request.POST or None)

    if form.is_valid():
        diary.title = form.cleaned_data['title']
        diary.content = form.cleaned_data['content']
        diary.save()

        return HttpResponseRedirect(URL_DASHBOARD(user.username))
    else:
        template_name = 'master/diary_manage_update.html'
        template_value = {'form': form, 'diary': diary, 'user': user}
        return render_to_response(template_name, template_value, context_instance=RequestContext(request))
Exemplo n.º 7
0
def diary_edit(request, pk):
    template = 'diary_edit.html'
    day = get_object_or_404(Diary, pk=pk)
    if request.method == "POST":
        form = DiaryForm(request.POST or None,
                         request.FILES or None,
                         instance=day)
        try:
            if form.is_valid():
                form.save()
                messages.success(request, "Your MyDay is updated")

        except Exception as e:
            messages.warning(
                request,
                'Your update was not saved due to an error: {}'.format(e))

    else:
        form = DiaryForm(instance=day)

    context = {
        'form': form,
        'day': day,
    }

    return render(request, template, context)
Exemplo n.º 8
0
def create(request):

    # forms = formsets.formset_factory(Diary_entry, can_delete=True)
    if request.POST:
        # the_date = request.POST.get('the_date', 'what')
        the_date = request.POST["the_date"]

        print the_date
        # formset = forms(request.POST)
        x = 1
        pform = DiaryForm(request.POST, instance=Diary_entry())
        cforms = SecondDiaryForm(request.POST, prefix=str(x), instance=Diary_entry())
        x = 2
        cforms2 = SecondDiaryForm(request.POST, prefix=str(x), instance=Diary_entry())

        if pform.is_valid() and cforms.is_valid():
            obj = pform.save(commit=False)
            obj.user = request.user
            obj.entry_date = the_date
            obj.save()
            obj2 = cforms.save(commit=False)
            obj2.user = request.user
            obj2.entry_date = the_date
            obj2.save()
            obj3 = cforms2.save(commit=False)
            obj3.user = request.user
            obj3.entry_date = the_date
            obj3.save()
        # new_poll = pform.save()
        # for cf in cforms:
        # new_choice = cf.save(commit=False)
        # new_choice.poll = new_poll
        # new_choice.save()
        return HttpResponseRedirect("/diary/create")
    else:
        pform = DiaryForm(instance=Diary_entry())
        x = 1
        cforms = SecondDiaryForm(prefix=str(x), instance=Diary_entry())
        x = 2
        cforms2 = SecondDiaryForm(prefix=str(x), instance=Diary_entry())
        return render_to_response(
            "diary/create_diary.html",
            {"poll_form": pform, "choice_forms": cforms, "choice_forms2": cforms2},
            context_instance=RequestContext(request),
        )
Exemplo n.º 9
0
def DiaryManageUpdateView(request, username='', diaryid=''):

    diary = Diary.objects.get(id=diaryid)
    user = User.objects.get(username=username)

    form = DiaryForm(request.POST or None)

    if form.is_valid():
        diary.title = form.cleaned_data['title']
        diary.content = form.cleaned_data['content']
        diary.save()

        return HttpResponseRedirect(URL_DASHBOARD(user.username))
    else:
        template_name = 'master/diary_manage_update.html'
        template_value = {'form': form, 'diary': diary, 'user': user}
        return render_to_response(template_name,
                                  template_value,
                                  context_instance=RequestContext(request))
Exemplo n.º 10
0
 def post(self,request):
     name = request.POST.get('name',None)
     if name == None:
         raise Http404()
     
     data = request.POST.copy()
     try:
         diary = Diary.objects.get(name = name)
         raise Http404()
     except:
         pass
     
     data['user'] = request.user.id
     form = DiaryForm(data)
     if(form.is_valid()):
         form.save()
         diary_id = Diary.objects.get(name=data['name']).id
         return HttpResponseRedirect(reverse('diary_home',kwargs={'diary_id':diary_id}))
     else:
         raise Http404()
Exemplo n.º 11
0
def new(request):
    if request.method == 'GET':
        new_diaryform = DiaryForm(initial={'creator': request.user,
                                       'text': 'Start Typing Here',
                                       'created': datetime.datetime.now(),
                                       'title': 'Enter title here',
                                       'is_draft': True, })
        data = {'diaryform' : new_diaryform}
        return jingo.render(request,
                        'diary/new_diary.html',
                        data, )
    if request.method == 'POST':
        form = DiaryForm(request.POST)
        if form.is_valid():
            new_diary = form.save(commit=False)
            new_diary.creator = request.user
            new_diary.created = datetime.datetime.now()
            new_diary.save()
            form.save_m2m()
    return HttpResponseRedirect(reverse('diary.views.personal'))
Exemplo n.º 12
0
def edit(request, diary_id):
    entry = get_object_or_404(Diary, pk=diary_id)
    if (entry.creator == request.user):
        if request.method == 'GET':
            diaryform = DiaryForm(instance=entry)
            data = {'diaryform': diaryform,
                    'diary_id': entry.pk}
            return jingo.render(request, 'diary/edit_diary.html', data)
        if request.method == 'POST':
            diaryform = DiaryForm(request.POST)
            if diaryform.is_valid():
                temp_entry = diaryform.save(commit=False)
                entry.title = temp_entry.title
                entry.text = temp_entry.text
                entry.created = datetime.datetime.now()
                entry.is_draft = temp_entry.is_draft
                entry.save()
                # form.save_m2m()
                return HttpResponseRedirect(reverse('diary.views.view_diary',
                                        args=[diary_id]))
    return index(request)
Exemplo n.º 13
0
def diary(request):
    if request.method == "POST":
        form = DiaryForm(request.POST or None, request.FILES or None)
        #        try:
        #            if form.is_valid():
        #                form.save()
        #                messages.success(request, "Your MyDay is updated")

        #       except Exception as e:
        #           messages.warning(request, 'Your update was not saved due to an error: {}'.format(e))

        if form.is_valid():
            instance = form.save(commit=False)
            instance.manage = request.user
            instance.save()
            messages.success(request, "MyDay Added!")
        else:
            messages.warning(
                request, 'Not saved - wrong date format or duplicate date')
        return redirect('log')
    else:
        return render(request, 'diary.html')
Exemplo n.º 14
0
def diary_update(request, id=None):
    instance = get_object_or_404(Diary, id=id)
    if instance.user == request.user:
        form = DiaryForm(request.POST or None,
                         request.FILES or None,
                         instance=instance)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            messages.success(request, "Item Saved", extra_tags='html_safe')
            return HttpResponseRedirect('/')

        context = {
            "title": instance.title,
            "instance": instance,
            "form": form,
        }
        return render(request, "diary_form.html", context)

    else:
        messages.error(request, "You Dont have permission to edit")
        return HttpResponseRedirect('/')
Exemplo n.º 15
0
def DashboardView(request, username=''):

    user = User.objects.get(username=username)

    # Goals:
    ## Todo
    todo_form = TodoForm()
    todos = Todo.objects.filter(user_id=user.id, status=1)  # list all undos
    ## 10K Hours
    tracker_form = TrackerForm()
    trackers = Tracker.objects.filter(user_id=user.id)
    ## Event
    event_form = EventForm()
    events = Event.objects.filter(user_id=user.id)

    # Life:
    ## Mood
    mood_form = MoodForm()
    moods = Mood.objects.filter(user_id=user.id)
    ## Dream
    dream_form = DreamForm()
    dreams = Dream.objects.filter(user_id=user.id)
    ## Diary
    diary_form = DiaryForm()
    diaries = Diary.objects.filter(user_id=user.id)

    # Rendering template
    template_name = 'master/dashboard.html'
    template_value = {
        'TodoForm': todo_form,
        'todos': todos,
        'EventForm': event_form,
        'events': events,
        'TrackerForm': tracker_form,
        'trackers': trackers,
        'MoodForm': mood_form,
        'moods': moods,
        'DreamForm': dream_form,
        'dreams': dreams,
        'DiaryForm': diary_form,
        'diaries': diaries,
    }
    return render_to_response(template_name,
                              template_value,
                              context_instance=RequestContext(request))
Exemplo n.º 16
0
def create(request):

    #forms = formsets.formset_factory(Diary_entry, can_delete=True)
    if request.POST:
        #the_date = request.POST.get('the_date', 'what')
        the_date = request.POST["the_date"]

        print the_date
        #formset = forms(request.POST)
        x = 1
        pform = DiaryForm(request.POST, instance=Diary_entry())
        cforms = SecondDiaryForm(request.POST,
                                 prefix=str(x),
                                 instance=Diary_entry())
        x = 2
        cforms2 = SecondDiaryForm(request.POST,
                                  prefix=str(x),
                                  instance=Diary_entry())

        if pform.is_valid() and cforms.is_valid():
            obj = pform.save(commit=False)
            obj.user = request.user
            obj.entry_date = the_date
            obj.save()
            obj2 = cforms.save(commit=False)
            obj2.user = request.user
            obj2.entry_date = the_date
            obj2.save()
            obj3 = cforms2.save(commit=False)
            obj3.user = request.user
            obj3.entry_date = the_date
            obj3.save()
#new_poll = pform.save()
#for cf in cforms:
#new_choice = cf.save(commit=False)
#new_choice.poll = new_poll
#new_choice.save()
        return HttpResponseRedirect('/diary/create')
    else:
        pform = DiaryForm(instance=Diary_entry())
        x = 1
        cforms = SecondDiaryForm(prefix=str(x), instance=Diary_entry())
        x = 2
        cforms2 = SecondDiaryForm(prefix=str(x), instance=Diary_entry())
        return render_to_response('diary/create_diary.html', {
            'poll_form': pform,
            'choice_forms': cforms,
            'choice_forms2': cforms2
        },
                                  context_instance=RequestContext(request))