def handle(self, *args, **options): self.options = options self.verbosity = int(options['verbosity']) # 1 = normal, 0 = minimal, 2 = all self.v_normal = 1 year = date.today().year quarter = year_quarter(date.today().month) #get the quarter 1, 2, 3, 4 # counters self.counts = defaultdict(int) # set the name of the report of duplications self.reportsdirectory = settings.REPORTS_DIR self.reportname = "merge-report-%s.txt" % strftime("%Y-%m-%dT%H-%M-%S") # connection to repository self.repo = ManagementRepository() try: #if pids specified, use that list if len(args) == 2: pids = list(args) else: raise Exception("specify two pid") except Exception as e: raise Exception("Error getting pids: %s" % e.message) self.counts['total'] = len(pids) for idx,pid in enumerate(pids): try: if idx == 0: self.output(1, "\nProcessing Elements PID %s" % pid) # Load first as Article becauce that is the most likely type element_obj = self.repo.get_object(pid=pid, type=Publication) element_stats = ArticleStatistics.objects.filter(pid=pid) if element_stats: element_stats.delete() if not element_obj.exists: self.output(1, "Skipping because %s does not exist" % pid) continue elif idx == 1: self.output(1, "\nProcessing Old PID %s" % pid) original_obj = self.repo.get_object(pid=pid, type=Publication) if not original_obj.exists: self.output(1, "Skipping because %s does not exist" % pid) continue original_stats = ArticleStatistics.objects.filter(pid=pid) if not original_stats: original_stats = ArticleStatistics.objects.create(pid=pid, year=year, quarter=quarter) except (KeyboardInterrupt, SystemExit): if self.counts['saved'] > 0: self.write_report(self.duplicates, error="interrupt") raise except Exception as e: self.output(1, "Error processing %s: %s" % (pid, e.message)) self.output(1, element_obj.rels_ext.content.serialize(pretty=True)) self.counts['errors']+=1 element_obj.descMetadata.content = original_obj.descMetadata.content element_obj.provenance.content = original_obj.provenance.content element_obj.dc.content = original_obj.dc.content if original_obj.pdf.content: element_obj.pdf.content = original_obj.pdf.content original_obj.state = 'I' element_obj.provenance.content.init_object(element_obj.pid, 'pid') element_obj.provenance.content.merged(original_obj.pid, element_obj.pid) ArticleStatistics.objects.filter(pid=element_obj.pid).delete() for stat in original_stats: ArticleStatistics.objects.create(pid=element_obj.pid, year=stat.year, quarter=stat.quarter, num_downloads=stat.num_downloads, num_views=stat.num_views) coll = self.repo.get_object(pid=settings.PID_ALIASES['oe-collection']) element_obj.collection = coll element_obj.rels_ext.content.add((element_obj.uriref, relsextns.hasModel, URIRef(Publication.ARTICLE_CONTENT_MODEL))) element_obj.rels_ext.content.add((element_obj.uriref, relsextns.hasModel, URIRef(Publication.PUBLICATION_CONTENT_MODEL))) # SAVE OBJECTS UNLESS NOACT OPTION if not options['noact']: element_obj.save() original_obj.save() self.counts['saved']+=1 # summarize what was done self.stdout.write("\n\n") self.stdout.write("Total number selected: %s\n" % self.counts['total']) self.stdout.write("Skipped: %s\n" % self.counts['skipped']) self.stdout.write("Errors: %s\n" % self.counts['errors']) self.stdout.write("Converted: %s\n" % self.counts['saved'])
def handle(self, *args, **options): self.verbosity = int( options['verbosity']) # 1 = normal, 0 = minimal, 2 = all self.v_normal = 1 #counters for script reporting counts = defaultdict(int) #Connection to solr solr = solr_interface() #info today's date will be used to caculate previous quarter today = datetime.datetime.today() current_year = today.year current_month = today.month current_quarter = year_quarter(current_month) self.output( 1, "Current month/year: %s/%s quarter: %s" % (current_month, current_year, current_quarter)) if current_quarter == 1: self.year = current_year - 1 self.quarter = 4 else: self.year = current_year self.quarter = current_quarter - 1 self.output( 1, "Report will run for year: %s quarter: %s" % (self.year, self.quarter)) #start and end dates start_end = { 1: ('January 1, %s' % self.year, 'March 31, %s' % self.year), 2: ('April 1, %s' % self.year, 'June 30, %s' % self.year), 3: ('July 1, %s' % self.year, 'September 30, %s' % self.year), 4: ('October 1, %s' % self.year, 'December 31, %s' % self.year), } #if netids specified, use that list if len(args) != 0: netid_set = list(args) else: #search for authors with published articles. try: netid_set = solr.query().filter(content_model=Article.ARTICLE_CONTENT_MODEL,state='A').\ facet_by('owner').paginate(rows=0).execute() except Exception as e: if 'is not a valid field name' in e.message: raise CommandError( 'Solr unknown field error ' + '(check that local schema matches running instance)') raise CommandError('Error (%s)' % e.message) #get just the netid and thow away the count netid_set = netid_set.facet_counts.facet_fields['owner'] netid_set = [n[0] for n in netid_set] #query solr for all articles for each user for n in netid_set: self.output(1, "Processing user %s" % n) try: article_query = solr.query().filter( content_model=Article.ARTICLE_CONTENT_MODEL, state='A', owner=n).field_limit(['pid', 'title']) articles = Paginator(article_query, 5) #change later articles = articles.object_list except Exception as e: self.output.error(0, e.message) continue article_data = self.get_article_data(articles, self.year, self.quarter) #add name and email to article data user = User.objects.filter(username=n) if user: user = user[0] # again should only be 1 record article_data['first_name'] = user.first_name article_data['last_name'] = user.last_name article_data['email'] = user.email article_data['start'] = start_end[self.quarter][0] article_data['end'] = start_end[self.quarter][1] #send the email! self.send_mail(article_data, options)
def handle(self, *args, **options): self.verbosity = int(options['verbosity']) # 1 = normal, 0 = minimal, 2 = all self.v_normal = 1 #counters for script reporting counts = defaultdict(int) #Connection to solr solr = solr_interface() #info today's date will be used to caculate previous quarter today = datetime.datetime.today() current_year = today.year current_month = today.month current_quarter = year_quarter(current_month) self.output(1, "Current month/year: %s/%s quarter: %s" % (current_month, current_year, current_quarter)) if current_quarter == 1: self.year = current_year -1 self.quarter = 4 else: self.year = current_year self.quarter = current_quarter - 1 self.output(1, "Report will run for year: %s quarter: %s" % (self.year, self.quarter)) #start and end dates start_end = { 1 : ('January 1, %s' % self.year, 'March 31, %s' % self.year), 2 : ('April 1, %s' % self.year, 'June 30, %s' % self.year), 3 : ('July 1, %s' % self.year, 'September 30, %s' % self.year), 4 : ('October 1, %s' % self.year, 'December 31, %s' % self.year), } #if netids specified, use that list if len(args) != 0: netid_set = list(args) else: #search for authors with published articles. try: netid_set = solr.query().filter(content_model=Publication.ARTICLE_CONTENT_MODEL,state='A').\ facet_by('owner').paginate(rows=0).execute() except Exception as e: if 'is not a valid field name' in e.message: raise CommandError('Solr unknown field error ' + '(check that local schema matches running instance)') raise CommandError('Error (%s)' % e.message) #get just the netid and thow away the count netid_set = netid_set.facet_counts.facet_fields['owner'] netid_set = [n[0] for n in netid_set] #query solr for all articles for each user for n in netid_set: self.output(1, "Processing user %s" % n) try: article_query = solr.query().filter(content_model=Publication.ARTICLE_CONTENT_MODEL,state='A' , owner=n).field_limit(['pid', 'title']) articles = Paginator(article_query, 5) #change later articles = articles.object_list except Exception as e: self.output.error(0, e.message) continue article_data = self.get_article_data(articles, self.year, self.quarter) #add name and email to article data user = User.objects.filter(username=n) if user: user = user[0] # again should only be 1 record article_data['first_name'] = user.first_name article_data['last_name'] = user.last_name article_data['email'] = user.email article_data['start'] = start_end[self.quarter][0] article_data['end'] = start_end[self.quarter][1] #send the email! self.send_mail(article_data, options)