Beispiel #1
0
 def post(self, request, *args, **kwargs):
     """
     This will create a Repo object and and will redirect to choose_template
     """
     form_field_dict = FormHandler(request,
                                   self.form_class).handle_post_fields(
                                       ('repo', ))
     user = request.user
     form_field_dict['user'] = user
     repo = RepoDbIO().create_return(form_field_dict)
     RepoDbIO().update_obj(repo, {'main': True})
     RepoDbIO().change_main(user, repo)
     return HttpResponseRedirect(reverse('choose-template'))
 def perform_initial_tasks(self):
     """
     Perform initial tasks. Tasks include:
         * get user token
         * create repo
         * clone repo to the particular location
         * change config of the git repo
         * commit all changes
         * push code to remote
     """
     user_token = self.gh_handler.get_user_token()
     self.gh_handler.create_remote_repo(user_token)
     repo = self.gh_handler.clone_repo(JekyllNow.FORKED_URL, self.path)
     self.gh_handler.change_config(repo)
     self.gh_handler.gh_pages_branch(repo)
     repo_obj = RepoDbIO().get_obj({
         'user': self.user,
         'main': True,
         'repo': self.repo
     })
     self.update_baseurl(repo_obj)
     self.gh_handler.commit_all_changes(repo, 'Intial commit')
     self.gh_handler.push_code(repo)
     self.fill_page_database(repo_obj)
     self.update_template_name(repo_obj)
Beispiel #3
0
    def post_social_profile_data(self, user, form_field_dict):
        """
        handle the post social profile View method
        :param user: the logged in user
        :param form_field_dict: form field cleaned data
        :return:
        """
        repo = RepoDbIO().get_repo(user)

        # repo is the foriegn key so it needs to be in the dict.
        form_field_dict['repo'] = repo
        social_data = SocialProfileDbIO().get_obj({'repo': repo})

        if social_data:
            SocialProfileDbIO().update_obj(social_data, form_field_dict)
        else:
            SocialProfileDbIO().create_obj(**form_field_dict)

        config_path = os.path.join(self.path, '_config.yml')

        self.del_repo(form_field_dict)
        # Complete all the yaml operations
        yaml_dict = YAMLHandler().read_yaml_file(config_path, True)
        new_yaml = YAMLHandler().change_yaml(yaml_dict, form_field_dict)
        YAMLHandler().write_dict_yaml(config_path, new_yaml)

        # Complete all the git operations
        repo = Repo(self.path)
        GithubHandler.commit_all_changes(repo, 'Change site data')
        GithubHandler.push_code(repo, 'gh-pages')
Beispiel #4
0
 def load_social_profile_initials(self, request, form_class):
     """
     Load the site profile initials from the database
     """
     social_data = SocialProfileDbIO().get_obj(
         {'repo': RepoDbIO().get_repo(request.user)})
     return FormHandler(request, form_class).load_initials(social_data)
Beispiel #5
0
 def load_site_initials(self, request, form_class):
     """
     Load the site data initials from the database
     """
     site_data = SiteDataDbIO().get_obj(
         {'repo': RepoDbIO().get_repo(request.user)})
     return FormHandler(request, form_class).load_initials(site_data)
Beispiel #6
0
    def get(self, request, pk='', *args, **kwargs):
        repo_name = RepoDbIO().get_repo(request.user).repo
        form_response = SBSFormHandler(
            request.user, repo_name).load_page_initials(
            request, self.form_class, pk)

        return render(request, TemplateName.SBS_PAGE_DATA,
                      {'form': form_response, 'media': settings.MEDIA_URL})
Beispiel #7
0
    def get(self, request, *args, **kwargs):
        repo_name = RepoDbIO().get_repo(request.user).repo
        form_response = SBSFormHandler(
            request.user, repo_name).load_site_initials(
            request, self.form_class)

        return render(request,
                      TemplateName.SBS_POST_DATA, {'form': form_response})
Beispiel #8
0
 def load_initials(self, user, form_class):
     """
     Load the initials from the database
     """
     cname = CNameDbIO().get_obj({'repo': RepoDbIO().get_repo(user)})
     if cname is None:
         return form_class
     return form_class(initial=cname.__dict__)
 def main_repo_with_no_template(self, user):
     """
     Method to get the repo with main True and template not set
     """
     repo = RepoDbIO().get_obj({
         'user': user,
         'main': True,
         'template': BlogTemplates.TEMPLATE_NOT_SET
     })
     if repo:
         return repo.repo
     raise PermissionDenied
Beispiel #10
0
 def assign_cname(self, user, cname):
     """
     Assign the Cname to the repo
     """
     repo = RepoDbIO().get_repo(user)
     cname_obj = CNameDbIO().get_obj({'repo': repo})
     if cname_obj is None:
         CNameDbIO().create_obj({'repo': repo, 'c_name': cname})
     else:
         CNameDbIO().update_obj(cname_obj, {'c_name': cname})
     self.write_to_file(user, repo.repo, cname)
     BashScript().push_online(user, repo)
Beispiel #11
0
 def read_pages(self):
     """
     Read pages and save the instance into database
     """
     for file in os.listdir(self.repo_path):
         if file.endswith('.md'):
             if str(file) is not ('README.md' or '404.md'):
                 with open(self.repo_path + file, 'r') as page_file:
                     file_data = page_file.read()
                     content_dict = self.page_call_scrapers(file_data)
                     content_dict['repo'] = RepoDbIO().get_repo(
                         self.user, self.repo_name)
                     PageDbIO().save_db_instance(content_dict)
Beispiel #12
0
 def read_posts(self):
     """
     Read the _posts directory and iterate over all the files in the
     directory. In the end save the instance in the database.
     """
     for file in os.listdir(self.posts_path):
         file_data = self.extract_post(self.posts_path + file)
         content_dict = self.call_scrapers(file_data)
         if content_dict['comments'] == 'true':
             content_dict['comments'] = True
         else:
             content_dict['comments'] = False
         content_dict['repo'] = RepoDbIO().get_repo(self.user,
                                                    self.repo_name)
         PostDbIO().save_db_instance(content_dict)
Beispiel #13
0
    def post(self, request, pk='', *args, **kwargs):
        form_field_dict = FormHandler(
            request, self.form_class).handle_post_fields((
                'title',
                'description',
                'background',
                'content')
        )
        user = request.user
        repo_name = RepoDbIO().get_repo(request.user).repo
        SBSFormHandler(
            user, repo_name).post_page_data(user, form_field_dict)

        return render(request, TemplateName.SBS_PAGE_DATA,
                      {'msg': 'Post updated successfully.'})
Beispiel #14
0
    def post(self, request, *args, **kwargs):
        form_field_dict = FormHandler(
            request, self.form_class).handle_post_fields((
                'email',
                'facebook',
                'github',
                'twitter')
        )
        user = request.user
        repo_name = RepoDbIO().get_repo(request.user).repo
        SBSFormHandler(
            user, repo_name).post_social_profile_data(user, form_field_dict)

        return render(request, TemplateName.SBS_SOCIAL_DATA,
                      {'msg': 'Social data updated successfully.'})
Beispiel #15
0
    def post_page_data(self, user, form_field_dict, pk=None):
        """
        handle the post page View method
        :param user: the logged in user
        :param form_field_dict: form field cleaned data
        We have to delete the file if the title is changed otherwise two
        different files will be created.
        :return:
        """
        # TODO image copying is not done.
        # TODO take care of the layout
        repo = RepoDbIO().get_repo(user)
        if pk:
            post = PostDbIO().get_obj({
                'pk': pk,
                'repo__user': user,
                'repo': repo
            })
            if pk is None:
                raise PermissionDenied

            if post.title is not form_field_dict['title']:
                file_name = ExtraHandler().file_name_f_title(
                    post.title, 'html')
                FileHandler('/'.join([self.path, '_posts']),
                            file_name).delete_file()

            post = PostDbIO().update_obj(post, **form_field_dict)

        else:
            raise PermissionDenied

        ExtraHandler().del_keys(form_field_dict, (
            'repo',
            'content',
        ))
        yaml_content = YAMLHandler().create_yaml(form_field_dict)
        w_yaml_content = ExtraHandler().wrap_content('---', yaml_content)
        full_content = ExtraHandler().join_content(w_yaml_content,
                                                   post.content)
        file_name = ExtraHandler().file_name_f_title(post.title, 'html')
        FileHandler('/'.join([self.path, '_posts']),
                    file_name).rewrite_file(full_content)

        # Complete all the git operations
        repo = Repo(self.path)
        GithubHandler.commit_all_changes(repo, 'Change site data')
        GithubHandler.push_code(repo, 'gh-pages')
Beispiel #16
0
    def load_page_initials(self, request, form_class, pk=None):
        """
        Load the page initials from the database
        """
        repo = RepoDbIO().get_repo(request.user)
        if pk:
            post = PostDbIO().get_obj({
                'pk': pk,
                'repo__user': request.user,
                'repo': repo
            })

        else:
            raise PermissionDenied

        return FormHandler(request, form_class).load_initials(post)
Beispiel #17
0
    def post(self, request, *args, **kwargs):
        form_field_dict = FormHandler(
            request, self.form_class).handle_post_fields((
                'title',
                'description',
                'author',
                'baseurl',
                'url')
        )
        user = request.user
        repo_name = RepoDbIO().get_repo(request.user).repo
        SBSFormHandler(user,
                       repo_name).post_site_data(user, form_field_dict)

        return render(request, TemplateName.SBS_SITE_DATA,
                      {'msg': 'Site data updated successfully.'})
Beispiel #18
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            user = request.user
            name = request.POST['name']
            description = request.POST['description']
            avatar = request.POST['avatar']

            repo = RepoDbIO().get_obj({
                'user': self.request.user,
                'main': True,
            })
            data_dict = {
                'repo': repo,
                'name': name,
                'description': description,
                'avatar': avatar
            }
            JekyllNowHandler(request.user,
                             repo.repo).perform_site_data(data_dict)
            return HttpResponseRedirect(reverse('home'))
 def perform_site_data(self, data_dict):
     """
     Perform all the site data operations here.
     """
     repo = RepoDbIO().get_obj({
         'user': self.user,
         'main': True,
     })
     config_path = os.path.join(self.path, '_config.yml')
     site_data = SiteDataDbIO().get_obj({'repo': repo})
     if site_data:
         SiteDataDbIO().update_obj(site_data, data_dict)
     else:
         SiteDataDbIO().save_db_instance(data_dict)
     self.del_key(data_dict, 'repo')
     yaml_dict = self.yaml_handler.read_yaml_file(config_path, True)
     new_yaml = self.yaml_handler.change_yaml(yaml_dict, data_dict)
     self.yaml_handler.write_dict_yaml(config_path, new_yaml)
     repo = self.gh_handler.get_repo_from_path(self.path)
     self.gh_handler.commit_all_changes(repo, 'Intial commit')
     self.gh_handler.push_code(repo)
Beispiel #20
0
 def load_jn_site_initials(self):
     repo = RepoDbIO().get_repo(self.user)
     site_data = SiteDataDbIO().get_obj({'repo': repo})
     if site_data:
         return self.form_class(initial=site_data.__dict__)
     return self.form_class
 def update_template_name(self, repo):
     """
     update the template name to jekyllnow
     """
     RepoDbIO().update_obj(repo, {'template': BlogTemplates.JEKYLL_NOW})