def create_report(self, unit=None, title=None): inv, profiles, unit_name = self.get_investigadores(unit, title) if not inv: return # We use the historical cv of the users database = (str(self.year) if self.year != datetime.date.today().year else 'default') with in_database(database): articulos = list( Articulo.objects.byUsuariosYear(profiles, self.year)) libros = list(Libro.objects.byUsuariosYear(profiles, self.year)) capitulos_libro = list( Capitulo.objects.byUsuariosYear(profiles, self.year)) congresos = list( Congreso.objects.byUsuariosYear(profiles, self.year)) proyectos = list( Proyecto.objects.byUsuariosYear(profiles, self.year)) convenios = list( Convenio.objects.byUsuariosYear(profiles, self.year)) tesis = list( TesisDoctoral.objects.byUsuariosYear(profiles, self.year)) patentes = list(Patente.objects.byUsuariosYear( profiles, self.year)) return self.generator.go(unit_name, inv, articulos, libros, capitulos_libro, congresos, proyectos, convenios, tesis, patentes)
def update_members(self, year): with in_database(str(year), write=True): try: members = ws.get(url=self.WS_URL_DETAIL % (self.code, year), use_redis=False)[0]['miembros'] except KeyError: logger.warn(self.type + " " + self.code + " does not exist in WS_URL_DETAIL for year " + year) return # This happens if the webservices are not that good. for member in members: try: up = UserProfile.objects.get( rrhh_code=member['cod_persona']) except UserProfile.DoesNotExist: response = ws.get(st.WS_DOCUMENT % member['cod_persona']) document = response['numero_documento'] + response['letra'] up = UserProfile.get_or_create_user(document, document)[0].profile up.rrhh_code = member['cod_persona'] up.save() up.user.first_name = member['cod_persona__nombre'] up.user.last_name = (member['cod_persona__apellido1'] + " " + member['cod_persona__apellido2'])[:30] up.user.save() ReportMember.objects.get_or_create(user_profile=up) setattr(up.reportmember, self.type, self) up.reportmember.cce = member['cod_cce__descripcion'] up.reportmember.save()
def get_investigadores(self, unit, title): with in_database(str(self.year)): unit_model = self.Report.objects.get(code=unit) if unit_model is None: return NotImplemented members = unit_model.reportmember_set.all().order_by( 'user_profile__user__last_name', 'user_profile__user__first_name') investigadores = [] usuarios = [] for m in members: usuarios.append(m.user_profile) investigadores.append({ 'cod_persona__nombre': m.user_profile.user.first_name, 'cod_persona__apellido1': m.user_profile.user.last_name, 'cod_persona__apellido2': '', 'cod_cce__descripcion': m.cce }) if title is None: title = unit_model.name return investigadores, usuarios, title
def load(cls, year): with in_database(str(year), write=True): units = ws.get(url=cls.WS_URL_ALL % year, use_redis=False) for unit in units: unit_code = str(unit['codigo']) unit_object = cls.objects.create(code=unit_code, name=unit['nombre']) unit_object.update_members(year)
def university_report(request, year): with in_database(year): if year is None or year not in st.HISTORICAL: raise Http404 context = {} user = User.objects.get(username='******') scientific_production_to_context(user.profile, context) try: context['report_date'] = unicode(year) except ObjectDoesNotExist: context['report_date'] = _("Not Available") return render(request, 'cvn/ull_report.html', context)
def _create_dept_and_area_fake(self, year, code, report_type): with in_database(year, write=True): ReportDept.objects.create(code=code, name='Departamento 1') ReportArea.objects.create(code=code, name='Area 1') dept_path = get_report_path(unit_type='dept', report_type=report_type, year=year, code=code, check_file=False) area_path = get_report_path(unit_type='area', report_type=report_type, year=year, code=code, check_file=False) touch(dept_path) touch(area_path)
def handle(self, *args, **options): database = (options['database'] if options['database'] is not None else 'default') with in_database(database, write=True): cvn_file = os.path.join(st_cvn.MIGRATION_ROOT, 'users_to_migrate.csv') with open(cvn_file, 'rb') as csvfile: lines = csv.reader(csvfile, dialect=st.CSV_DIALECT) for line in lines: user, created = UserProfile.get_or_create_user( username=unicode(line[0]), documento=unicode(line[1])) if created: user.first_name = line[3] user.last_name = line[4] user.save() # Reload user to have profile updated user = User.objects.get(pk=user.profile.user.pk) try: pdf_file = os.path.join(st_cvn.MIGRATION_ROOT, line[2]) upload_file = open(pdf_file) except IOError: print( u'[%s] \t \t ERROR: CVN No encontrado (%s - %s)' % (lines.line_num, line[0], line[2])) continue cvn_file = SimpleUploadedFile( upload_file.name, upload_file.read(), content_type="application/pdf") upload_file.close() try: user.profile.cvn.remove() user.profile.cvn.delete() except ObjectDoesNotExist: pass form = UploadCVNForm(initial={'cvn_file': cvn_file}, user=user) if form.is_valid(): cvn = form.save() cvn.insert_xml() print u'[%s] Usuario: %s - CVN: %s \t \t OK' % ( lines.line_num, line[0], line[2]) else: print u'[%s] \t \t ERROR: CVN No válido (%s - %s)' % ( lines.line_num, line[0], line[2])
def handle(self, *args, **options): table, name_field, year, database = self.check_args(options) with in_database(str(database), write=True): log_print("Haciendo copia de seguridad de BD") error = backup_database(database) if error: log_print(error) else: print('Realizando consultas') registros = self.run_queries(options, table) registros = [p for p in registros] print('Buscando parejas de duplicados') duplicates = self.find_duplicates(registros, name_field, 2) sorted_pairs = sorted(duplicates, key=duplicates.get, reverse=True) signal.signal(signal.SIGINT, signal_handler) pairs_solved = self.confirm_duplicates(sorted_pairs, table, name_field, duplicates) self.commit_changes(table, pairs_solved)
def handle(self, *args, **options): if options['unit'] != 'dept' and options['unit'] != 'area': raise CommandError( 'You must specify the of unit: ' 'python manage.py fix_unit_members -u {dept|area}') unit = options['unit'] database = (options['database'] if options['database'] is not None else 'default') ReportUnit = ReportDept if unit == 'dept' else ReportArea unit_field = 'COD_DEPARTAMENTO' if unit == 'dept' else 'COD_AREA' with open(os.path.join(st.BASE_DIR, unit + '_membership.csv')) as csv: reader = unicodecsv.DictReader(csv, delimiter=',') with in_database(database, write=True): for row in reader: member = ReportMember.objects.get( user_profile__rrhh_code=row['COD_PERSONA']) department = ReportUnit.objects.get(code=row[unit_field]) member.department = department member.save() if options['delempty']: ReportDept.objects.filter(reportmember=None).delete()
def create_all(cls, year): with in_database(str(year), write=True): for up in UserProfile.objects.all(): cls.objects.create(user_profile=up)
def get_all_units_names(cls, year): with in_database(str(year)): return list(cls.Report.objects.all())
def get_unit_name(cls, code, year): with in_database(str(year)): return cls.Report.objects.get(code=code).name
def get_all_units(self): with in_database(str(self.year)): return [ r.code for r in self.Report.objects.exclude( reportmember=None).order_by('name') ]