diff --git a/laws/management/commands/scrape_votes.py b/laws/management/commands/scrape_votes.py index 0e181a67..c92fcd88 100644 --- a/laws/management/commands/scrape_votes.py +++ b/laws/management/commands/scrape_votes.py @@ -50,3 +50,11 @@ def _create_new_object(self, dataservice_vote): # if v.full_text_url != None: # l = Link(title=u'מסמך הצעת החוק באתר הכנסת', url=v.full_text_url, content_type=ContentType.objects.get_for_model(v), object_pk=str(v.id)) # l.save() + + def _recreate_object(self, vote_id): + vote = Vote.objects.get(id=int(vote_id)) + vote_src_id = vote.src_id + dataservice_vote = self.DATASERVICE_CLASS.get(vote_src_id) + VoteAction.objects.filter(vote=vote).delete() + vote.delete() + return self._create_new_object(dataservice_vote) diff --git a/simple/scrapers/__init__.py b/simple/scrapers/__init__.py index 1b40ad8f..c46b1ca4 100644 --- a/simple/scrapers/__init__.py +++ b/simple/scrapers/__init__.py @@ -1,6 +1,6 @@ import locale -def hebrew_strftime(dt, fmt=u'%A %m %B %Y %H:%M'): +def hebrew_strftime(dt, fmt=u'%A %d %B %Y %H:%M'): locale.setlocale(locale.LC_ALL, 'he_IL.utf8') return dt.strftime(fmt).decode('utf8') diff --git a/simple/scrapers/management.py b/simple/scrapers/management.py index 17a6228e..c865ec54 100644 --- a/simple/scrapers/management.py +++ b/simple/scrapers/management.py @@ -13,6 +13,8 @@ def _has_existing_object(self, dataservice_object): def _create_new_object(self, dataservice_object): raise NotImplementedError() + def _recreate_object(self, recreate_param): + raise NotImplementedError() class ReachedMaxItemsException(Exception): @@ -24,6 +26,7 @@ class BaseKnessetDataserviceCollectionCommand(BaseKnessetDataserviceCommand): option_list = BaseKnessetDataserviceCommand.option_list + ( make_option('--page-range', dest='pagerange', default='1-10', help="range of page number to scrape (e.g. --page-range=5-12), default is 1-10"), make_option('--max-items', dest='maxitems', default='0', help='maximum number of items to process'), + make_option('--re-create', dest='recreate', default='', help='item id to delete and then re-create'), ) def _handle_page(self, page_num): @@ -37,13 +40,18 @@ def _handle_page(self, page_num): raise ReachedMaxItemsException('reached maxitems') def _handle_noargs(self, **options): - page_range = options['pagerange'] - first, last = map(int, page_range.split('-')) - self._max_items = int(options['maxitems']) - self._num_items = 0 - for page_num in range(first, last+1): - self._log_debug('page %s'%page_num) - try: - self._handle_page(page_num) - except ReachedMaxItemsException: - break + if (options['recreate'] != ''): + self._log_info('recreating object %s'%options['recreate']) + vote = self._recreate_object(options['recreate']) + self._log_info('created as object %s'%vote.pk) + else: + page_range = options['pagerange'] + first, last = map(int, page_range.split('-')) + self._max_items = int(options['maxitems']) + self._num_items = 0 + for page_num in range(first, last+1): + self._log_debug('page %s'%page_num) + try: + self._handle_page(page_num) + except ReachedMaxItemsException: + break