Beispiel #1
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')
class SBSHandler:
    def __init__(self, user, repo):
        self.repo = repo
        self.user = user
        self.gh_handler = GithubHandler(user, repo)
        self.path = PathHandler(user, repo).create_repo_path()

    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_path = PathHandler(self.user, self.repo).create_repo_path
        repo = self.gh_handler.clone_repo(StartBootstrap.FORKED_URL, repo_path)
        self.gh_handler.change_config(repo)
        self.gh_handler.commit_all_changes(repo, 'Intial commit')
        self.gh_handler.push_code(repo)
        self.fill_page_database()

    def fill_page_database(self):
        """
        This method will be used to fill the page database by iterating
        over the pages in the database.
        """
        AbstractPageHandler(self.path).read_pages(self.repo, 'html', [])
Beispiel #3
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')
 def __init__(self, user, repo):
     self.user = user
     self.repo = repo
     self.gh_handler = GithubHandler(user, repo)
     self.path = PathHandler(user, repo).create_repo_path()
     self.yaml_handler = YAMLHandler()
class JekyllNowHandler:
    def __init__(self, user, repo):
        self.user = user
        self.repo = repo
        self.gh_handler = GithubHandler(user, repo)
        self.path = PathHandler(user, repo).create_repo_path()
        self.yaml_handler = YAMLHandler()

    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)

    def update_baseurl(self, repo_obj):
        """
        update baseurl in config file.
        """
        config_path = os.path.join(self.path, '_config.yml')
        yaml_dict = self.yaml_handler.read_yaml_file(config_path, True)
        if repo_obj.repo != (str(self.user.username) + '.github.io'):
            new_yaml = self.yaml_handler.change_yaml(
                yaml_dict, {'baseurl': "/{}".format(str(repo_obj.repo))})
            self.yaml_handler.write_dict_yaml(config_path, new_yaml)

    def update_template_name(self, repo):
        """
        update the template name to jekyllnow
        """
        RepoDbIO().update_obj(repo, {'template': BlogTemplates.JEKYLL_NOW})

    def fill_page_database(self, repo):
        """
        This method will be used to fill the page database by iterating
        over the pages in the database.
        """
        AbstractPageHandler(self.path).read_pages(repo, 'md',
                                                  ('README.md', '404.md'))

    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)

    def del_key(self, dict, key):
        del dict[key]