assignments/views.py
def assignment_create(request, **kwargs):
    """ View for creating and editing model Assignment using Assignment Form"""
    if request.method == "POST":
        assignment_id = kwargs.get('assignment_id', None)
        if assignment_id is not None:
            assignment = get_object_or_404(Assignment, pk=assignment_id)
            if assignment.owner != request.user.id:
                raise Http404
            form = AssignmentForm(request.POST, instance=assignment)
        else:
            form = AssignmentForm(request.POST)
        if form.is_valid():
            if form.instance.pk is None:  # New assignment, create folder
                assignment = form.save(commit=False)
                assignment.owner = request.user.id
                assignment.save()  # we have to save in order to get the pk
                assignment.absolute_path = "%s/%s/%s" % (settings.MEDIA_ROOT, request.user.id, assignment.name)
                assignment.save()
                if not os.path.exists(assignment.absolute_path):
                    os.makedirs(assignment.absolute_path)
                if assignment.git_password != "":
                    url_matches = re.findall("(.*?)://(.*?)$", assignment.git_source)
                    if url_matches[0][0] == 'https':
                        git_url = "%s://%s:%s@%s" % (url_matches[0][0],
                                                         html.escape(assignment.git_username),
                                                         html.escape(assignment.git_password),
                                                         url_matches[0][1])
                        git.Repo.clone_from(git_url, assignment.absolute_path)
                    else:
                        git.Repo.clone_from(assignment.git_source, assignment.absolute_path)
                else:
                    git.Repo.clone_from(assignment.git_source, assignment.absolute_path)
            else:  # Existing assignment, move folder
                old_assignment = get_object_or_404(Assignment, pk=assignment_id)
                assignment = form.save(commit=False)
                if old_assignment.name != assignment.name:
                    assignment.absolute_path = "%s/%s/%s" % (settings.MEDIA_ROOT, request.user.id, assignment.name)
                    shutil.move("%s/%s" % (settings.BASE_DIR, old_assignment.absolute_path),
                                "%s/%s" % (settings.BASE_DIR, assignment.absolute_path))
                assignment.save()
            os.chmod("%s/%s" % (settings.BASE_DIR, assignment.absolute_path), 0o777)
            return redirect('filemanager:index', inner_path="%s" % assignment.name)
    else:
        assignment_id = kwargs.get('assignment_id', None)
        if assignment_id is not None:
            assignment = get_object_or_404(Assignment, pk=assignment_id)
            if assignment.owner != request.user.id:
                raise Http404
            form = AssignmentForm(instance=assignment)
        else:
            form = AssignmentForm()
    return render(request, 'assignments/assignment_create.html', {'form': form})
 View for creating and editing model Assignment using Assignment Form
View for creating and editing model Assignment using Assignment Form
View for creating and editing model Assignment using Assignment Form
def assignment_create(request, **kwargs):
    if request.method == "POST":
        assignment_id = kwargs.get('assignment_id', None)
        if assignment_id is not None:
            assignment = get_object_or_404(Assignment, pk=assignment_id)
            if assignment.owner != request.user.id:
                raise Http404
            form = AssignmentForm(request.POST, instance=assignment)
        else:
            form = AssignmentForm(request.POST)
        if form.is_valid():
            if form.instance.pk is None:  
                assignment = form.save(commit=False)
                assignment.owner = request.user.id
                assignment.save()  
                assignment.absolute_path = "%s/%s/%s" % (settings.MEDIA_ROOT, request.user.id, assignment.name)
                assignment.save()
                if not os.path.exists(assignment.absolute_path):
                    os.makedirs(assignment.absolute_path)
                if assignment.git_password != "":
                    url_matches = re.findall("(.*?)://(.*?)$", assignment.git_source)
                    if url_matches[0][0] == 'https':
                        git_url = "%s://%s:%s@%s" % (url_matches[0][0],
                                                         html.escape(assignment.git_username),
                                                         html.escape(assignment.git_password),
                                                         url_matches[0][1])
                        git.Repo.clone_from(git_url, assignment.absolute_path)
                    else:
                        git.Repo.clone_from(assignment.git_source, assignment.absolute_path)
                else:
                    git.Repo.clone_from(assignment.git_source, assignment.absolute_path)
            else:  
                old_assignment = get_object_or_404(Assignment, pk=assignment_id)
                assignment = form.save(commit=False)
                if old_assignment.name != assignment.name:
                    assignment.absolute_path = "%s/%s/%s" % (settings.MEDIA_ROOT, request.user.id, assignment.name)
                    shutil.move("%s/%s" % (settings.BASE_DIR, old_assignment.absolute_path),
                                "%s/%s" % (settings.BASE_DIR, assignment.absolute_path))
                assignment.save()
            os.chmod("%s/%s" % (settings.BASE_DIR, assignment.absolute_path), 0o777)
            return redirect('filemanager:index', inner_path="%s" % assignment.name)
    else:
        assignment_id = kwargs.get('assignment_id', None)
        if assignment_id is not None:
            assignment = get_object_or_404(Assignment, pk=assignment_id)
            if assignment.owner != request.user.id:
                raise Http404
            form = AssignmentForm(instance=assignment)
        else:
            form = AssignmentForm()
    return render(request, 'assignments/assignment_create.html', {'form': form})
def assignment_create ( request , ** kwargs ) : if request . method == "POST" : assignment_id = kwargs . get ( 'assignment_id' , None ) if assignment_id is not None : assignment = get_object_or_404 ( Assignment , pk = assignment_id ) if assignment . owner != request . user . id : raise Http404 form = AssignmentForm ( request . POST , instance = assignment ) else : form = AssignmentForm ( request . POST ) if form . is_valid ( ) : if form . instance . pk is None : assignment = form . save ( commit = False ) assignment . owner = request . user . id assignment . save ( ) assignment . absolute_path = "%s/%s/%s" % ( settings . MEDIA_ROOT , request . user . id , assignment . name ) assignment . save ( ) if not os . path . exists ( assignment . absolute_path ) : os . makedirs ( assignment . absolute_path ) if assignment . git_password != "" : url_matches = re . findall ( "(.*?)://(.*?)$" , assignment . git_source ) if url_matches [ 0 ] [ 0 ] == 'https' : git_url = "%s://%s:%s@%s" % ( url_matches [ 0 ] [ 0 ] , html . escape ( assignment . git_username ) , html . escape ( assignment . git_password ) , url_matches [ 0 ] [ 1 ] ) git . Repo . clone_from ( git_url , assignment . absolute_path ) else : git . Repo . clone_from ( assignment . git_source , assignment . absolute_path ) else : git . Repo . clone_from ( assignment . git_source , assignment . absolute_path ) else : old_assignment = get_object_or_404 ( Assignment , pk = assignment_id ) assignment = form . save ( commit = False ) if old_assignment . name != assignment . name : assignment . absolute_path = "%s/%s/%s" % ( settings . MEDIA_ROOT , request . user . id , assignment . name ) shutil . move ( "%s/%s" % ( settings . BASE_DIR , old_assignment . absolute_path ) , "%s/%s" % ( settings . BASE_DIR , assignment . absolute_path ) ) assignment . save ( ) os . chmod ( "%s/%s" % ( settings . BASE_DIR , assignment . absolute_path ) , 0o777 ) return redirect ( 'filemanager:index' , inner_path = "%s" % assignment . name ) else : assignment_id = kwargs . get ( 'assignment_id' , None ) if assignment_id is not None : assignment = get_object_or_404 ( Assignment , pk = assignment_id ) if assignment . owner != request . user . id : raise Http404 form = AssignmentForm ( instance = assignment ) else : form = AssignmentForm ( ) return render ( request , 'assignments/assignment_create.html' , { 'form' : form } )
View for creating and editing model Assignment using Assignment Form
View for creating and editing model Assignment using Assignment Form
""" View for creating and editing model Assignment using Assignment Form""" # New assignment, create folder # we have to save in order to get the pk # Existing assignment, move folder
request