Exemple #1
0
    def do_save(self):
        for hexsha in self.cached_data:

            val = self.cached_data[hexsha]
            try:
                actor = Actor.objects.get(full_name = val['name'])
            except Actor.DoesNotExist:
                actor = Actor(full_name = val['name'])
                actor.save()
                #Create the actor

            try:
                commit = Commit.objects.get(hexsha = hexsha)
            except Commit.DoesNotExist:
                commit = Commit(hexsha = hexsha, repo = self.repo_model, actor = actor)
                commit.save()

            for path, fun in val['funcs']:
                if not Function.objects.filter(name = fun, path = path).exists():
                    fmodel = Function(name = fun, commit = commit, path = path)
                    fmodel.save()
                    print "Saved  `%s` : `%s`" % (path[-16:], fun)

            for file_name in val['files_changed']:
                FileChange(path = file_name, actor = actor, commit = commit).save()


        self.cached_data.clear()
Exemple #2
0
    def test_repo_commits(self):
        repo = Repo.create(**self.TEST_REPO)
        repo.save()

        response = self.fetch(self.get_app().reverse_url('view', repo.id))

        self.assertIn(self.MESSAGES['no_records'], response.body.decode())

        self.assertIn(self.MESSAGES['get_more'], response.body.decode())

        for commit in range(self.TEST_COUNT):
            commit_data = self.TEST_COMMIT
            commit_data.update({'repo': repo})
            c = Commit(**commit_data)
            c.save()

        response = self.fetch(self.get_app().reverse_url('view', repo.id))

        self.assertEqual(response.body.decode().count(self.TEST_COMMIT['message']),
                         self.TEST_COUNT)

        self.assertIn(self.MESSAGES['get_more'], response.body.decode())

        repo.next_page = None
        repo.save()

        response = self.fetch(self.get_app().reverse_url('view', repo.id))

        self.assertNotIn(self.MESSAGES['get_more'], response.body.decode())
Exemple #3
0
    def handle(self):
        commit = Commit()

        commit.opcode = "created_debt_engine"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        debt = debt_engine_interface.get_debt_by_id(self._id)

        error = False
        balance = 0

        model = debt.get("model")
        creator = debt.get("creator")
        oracle = debt.get("oracle")

        created = str(self._block_timestamp())

        data = {
            "error": error,
            "balance": str(balance),
            "model": model,
            "creator": creator,
            "oracle": oracle,
            "created": created,
            "id": self._args.get("_id")
        }

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #4
0
def group_commits(tags, commits):
    tags = sorted(tags, key=lambda t: t.date)

    # Adding the tag's commit manually because those seem to be skipped
    commits.extend([Commit(t._commit) for t in tags])

    # Sort the commits and filter out those not formatted correctly
    commits = sorted(commits, key=lambda c: c.date)
    commits = list(filter(lambda c: c.category, commits))

    for index, tag in enumerate(tags):
        # Everything is sorted in ascending order (earliest to most recent),
        # So everything before the first tag belongs to that one
        if index == 0:
            children = filter(lambda c: c.date <= tag.date, commits)
        else:
            prev_tag = tags[index - 1]
            children = filter(lambda c: prev_tag.date < c.date <= tag.date,
                              commits)

        for child in children:
            commits.remove(child)
            tag.add_commit(child)

    left_overs = list(filter(lambda c: c.date > tags[-1].date, commits))
    return left_overs
Exemple #5
0
    def handle(self):
        commit = Commit()

        commit.opcode = "closed_auction"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        collateral_id = str(self._args.get("_entryId"))
        collateral = Collateral.objects.get(id=collateral_id)
        print(collateral.debt_id)
        loan = Loan.objects.get(id=collateral.debt_id)
        if loan.status == "2":
            status = str(CollateralState.TO_WITHDRAW.value)
        else:
            status = str(CollateralState.STARTED.value)

        data = {
            "id": collateral_id,
            "received": str(self._args.get("_received")),
            "leftover": str(self._args.get("_leftover")),
            "status": status
        }

        commit.data = data

        return [commit]
Exemple #6
0
def add_log():
    # Bork if something is terribly wrong
    if not request.json or not 'log' in request.json:
        abort(400)

    # Iterate over the log
    cip = CommitInsertionPipeline()
    for logitem in request.json['log']:
        try:
            commit = Commit()
            commit.author = "%s <%s>" % (logitem['author_name'], 
                                         logitem['author_email'])
            date_time = parse(logitem['date'])
            commit.c_date = datetime(
                date_time.year,
                date_time.month,
                date_time.day,
                date_time.hour,
                date_time.minute,
                date_time.second
                )
            commit.cuid = logitem['id']
            commit.files = "foo.py"
            commit.message = logitem['message']
            commit.project = "Project"
            cip.add_commit(commit, True)
        except KeyError:
            print("Something is terribly wrong")
            continue
    cip.stash_commits()

    # Return something nice and positive
    return jsonify({'result': 'success'}), 201
 def test_commit_token_unique(self):
     """
     Test that commit.create() method generates unique tokens.
     """
     airport = Airport.objects.get(icao="XYZZ")
     # this is funny test
     for i in range(100):
         commit = Commit.create(airport)
         commit.save()
Exemple #8
0
    def handle(self):
        commit = Commit()

        commit.opcode = "requested_loan_manager"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        oracle = self._args.get("_oracle")
        try:
            if oracle != "0x0000000000000000000000000000000000000000":
                oracle_contract = OracleInterface(oracle)
                currency = oracle_contract.currency()
            else:
                currency = loan_manager_interface.get_currency(
                    self._args.get('_id'))
                if currency:
                    currency = utils.add_0x_prefix(currency)
        except BadFunctionCallOutput:
            currency = "0x"

        print("currency: {}".format(currency))

        data = {
            "id": self._args.get("_id"),
            "open": True,
            "approved":
            self._args.get("_creator") == self._args.get("_borrower"),
            "position": "0",
            "expiration": str(self._args.get("_expiration")),
            "amount": str(self._args.get("_amount")),
            "cosigner": "0x0000000000000000000000000000000000000000",
            "model": self._args.get("_model"),
            "creator": self._args.get("_creator"),
            "oracle": self._args.get("_oracle"),
            "borrower": self._args.get("_borrower"),
            "callback": self._args.get("_callback"),
            "salt": str(self._args.get("_salt")),
            "loanData": self._args.get("_loanData"),
            "created": str(self._block_timestamp()),
            "currency": currency,
            "status": "0"
        }

        descriptor = loan_manager_interface.get_descriptor(data)

        data["descriptor"] = descriptor

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #9
0
    def handle(self):
        commit = Commit()

        config_data = installments_model_interface.get_config_by_id(self._args.get("_id"))

        commit.opcode = "created_installments"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")
        commit.data = config_data
        commit.id_loan = self._args.get("_id")

        return [commit]
Exemple #10
0
    async def commit_scan_loop(self):
        leaderboard_cog = self.bot.get_cog("Leaderboard")
        await leaderboard_cog.create_leaderboard()
        repositories = self.bot.db_session.query(Repository).all()
        user_cache = {}
        summaries = {}
        repo_cache = {}

        for repository in repositories:
            repository_user, repository_repo = repository.repository.split("/")
            raw_commits = await self.utils.get_commits(repository_user, repository_repo)
            commits = self.utils.parse_commits(raw_commits)

            for commit in commits:
                commit["author"] = commit["author"].lower()
                if commit["author"] not in user_cache.keys():
                    user = self.bot.db_session.query(User).filter(
                        User.github_username == commit["author"]).first()
                    if user is None:
                        continue
                    user_cache[commit["author"]] = user
                user = user_cache[commit["author"]]
                old_commit = self.bot.db_session.query(Commit).filter(Commit.commit_hash == commit["hash"]).first()
                if old_commit is not None:
                    continue
                database_commit = Commit(commit_hash=commit["hash"], user_id=user.id)
                self.bot.db_session.add(database_commit)
                old_summary = summaries.get(user.id, [])
                old_summary.append(commit)
                summaries[user.id] = old_summary
                repo_cache[commit["hash"]] = repository.repository
        self.bot.db_session.commit()

        for user_id, commits in summaries.items():
            user = await self.bot.fetch_user(user_id)
            if user is None:
                continue
            discord_user = self.bot.get_user(user)
            for commit in commits:
                if user is not None:
                    embed = discord.Embed(
                        description=f"https://github.com/{repo_cache[commit['hash']]}/commit/{commit['hash']}",
                        color=0x00FFFF)
                    embed.set_author(name=str(user), icon_url=user.avatar_url)
                    await self.commit_webhook.send(embed=embed)
            summary_pages = self.create_summary(user, commits)
            self.logger.debug(f"Sending a dm to {user}!")
            for page in summary_pages:
                try:
                    await user.send(page)
                except:
                    pass
        await leaderboard_cog.post_leaderboard()
Exemple #11
0
    def init_commit_from_summary(self, line):
        commit_parameters, author_parameters = self.map_parameters(line)

        commit = Commit(**commit_parameters)
        self.commit_list.append(commit)

        if not author_parameters['author_email'] in self.author_dict:
            author = Author(**author_parameters)
            self.author_list.append(author)
            self.author_dict[author.author_email] = author
        self.current_author = self.author_dict[author_parameters['author_email']]
        self.current_author.commits.append(commit)
Exemple #12
0
    def handle(self):
        commit = Commit()

        commit.opcode = "approved_loan_manager"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {"id": self._args.get("_id"), "approved": True}

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #13
0
    def handle(self):
        commit = Commit()

        commit.opcode = "error_recover_debt_engine"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {"id": self._args.get("_id"), "error": False}

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #14
0
def load_commits(user, author, start_date, end_date):
    print('loading commits for ' + author)
    result = github_api.commits(user, author, start_date, end_date)
    for r in result:
        print('putting ' + r['sha'])
        commit = Commit(id=r['sha'])
        commit.author = author
        ts = time.mktime(parse(r['commit']['author']['date']).timetuple())
        commit.date = datetime.datetime.utcfromtimestamp(ts)
        commit.html_url = r['html_url']
        commit.put()
Exemple #15
0
def parse_push(ctxt, hook):
    commits = [
        Commit(id=commit["id"],
               message=commit["message"],
               url=commit["url"],
               author=ctxt.get_or_create_user(commit["committer"]["name"],
                                              commit["committer"]["email"]))
        for commit in hook["commits"]
    ]
    return Push(branch=hook["ref"].split('/')[-1],
                commits=commits,
                url=hook["compare"],
                user=ctxt.user,
                project=ctxt.project)
Exemple #16
0
    def handle(self):
        commit = Commit()

        commit.opcode = "pay_batch_error_debt_engine"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "id": self._args.get("_id"),
            "oracle": self._args.get("_oracle")
        }

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #17
0
    def handle(self):
        commit = Commit()

        commit.opcode = "set_paid_base_installments"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "id": self._args.get("_id"),
            "paid_base": str(self._args.get("_paidBase"))
        }

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #18
0
    def handle(self):
        commit = Commit()

        commit.opcode = "settled_lend_loan_manager"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "id": self._args.get("_id"),
            "lender": self._args.get("_lender"),
            "tokens": self._args.get("_tokens")
        }

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #19
0
 def test_push(self):
     commits = [
         Commit(id="1d8d58ec4c8865394cd5ff91ce8e54b1d598346f",
                message="Fix py2 compat",
                url="http://example.com/project/commit/...",
                author=self.user)
     ]
     push = Push(user=self.user,
                 project=self.project,
                 branch="hotfix/Issue214",
                 commits=commits,
                 url="too long don't write")
     self.assertEqual(
         push.render_simple(),
         "[My Project] Mrs Foobar pushed 1 commits to hotfix/Issue214. "
         "(too long don't write)\n"
         "1d8d58e Mrs Foobar: Fix py2 compat")
Exemple #20
0
    def handle(self):
        commit = Commit()

        commit.opcode = "pool_started_loanDAO"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction

        data = {"poolId": self._args.get("_poolId")}

        commit.data = data

        return [commit]
Exemple #21
0
    def handle(self):
        commit = Commit()

        commit.opcode = "withdrawn_debt_engine"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "id": self._args.get("_id"),
            "sender": self._args.get("_sender"),
            "to": self._args.get("_to"),
            "amount": str(self._args.get("_amount"))
        }

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #22
0
    def handle(self):
        commit = Commit()

        commit.opcode = "redeemed_collateral"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "id": str(self._args.get("_entryId")),
            "to": str(self._args.get("_to")),
        }

        commit.data = data

        return [commit]
Exemple #23
0
    def handle(self):
        commit = Commit()

        commit.opcode = "started_collateral"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "id": str(self._args.get("_entryId")),
            "status": str(CollateralState.STARTED.value)
        }

        commit.data = data

        return [commit]
Exemple #24
0
    def test_commits_create(self, get_commits):
        fixtures = self.data.buffer.read()
        jsn = json.loads(fixtures.decode())

        request = HTTPRequest(self.TEST_REPO['href'])
        response = HTTPResponse(request, client.OK, buffer=io.BytesIO(fixtures))

        future = Future()
        future.set_result(response)
        get_commits.return_value = future

        body = {'href': self.TEST_REPO['href']}
        response = self.fetch(self.get_app().reverse_url('create'), method='POST',
                              body=urlencode(body).encode())

        self.assertIn('всего {}'.format(len(jsn)), response.body.decode())
        self.assertEqual(len(jsn), Commit.select().count())
        self.assertEqual(1, Repo.select().count())
Exemple #25
0
def parse_push(ctxt, hook):
    # First generate the list of all commits
    commits = [
        Commit(
            id=commit["id"],
            message=commit["message"],
            url=commit["url"],
            author=ctxt.get_or_create_user(**commit["author"]),
        ) for commit in hook["commits"]
    ]
    return Push(
        branch=hook["ref"].split('/')[-1],
        commits=commits,
        url=("{}/compare/{}...{}".format(hook["project"]["web_url"],
                                         hook["before"], hook["after"])),
        user=ctxt.user,
        project=ctxt.project,
    )
Exemple #26
0
def parse_creation(ctxt, hook):
    commits = [
        Commit(
            id=commit["id"],
            message=commit["message"],
            url=commit["url"],
            author=ctxt.get_or_create_user(**commit["author"]),
        ) for commit in hook["commits"]
    ]
    return Creation(
        branch=hook["ref"].split('/')[-1],
        commits=commits,
        url=("{}/compare/{}...{}".format(hook["project"]["web_url"],
                                         hook["project"]["default_branch"],
                                         hook["after"])),
        user=ctxt.user,
        project=ctxt.project,
    )
Exemple #27
0
    async def commit_scan_loop(self):
        leaderboard_cog = self.bot.get_cog("Leaderboard")
        await leaderboard_cog.create_leaderboard()
        repositories = self.bot.db_session.query(Repository).all()
        user_cache = {}
        summaries = {}

        for repository in repositories:
            repository_user, repository_repo = repository.repository.split("/")
            raw_commits = await self.utils.get_commits(repository_user,
                                                       repository_repo)
            commits = self.utils.parse_commits(raw_commits)

            for commit in commits:
                commit["author"] = commit["author"].lower()
                if commit["author"] not in user_cache.keys():
                    user = self.bot.db_session.query(User).filter(
                        User.github_username == commit["author"]).first()
                    if user is None:
                        continue
                    user_cache[commit["author"]] = user
                user = user_cache[commit["author"]]
                old_commit = self.bot.db_session.query(Commit).filter(
                    Commit.commit_hash == commit["hash"]).first()
                if old_commit is not None:
                    continue
                database_commit = Commit(commit_hash=commit["hash"],
                                         user_id=user.id)
                self.bot.db_session.add(database_commit)

                old_summary = summaries.get(user.id, [])
                old_summary.append(commit)
                summaries[user.id] = old_summary
        self.bot.db_session.commit()

        for user_id, commits in summaries.items():
            user = await self.bot.fetch_user(user_id)
            if user is None:
                continue
            summary_pages = self.create_summary(user, commits)
            self.logger.debug(f"Sending a dm to {user}!")
            for page in summary_pages:
                await user.send(page)
        await leaderboard_cog.post_leaderboard()
Exemple #28
0
    def handle(self):
        commit = Commit()

        commit.opcode = "transfer_collateral"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "from": self._args.get("_from"),
            "to": self._args.get("_to"),
            "tokenId": str(self._args.get("_tokenId"))
        }

        commit.data = data

        return [commit]
Exemple #29
0
    def handle(self):
        commit = Commit()

        commit.opcode = "paid_ERC20D"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._address

        data = {
            "from": self._args.get("_from"),
            "value": self._args.get("_value"),
            "contractAddress": self._address
        }

        commit.data = data

        return [commit]
Exemple #30
0
    def handle(self):
        commit = Commit()

        commit.opcode = "provide_oracleFactory"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "oracle": self._args.get("_oracle"),
            "signer": self._args.get("_signer"),
            "rate": str(self._args.get("_rate"))
        }

        commit.data = data

        return [commit]
Exemple #31
0
    def handle(self):
        commit = Commit()

        commit.opcode = "ownership_transferred_debt_engine"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        data = {
            "previous_owner": self._args.get("_previousOwner"),
            "new_owner": self._args.get("_newOwner"),
        }

        commit.data = data

        # return [commit]
        return []
Exemple #32
0
    def handle(self):
        commit = Commit()

        commit.opcode = "added_paid_installments"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction
        commit.address = self._tx.get("from")

        state = State.objects.get(id=self._args.get("_id"))

        data = {
            "id": self._args.get("_id"),
            "real": str(self._args.get("_paid")),
            "paid": str(int(state.paid) + self._args.get("_paid")),
            "state_last_payment": state.clock,
        }

        commit.id_loan = self._args.get("_id")
        commit.data = data

        return [commit]
Exemple #33
0
    def handle(self):
        commit = Commit()

        commit.opcode = "set_URIProvider_collateral"
        commit.timestamp = self._block_timestamp()
        commit.proof = self._transaction

        data = {
            "uriProvider": self._args.get("_uriProvider"),
        }

        commit.data = data

        return [commit]
    def test_commit_airport_merge(self):
        """
        Test that commits work on airports.
        """
        airport = Airport.objects.get(icao="XYZZ")
        self.assertTrue(isinstance(airport, Airport))
        commit = Commit.create(airport)
        self.assertTrue(isinstance(commit, Commit))
        commit.email = "*****@*****.**"
        commit.description = "Testing commits"
        commit.url = '/airports/details/' + airport.icao
        commit.save()

        new_data = {
            'name': u'Test Airport after test',
            'city': u'Test City after test',
            'country': u'Test Country after test',
            'latitude': 2.00,
            'longitude': 2.00,
            'altitude': 220
        }
        fields = ['name', 'city', 'country', 'latitude', 'longitude', 'altitude']
        for f in fields:
            old = unicode(getattr(airport, f))
            new = new_data[f]

            if old != new:
                data = CommitData.create(commit, f, old, new)
                data.save()

        data_set = commit.commitdata_set.all()
        self.assertEqual(len(data_set), 6)
        commit.merge()

        airport = Airport.objects.get(icao="XYZZ")
        self.assertEqual(commit.status, 'AC')
        self.assertEqual(airport.name, u"Test Airport after test")
        self.assertEqual(airport.city, u"Test City after test")
        self.assertEqual(airport.country, u"Test Country after test")
        self.assertEqual(airport.latitude, 2.00)
        self.assertEqual(airport.longitude, 2.00)
        self.assertEqual(airport.altitude, 220)
    def test_commit_airline_merge(self):
        """
        Test that commits work on airlines.
        """
        airline = Airline.objects.get(icao="XYZ")
        self.assertTrue(isinstance(airline, Airline))
        commit = Commit.create(airline)
        self.assertTrue(isinstance(commit, Commit))
        commit.email = "*****@*****.**"
        commit.description = "Testing commits"
        commit.url = '/airlines/details/' + airline.icao
        commit.save()

        new_data = {
            'name': u'Test Airline after test',
            'country': u'Test Country after test',
            'website': u'xyz.example.com',
            'logo': ''
        }
        fields = ['name', 'country', 'website', 'logo']
        for f in fields:
            old = unicode(getattr(airline, f))
            new = new_data[f]

            if old != new:
                data = CommitData.create(commit, f, old, new)
                data.save()

        data_set = commit.commitdata_set.all()
        self.assertEqual(len(data_set), 3)
        commit.merge()

        airline = Airline.objects.get(icao="XYZ")
        self.assertEqual(commit.status, 'AC')
        self.assertEqual(airline.name, u"Test Airline after test")
        self.assertEqual(airline.country, u"Test Country after test")
        self.assertEqual(airline.website, u"xyz.example.com")
Exemple #36
0
 def get_commits(self, repo):
     last_commit = repo.commits.order_by(Commit.date.desc()).first()
     commit_pages = self.gh.repos.commits.list(user=repo.organization.username,
                                         repo=repo.name,
                                         sha="master")
     for commit in commit_pages.iterator():
         if last_commit and commit.sha == last_commit.sha:
             print 'Next'
             break
         c = Commit.query.filter_by(sha=commit.sha).first()
         if c is None:
             c = Commit()
             c.sha = commit.sha
             c.message = commit.commit.message
             c.repository = repo
             c.date = commit.commit.author.date
             if commit.author:
                 c.user = self.get_or_create_user(commit.author)
             print 'New commit: ' + c.sha
             db.session.add(c)
             db.session.commit()
Exemple #37
0
def view_blog(request, blog_id):
    if request.user.is_authenticated():
        if request.is_ajax() and request.method == "GET":
            if request.GET["type"] == "1":
                current_id = request.GET["id"].strip()
                current_blog = Blog.objects.get(id=current_id)
                current_blog.rank += 1
                current_blog.save()
                return HttpResponse(current_blog.rank)
            elif request.GET["type"] == "3":
                current_id = request.GET["commit_id"].strip()
                current_commit = Commit.objects.get(id=current_id)
                current_commit.star += 1
                current_commit.save()
                return HttpResponse(current_commit.star)
            elif request.GET["type"] == "4":
                current_id = request.GET["id"].strip()
                current_blog = Blog.objects.get(id=current_id)
                current_blog.collect_blogs.remove(Hacker.objects.get(hacker=request.user))
                current_blog.collect -= 1
                current_blog.save()
                return HttpResponse(current_blog.collect)
            elif request.GET["type"] == "5":
                current_id = request.GET["id"].strip()
                current_blog = Blog.objects.get(id=current_id)
                current_blog.collect_blogs.add(Hacker.objects.get(hacker=request.user))
                current_blog.collect += 1
                current_blog.save()
                return HttpResponse(current_blog.collect)
        elif request.is_ajax() and request.method == "POST":
            if request.POST["type"] == "2":
                observer = User.objects.get(username=request.user.username)
                target = Blog.objects.get(id=blog_id)
                content = request.POST["content"].strip()
                new_commit = Commit(observer=observer, target=target, content=content)
                new_commit.save()

                timestamp = str(new_commit.create_timestamp)[:16]
                timestamp_half_1 = timestamp[:5]
                timestamp_half_2 = timestamp[5:16]
                timestamp = timestamp_half_1.replace('-', '月') + timestamp_half_2.replace('-', '日')

                dom = """
                <div class="commit-show-area">
                    <div class="container">
                        <div class="col-md-1 col-sm-1 col-xs-1" style="width: 70px;">
                            <img src="/media/cropped_avatars/%s-40.jpeg" />
                        </div>
                        <div class="col-md-9 col-sm-9 col-xs-9">
                            %s
                            <p>%s发表于%s<p>
                            <p><a href="javascript:void(0)">好评</a>&nbsp;&nbsp;0
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)">回复</a></p>
                        </div>
                    </div>
                </div>
                """ % (observer.username.encode("utf-8"), content.encode("utf-8"),
                       observer.username.encode("utf-8"), timestamp)
                return HttpResponse(dom)
        else:
            blog = Blog.objects.get(id=blog_id)
            blog.trace += 1
            blog.save()
            commits = Commit.objects.filter(target=blog).order_by("-create_timestamp")
            flag = "false"
            if blog.collect_blogs.filter(hacker=request.user).exists():
                flag = "true"
            return render(request, "blog_view.html", {"blog": blog, "commits": commits, "flag": flag})
    else:
        return redirect("/blog/list")