<commit_before>(function(angular){
    'use strict';
    var app = angular.module('new-course');

    app.factory('FormUpload', ['$http', '$q', function($http, $q){
        return function() {
            var formData = new FormData();

            this.addField = function(name, value) {
                formData.append(name, value);
            };

            this.sendTo = function(url) {
                var deferred = $q.defer();

                formData.append('csrfmiddlewaretoken',
                                /csrftoken=(\w+)/.extract(document.cookie, 1));

                var oReq = new XMLHttpRequest();

                oReq.onreadystatechange = function(){
                    if(this.readyState !== 4) return;

                    var response = {};
                    response.data = angular.fromJson(this.responseText || '{}');
                    response.status = this.status;
                    console.log(this);

                    if( this.status === 200  ) {
                        deferred.resolve(response);
                    } else {
                        deferred.reject(response);
                    }
                };

                oReq.open('POST', url, true);
                oReq.send(formData);

                return deferred.promise;
            };
        };
    }]);

    app.factory('getRestOptions', ['$http', function($http){
        return function(url){
            return $http({
                method:'POST',
                url: url,
                data:'_method=OPTIONS',
                headers:{'Content-Type':'application/x-www-form-urlencoded'}
            });
        };
    }]);

    app.factory('Course', ['$resource', '$http', 'getRestOptions', function($resource, $http, getRestOptions) {
        var url_template = '/api/course/:id';
        var Course = $resource(url_template, {'id':'@id'});

        Course.prototype.hasVideo = function(){
            return this.intro_video && this.intro_video.youtube_id &&
                   this.intro_video.youtube_id.length > 0;
        };

        Course.prototype.getUrl = function() {
            var that = this;
            return url_template.replace(/\/:(\w+)/g, function(mark, field){
                var part = '';
                if ( that[field] ) {
                    part = '/' + that[field];
                }
                return part;
            });
        };

        getRestOptions(Course.prototype.getUrl()).success(function(data) {
            Course.fields = angular.copy(data.actions.POST);
        });

        return Course;
    }]);

    app.factory('VideoData', ['$document', '$q', function($document, $q){
        var funcName = 'getYoutubeData'+Math.random().toString(16).substring(2);

        var VideoData = function() {
            this.load = function(vid) {
                this.deferred = $q.defer();

                this.src = 'http://gdata.youtube.com/feeds/api/videos/'+
                            vid + '?alt=json&callback=' + funcName;

                var script = $document[0].createElement('script');
                script.type = 'text/javascript';
                script.src = this.src;
                $document[0].body.appendChild(script);

                return this.deferred.promise;
            };
        };
        var vd = new VideoData();<commit_msg>coments on new-course factories code<commit_after>(function(angular){
    'use strict';
    var app = angular.module('new-course');

    /**
     * Provide an object that wraps a FormData and a XMLHttpRequest
     * to upload files. Returns a promise with request info.
     */
    app.factory('FormUpload', ['$http', '$q', function($http, $q){
        return function() {
            var formData = new FormData();

            this.addField = function(name, value) {
                formData.append(name, value);
            };

            this.sendTo = function(url) {
                var deferred = $q.defer();

                formData.append('csrfmiddlewaretoken',
                                /csrftoken=(\w+)/.extract(document.cookie, 1));

                var oReq = new XMLHttpRequest();

                oReq.onreadystatechange = function(){
                    if(this.readyState !== 4) return;

                    var response = {};
                    response.data = angular.fromJson(this.responseText || '{}');
                    response.status = this.status;
                    console.log(this);

                    if( this.status === 200  ) {
                        deferred.resolve(response);
                    } else {
                        deferred.reject(response);
                    }
                };

                oReq.open('POST', url, true);
                oReq.send(formData);

                return deferred.promise;
            };
        };
    }]);

    /**
     * Give a function that send _method=OPTIONS to django rest_framework
     * URL informed and return a promise with result.
     */
    app.factory('getRestOptions', ['$http', function($http){
        return function(url){
            return $http({
                method:'POST',
                url: url,
                data:'_method=OPTIONS',
                headers:{'Content-Type':'application/x-www-form-urlencoded'}
            });
        };
    }]);

    /**
     *  Provide a Course class. The property Class.fields contains the
     *  list of fields that reflects Course model in Django
     */
    app.factory('Course', ['$resource', '$http', 'getRestOptions', function($resource, $http, getRestOptions) {
        var url_template = '/api/course/:id';
        var Course = $resource(url_template, {'id':'@id'});

        Course.prototype.hasVideo = function(){
            return this.intro_video && this.intro_video.youtube_id &&
                   this.intro_video.youtube_id.length > 0;
        };

        Course.prototype.getUrl = function() {
            var that = this;
            return url_template.replace(/\/:(\w+)/g, function(mark, field){
                var part = '';
                if ( that[field] ) {
                    part = '/' + that[field];
                }
                return part;
            });
        };

        getRestOptions(Course.prototype.getUrl()).success(function(data) {
            Course.fields = angular.copy(data.actions.POST);
        });

        return Course;
    }]);


    /**
     * A object that fetch info from Youtube. It expects a video ID and returns
     * a promise that video info will be fetched.
     */
    app.factory('VideoData', ['$document', '$q', function($document, $q){
        var funcName = 'getYoutubeData'+Math.random().toString(16).substring(2);

        var VideoData = function() {
            this.load = function(vid) {
                this.deferred = $q.defer();

                // youtube service
                this.src = 'http://gdata.youtube.com/feeds/api/videos/'+
                            vid + '?alt=json&callback=' + funcName;

                var script = $document[0].createElement('script');
                script.type = 'text/javascript';
                script.src = this.src;
                $document[0].body.appendChild(script);

                return this.deferred.promise;
            };
        };
        var vd = new VideoData();
{'fraction_non_alphanumeric': 0.13582273286828067, 'fraction_numerical': 0.0030091642730132677, 'mean_word_length': 1.4981209429449949, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4325270', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1840, 'n_words': 375}