<commit_before>from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import redirect, render, get_object_or_404
from scheduler.models import *
import datetime
import parsedatetime.parsedatetime as pdt
from itertools import chain

@login_required
def dashboard(request):
    message = {}
    has_delete_permission = request.user.has_perm('scheduler.add_edit_post')
    if request.method == "POST" and has_delete_permission:
        post_id = request.POST.get('post_id_to_delete',None)
        post = get_object_or_404(SMPost, pk=post_id)
        post.delete()
        message = {
            "mtype":"success",
            "mtext":"Your post was deleted",
        }
        
    alt_date = None
    if request.method == "GET":
        date_change_str = request.GET.get('date',None)
        print date_change_str
        print "BLAH"
        if date_change_str:
            cal = pdt.Calendar()
            date_change = cal.parse(date_change_str)
            if date_change[1] == 1 or date_change[1] == 3:
                alt_date = datetime.date(date_change[0][0], date_change[0][1], date_change[0][2])

    if alt_date:
        view_date = alt_date
    else:
        if datetime.datetime.now().hour <= 4:
            view_date = datetime.date.today()
        else:
            view_date = datetime.date.today() + datetime.timedelta(days=1)

    tomorrow_posts = SMPost.objects.filter(pub_date=view_date)
    lost_posts = SMPost.objects.filter(pub_date=None)

    context = {
        "user" : request.user,
        "sections" : Section.objects.all(),
        "smposts" : list(chain(tomorrow_posts, lost_posts)),
        "message" : message,
        "view_date" : view_date,
    }
    return render(request, 'scheduler/dashboard.html', context)

@login_required
def edit(request, post_id, post=None):
    if not post:
        post = get_object_or_404(SMPost, pk=post_id)
    
    if post.sent or not request.user.has_perm('scheduler.add_edit_post'):
        message = {
            "mtype":"status",
        }
        
        if not request.user.has_perm('scheduler.add_edit_post'):
            message['mtext'] = "You do not have permission to edit this post"
        
        if post.sent:
            message['mtext'] = "This post has already been sent and cannot be edited"
        
        context = {
            "user" : request.user,
            "sections" : Section.objects.all(),
            "post" : post,
            "message" : message,
        }
        return render(request, 'scheduler/view.html', context)
    
    
    message = {}
    if request.method == "POST" and request.user.has_perm('scheduler.add_edit_post'):
        post.story_url = request.POST.get('url',None)
        post.slug = request.POST.get('slug',None)
        try:
            post.section = Section.objects.get(pk=request.POST.get('section',None))
        except:
            post.section = None
        post.post_twitter = request.POST.get('tweet',None)
        post.post_facebook = request.POST.get('fb',None)
        date_str = request.POST.get('pub_date',None)
        time_str = request.POST.get('pub_time',None)
        
        # Date<commit_msg>If the post has already been approved by copy or online you cannot edit it unless you have the approval permission.<commit_after>from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import redirect, render, get_object_or_404
from scheduler.models import *
import datetime
import parsedatetime.parsedatetime as pdt
from itertools import chain

def can_edit_post(user, post):
    if (user.has_perm('scheduler.add_edit_post') and
        (user.has_perm('scheduler.approve_copy') or not post.pub_ready_copy) and
        (user.has_perm('scheduler.approve_online') or not post.pub_ready_online) and
        not post.sent):
        return True
    return False

@login_required
def dashboard(request):
    message = {}
    has_delete_permission = request.user.has_perm('scheduler.add_edit_post')
    if request.method == "POST" and has_delete_permission:
        post_id = request.POST.get('post_id_to_delete',None)
        post = get_object_or_404(SMPost, pk=post_id)
        post.delete()
        message = {
            "mtype":"success",
            "mtext":"Your post was deleted",
        }
        
    alt_date = None
    if request.method == "GET":
        date_change_str = request.GET.get('date',None)
        if date_change_str:
            cal = pdt.Calendar()
            date_change = cal.parse(date_change_str)
            if date_change[1] == 1 or date_change[1] == 3:
                alt_date = datetime.date(date_change[0][0], date_change[0][1], date_change[0][2])

    if alt_date:
        view_date = alt_date
    else:
        if datetime.datetime.now().hour <= 4:
            view_date = datetime.date.today()
        else:
            view_date = datetime.date.today() + datetime.timedelta(days=1)

    tomorrow_posts = SMPost.objects.filter(pub_date=view_date)
    lost_posts = SMPost.objects.filter(pub_date=None)

    context = {
        "user" : request.user,
        "sections" : Section.objects.all(),
        "smposts" : list(chain(tomorrow_posts, lost_posts)),
        "message" : message,
        "view_date" : view_date,
    }
    return render(request, 'scheduler/dashboard.html', context)

@login_required
def edit(request, post_id, post=None):
    if not post:
        post = get_object_or_404(SMPost, pk=post_id)
    
    if post.sent or not can_edit_post(request.user, post):
        message = {
            "mtype":"status",
        }
        
        if not can_edit_post(request.user, post):
            message['mtext'] = "You do not have permission to edit this post"
        
        if post.sent:
            message['mtext'] = "This post has already been sent and cannot be edited"
        
        context = {
            "user" : request.user,
            "sections" : Section.objects.all(),
            "post" : post,
            "message" : message,
        }
        return render(request, 'scheduler/view.html', context)
    
    
    message = {}
    if request.method == "POST" and can_edit_post(request.user, post):
        post.story_url = request.POST.get('url',None)
        post.slug = request.POST.get('slug',None)
        try:
            post.section = Section.objects.get(pk=request.POST.get('section',None))
        except:
            post.section = None
        post.post_twitter = request.POST.get('tweet',None)
        post.post_facebook = request.POST.get('fb',None)
        date_str = request.POST.get('pub_date',None)
        time_str = request.POST.get('pub_time',None)
        
        # Date
{'fraction_non_alphanumeric': 0.1129976580796253, 'fraction_numerical': 0.006147540983606557, 'mean_word_length': 2.495140664961637, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7355848', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2079, 'n_words': 439}