示例#1
0
    def handle(self, *args, **kwargs):
        MAX_DESCRIPTION_LENGTH = Log._meta.get_field('description').max_length

        e_query = Event.objects.filter(
            Q(end_date__lt=datetime.datetime.today())
            | Q(end_date=datetime.datetime.today(),
                end_time__lte=timezone.now()))
        e_query = [e for e in e_query if e.s_calendar.nonprofit]
        #^ get events that are done and have an assoicated nonprofit calendar
        a_query = Attendee.objects.filter(
            event__in=e_query, log__isnull=True, user__isnull=False
        )  #get attendees who were at a completed event and who do not have logs
        if not a_query:
            self.stdout.write("All attendee events have logs created")
            sys.exit()
        for a in a_query:
            e = a.event
            np = e.s_calendar.nonprofit
            duration = e.end_datetime - e.start_datetime
            description_header = 'Automatically created log for the attended event "%s":\n' % e.s_title
            log = Log(attendee=a,
                      user=a.user,
                      nonprofit=np,
                      duration=duration,
                      start_date=e.start_date,
                      description=(description_header +
                                   e.s_description)[:MAX_DESCRIPTION_LENGTH])
            log.save()
            self.stdout.write("Log created for attendee " + str(a) +
                              ", event " + str(e.s_title))
示例#2
0
def send_json(
    user,
    action: Action,
    log_item: Log,
    action_info: JSONPayload,
):
    """Send json objects to target URL.

    Sends the json objects evaluated per row to the URL in the action

    :param user: User object that executed the action

    :param action: Action from where to take the messages

    :param token: String to include as authorisation token

    :param key_column: Key column name to use to exclude elements (if needed)

    :param exclude_values: List of values to exclude from the mailing

    :param log_item: Log object to store results

    :return: Nothing
    """
    # Evaluate the action string and obtain the list of list of JSON objects
    action_evals = evaluate_action(
        action,
        column_name=action_info['item_column'],
        exclude_values=action_info['exclude_values'],
    )

    # Create the headers to use for all requests
    headers = {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Authorization': 'Bearer {0}'.format(action_info['token']),
    }

    # Create the context for the log events
    context = {
        'user': user.id,
        'action': action.id,
    }

    # Iterate over all json objects to create the strings and check for
    # correctness
    for json_string in action_evals:
        send_and_log_json(
            user,
            action,
            json.loads(json_string[0]),
            headers,
            context,
        )

        # Update data in the overall log item
    log_item.payload['objects_sent'] = len(action_evals)
    log_item.payload['filter_present'] = action.get_filter() is not None
    log_item.payload['datetime'] = str(
        datetime.datetime.now(pytz.timezone(ontask_settings.TIME_ZONE)))
    log_item.save()
示例#3
0
class LogTest(EngineTestCase):
	def setUp(self):
		super(LogTest, self).setUp()

		self.l = Log(
			turn=1,
			game=self.g,
			delta=1,
			hide_for_players=False,
			public=True,
			event_type=Game.OPE_PROTECTION,
			data=json.dumps({
				"player": "testplayer",
				"market": "testmarket",
				"corporation": "testcorporation"
			}),
			corporation=self.c,
			corporation_market=self.c.get_random_corporation_market()
		)

		self.l.save()

	def test_log_display(self):
		"""
		Logs should be displayed properly, according to parameters
		"""
		self.assertEqual(self.l.get_display(display_context="player", is_personal=True), u"<p>Vous avez lancé une opé de protection sur le marché testmarket de testcorporation</p>")

		self.assertEqual(self.l.get_display(display_context="corporation", is_personal=False), u"<p>testplayer a lancé une opé de protection sur le marché testmarket</p>")

	def test_logs_retrieval_for_player(self):
		pass
示例#4
0
 def initlog(self):
     """
     Creates a log entry for the person.
     """
     log = Log(maxlength=40, minlevel=logging.INFO)
     log.save()
     self.log = log
示例#5
0
def publish(request):
    """
    Publish page: Simply render the publish form if the request method is GET, or actually publishes (create or modify)
    a blog if the request method if POST.
    Note that creating and modifying requires different permissions.
    """
    # The permission authenticating part is the same, no matter which method the request is.
    if (('id' in request.GET or 'id' in request.POST) and not request.user.has_perm('blog.add_blog')) \
            or not request.user.has_perm('blog.change_blog'):
        raise PermissionDenied

    # Check the request method to distinguish between page requests and actual publishes.
    if request.method == 'GET':
        context = get_universal_context('', False)
        # Add field 'stit' so that the edit option will not appear in the top-right menu again.
        context['stit'] = ''
        # Get an existing blog from database and fill out the default values, if an `id` is given. Otherwise, this is
        # creation, nothing to be done.
        if 'id' in request.GET:
            try:
                context.update(
                    model_to_dict(
                        Blog.objects.get(id=int(request.GET.get('id')))))
            except Blog.DoesNotExist or ValueError:
                raise Http404()
        return render(request, 'blog-publish.html', context)

    if request.method == 'POST':
        if 'id' in request.POST:
            try:
                blog = Blog.objects.get(id=int(request.POST.get('id')))
            except Blog.DoesNotExist or ValueError:
                raise Http404()
        else:
            # This is required because django refuses to create a new blog object with empty publish date.
            blog = Blog.objects.create(
                publish_date=request.POST.get('publish_date'))
        # Handle static resources first.
        # Validate the uploaded files first, if any of them is invalid, the whole publish request will not be handled.
        for image in request.FILES.getlist('static_files'):
            if image.size > 10485760:  # 10M
                return JsonResponse({'error': image.name + '体积过大'})
        for image in request.FILES.getlist('static_files'):
            # Although we do not restrict the type of uploaded files, we still store all those files under `image`
            # directory.
            # TODO this may be improvable.
            with open(os.path.join(settings.STATIC_ROOT, 'images', image.name),
                      'wb') as f:
                for chunk in image.chunks():
                    f.write(chunk)
        # Iterate through all fields and update them. It is guaranteed that the POST parameters' name is the same as
        # database columns.
        for field in Blog._meta.fields:
            if field.name != 'id':
                blog.__setattr__(field.name, request.POST.get(field.name, ''))
        blog.save()
        # Since publishing blogs require certain privileges, we only log if a publish succeeded.
        Log.new_log(request, 'blog', 'publish', str(blog.id))
        return redirect('blog-content', path=blog.publish_path +
                        '/')  # the trailing slash is vital
示例#6
0
def add_log(title, description=''):
    action = Action()
    action.description = description
    log = Log()
    log.title = title
    log.actions.append(action)
    log.save()
示例#7
0
def parse_log_entry_to_log_model(logentry: LogEntry) -> Log:
    """
    Converts LogEntry instance to apache DB model Log
    :param logentry: LogEntry object instance
    :return: Log object instance
    """
    if logentry is None:
        return Log()

    url = logentry.headers_in["Referer"]
    urn = logentry.request_line.split()[1]
    uri = url  # TODO correctly concatenate URI from URL+URN

    log = Log(
        ip_address=logentry.remote_host,
        created_date=logentry.request_time,
        http_method=logentry.request_line.split()[0],
        uri=uri,
        url=url,
        urn=urn,
        response_code=logentry.final_status,
        content_length=logentry.bytes_sent,
        user_agent=logentry.headers_in["User-Agent"],
    )

    log.clean_fields()

    return log
示例#8
0
def content(request, path):
    """
    Blog content page: accept an access path, and return the corresponding page.
    If the path has a matching blog, render that blog.
    If the path has not matching blog, but it is the prefix of one or more blogs (i.e. there exists blogs under this
    directory), render an index page.
    Otherwise, trigger a 404 error.
    """
    path = unquote('/'.join(path[:-1].split('/')))  # remove the trailing slash
    # Add log even if the request failed.
    Log.new_log(request, 'blog', 'access', path)
    context = get_universal_context(path, True)
    # Assume that there is a matching blog.
    try:
        context.update(blog_to_dict(Blog.objects.get(publish_path=path)))
        return render(request, 'blog-content.html', context)
    except Blog.DoesNotExist:
        # If the blog specified by this path does not exists, then check if there are any blogs under this directory. If
        # yes, display an index page. Otherwise, return 404.
        query_set = Blog.objects.filter(
            publish_path__startswith=path).order_by('-publish_date')
        # Note that empty path (i.e. root) will never raise 404, otherwise there will be no entrance if there are no
        # blogs online.
        if len(query_set) == 0 and path != '':
            raise Http404()
        context['page'], context['plim'], context['pcnt'], context[
            'blog'] = utils.paginate(request, 10, query_set)
        context['blog'] = [
            blog_to_dict(blog, False) for blog in context['blog']
        ]
        return render(request, 'blog-indices.html', context)
示例#9
0
def condicoestempo_remove(request, id):
    condicoestempo = CondicaoTempo.objects.get(id=id)
    nome = condicoestempo.nome
    condicoestempo.delete()
    texto = request.user.username + " removeu a condição de tempo: " + nome
    log = Log(log=texto)
    log.save()
    return redirect('/condicoestempo/')
示例#10
0
def ura_remove(request, id):
    ura = URA.objects.get(id=id)
    nome = ura.nome
    ura.delete()
    texto = request.user.username + " removeu a URA: " + nome
    log = Log(log=texto)
    log.save()
    return redirect('/uras/')
示例#11
0
def tronco_remove(request, id):
    tronco = Tronco.objects.get(id=id)
    nome = tronco.nome
    tronco.delete()
    texto = request.user.username + " removeu o tronco: " + nome
    log = Log(log=texto)
    log.save()
    return redirect('/troncos/')
示例#12
0
def anuncio_remove(request, id):
    anuncio = Anuncio.objects.get(id=id)
    nome = anuncio.descricao
    anuncio.delete()
    texto = request.user.username + " removeu o anúncio: " + nome
    log = Log(log=texto)
    log.save()
    return redirect('/anuncios/')
示例#13
0
def fila_remove(request, id):
    fila = Fila.objects.get(id=id)
    nome = fila.nome
    fila.delete()
    texto = request.user.username + " removeu a fila: " +nome
    log = Log(log= texto)
    log.save()

    return redirect('/filas/')
示例#14
0
def chamadasgrupo_remove(request, id):
    chamadasgrupo = ChamadaEmGrupo.objects.get(id=id)
    cham = chamadasgrupo.descricao
    chamadasgrupo.delete()
    texto = request.user.username + ": removeu a chamada em grupo: " +cham
    log = Log(log= texto)
    log.save()

    return redirect('/chamadasgrupo/')
示例#15
0
def numero_remove(request, id):
    numero = NumeroEntrada.objects.get(id=id)
    num = numero.numero
    numero.delete()
    texto = request.user.username + ": removeu o número de entrada: " + num
    log = Log(log=texto)
    log.save()

    return redirect('/numeros/')
def save_log(member, timestamp, status):
    working_day, _ = WorkingDay.objects.get_or_create(date=timestamp.date())
    log = Log(
        member_fk=member,
        timestamp=timestamp,
        status=status,
        working_day=working_day,
    )
    log.save()

    return log
 def create(self, validated_data):
     request = self.context.get('request')
     user = request.user
     server_name = validated_data.get('server_name')
     type = validated_data.get('type')
     message = validated_data.get('message')
     text = validated_data.get('text')
     log_obj = Log(server_name=server_name, type=type,
                   message=message, text=text)
     log_obj.user = user
     log_obj.save()
     return log_obj
示例#18
0
 def create_invalid_login_log(self, serializer):
     username = self.request.data['username']
     if username:
         try:
             Log(
                 type=Log.Types.INVALID_LOGIN,
                 user=User.objects.get(username=username),
             ).save()
         except User.DoesNotExist:
             Log(type=Log.Types.INVALID_LOGIN).save()
     else:
         Log(type=Log.Types.INVALID_LOGIN).save()
示例#19
0
 def save_log(data: tuple) -> bool:
     try:
         log = Log()
         log.ip = data[0]
         log.date = datetime.strptime(data[1], "%d/%b/%Y:%H:%M:%S %z")
         log.method = data[2].split()[0]
         log.uri = data[2].split()[1]
         log.status_code = int(data[3]) if data[3].isdigit() else None
         log.size = int(data[4]) if data[4].isdigit() else None
         log.save()
     except Exception as e:
         logger.error(f"Error {e} on saving: {''.join(data)}")
         return False
     return True
示例#20
0
def send_emails(
    user,
    action: Action,
    log_item: Log,
    action_info: EmailPayload,
) -> None:
    """Send action content evaluated for each row.

    Sends the emails for the given action and with the
    given subject. The subject will be evaluated also with respect to the
    rows, attributes, and conditions.

    The messages are sent in bursts with a pause in seconds as specified by the
    configuration variables EMAIL_BURST  and EMAIL_BURST_PAUSE

    :param user: User object that executed the action
    :param action: Action from where to take the messages
    :param log_item: Log object to store results
    :param action_info: Mapping key, value as defined in EmailPayload

    :return: Send the emails
    """
    # Evaluate the action string, evaluate the subject, and get the value of
    # the email column.
    action_evals = evaluate_action(
        action,
        extra_string=action_info['subject'],
        column_name=action_info['item_column'],
        exclude_values=action_info['exclude_values'])

    check_cc_lists(action_info['cc_email'], action_info['bcc_email'])

    track_col_name = ''
    if action_info['track_read']:
        track_col_name = create_track_column(action)
        # Get the log item payload to store the tracking column
        log_item.payload['track_column'] = track_col_name
        log_item.save()

    msgs = create_messages(
        user,
        action,
        action_evals,
        track_col_name,
        action_info,
    )

    deliver_msg_burst(msgs)

    # Update data in the log item
    log_item.payload['objects_sent'] = len(action_evals)
    log_item.payload['filter_present'] = action.get_filter() is not None
    log_item.payload['datetime'] = str(
        datetime.datetime.now(pytz.timezone(ontask_settings.TIME_ZONE)), )
    log_item.save()

    if action_info['send_confirmation']:
        # Confirmation message requested
        send_confirmation_message(user, action, len(msgs))
示例#21
0
 def parse_all(self, file_path: str):
     t1 = time.time()
     logger.info('Starting saving file to db')
     with open(file_path, 'r') as f:
         next(f)  # первая строка - пустая
         bulk_mng = BulkCreateManager()
         for entry in self.parser.parse_lines(f, ignore_invalid=True):
             ip_address = entry.remote_host
             timestamp = entry.request_time
             response_status_code = entry.final_status
             http_method, request_path, http_protocol = entry.request_line.split(
             )
             content_length = entry.bytes_sent
             user_agent = entry.headers_in['User-Agent']
             referer = entry.headers_in['Referer']
             bulk_mng.add(
                 Log(ip_address=ip_address,
                     timestamp=timestamp,
                     response_status_code=response_status_code,
                     http_method=http_method,
                     request_path=request_path,
                     http_protocol=http_protocol,
                     content_length=content_length,
                     user_agent=user_agent,
                     referer=referer))
         bulk_mng.done()  # удостовериться что все данные добавились в бд
         logger.info(f'It took {time.time() - t1} seconds')
示例#22
0
def write_log_to_db(log: Log):
    """
    Saves apache Log instance to DB
    :param log: Log class instance
    :return:
    """
    return log.save()
    def create(self, validated_data):
        password = validated_data.pop("password")
        user = User(**validated_data)
        user.set_password(password)
        user.save()

        Log(type=Log.Types.SIGN_UP, user=user).save()
        return user
示例#24
0
def do_workflow_update_lusers(workflow: Workflow, log_item: Log):
    """Recalculate the field lusers.

    Recalculate the elements in the field lusers of the workflow based on the
    fields luser_email_column and luser_email_column_MD5

    :param workflow: Workflow to update

    :param log_item: Log where to leave the status of the operation

    :return: Changes in the lusers ManyToMany relationships
    """
    # Get the column content
    emails = get_rows(workflow.get_data_frame_table_name(),
                      column_names=[workflow.luser_email_column.name])

    luser_list = []
    created = 0
    for row in emails:
        uemail = row[workflow.luser_email_column.name]
        luser = get_user_model().objects.filter(email=uemail).first()
        if not luser:
            # Create user
            if settings.DEBUG:
                # Define users with the same password in development
                password = '******'  # NOQA
            else:
                password = get_random_string(length=RANDOM_PWD_LENGTH)
            luser = get_user_model().objects.create_user(
                email=uemail,
                password=password,
            )
            created += 1

        luser_list.append(luser)

    # Assign result
    workflow.lusers.set(luser_list)

    # Report status
    log_item.payload['total_users'] = emails.rowcount
    log_item.payload['new_users'] = created
    log_item.payload['status'] = ugettext(
        'Learner emails successfully updated.', )
    log_item.save()
示例#25
0
def indices(request):
    """
    Search page: accept a keyword and search for it in titles and tags. Render the search result as an index page.
    """
    keyword = unquote(request.GET.get('keyword', ''))
    # Add log in all cases.
    Log.new_log(request, 'blog', 'search', keyword)
    # We should disable subdirectories since this is not a real access path.
    context = get_universal_context('', False)
    query_set = Blog.objects \
        .filter(Q(content_name__icontains=keyword) | Q(content_tags__icontains=keyword)) \
        .order_by('-publish_date')
    context['page'], context['plim'], context['pcnt'], context[
        'blog'] = utils.paginate(request, 10, query_set)
    context['blog'] = [blog_to_dict(blog, False) for blog in context['blog']]
    # This parameter is used to fill out the default value of the search bar.
    context['skey'] = keyword
    return render(request, 'blog-indices.html', context)
示例#26
0
def importation_launch(request):
    if request.method == "POST":
        form = XMLImportationForm(request.POST, request.FILES)
        if form.is_valid():
            importation = form.save(commit=False)
            log = Log(maxlength=2000, minlevel=logging.INFO)
            log.save()
            importation.log = log
            importation.save()
            messages.info(request, _("Queued importation"))
            return HttpResponseRedirect(
                        reverse('importation_status',
                                kwargs={'importid': importation.pk}))
        else:
            messages.error(request, _("Form error"))
            return render(request, "importation/launch.html",
                              {'form': form})
    else:
        form = XMLImportationForm()
    return render(request, "importation/launch.html", {'form': form})
示例#27
0
def index(request):
    if request.user.role != 1:
        problems = Problem.objects.filter(user_id=request.user.id)
    else:
        problems = Problem.objects.all()
    template_name = 'problems/index.html'
    context = {}
    form = ProblemForm(request.POST or None)
    if form.is_valid():
        form.save()
        form = ProblemForm()
        problem_id = Problem.objects.latest('id')
        log = Log(title='Problema criado', description='', problem=problem_id)
        log.save()
        messages.success(request, 'Problema inserido com sucesso!')
    context['items'] = Item.objects.all()
    context['problems'] = problems
    context['form'] = form
    context['form'].fields['user'].initial = request.user.id

    return render(request, template_name, context)
示例#28
0
    def post(self, *args, **kwargs):
        blocked = get_object_or_404(User.objects, username=kwargs["username"])
        _, created = Block.objects.get_or_create(
            blocker=self.request.user, blocked=blocked
        )

        if created:
            Log(
                type=Log.Types.BLOCK, user=self.request.user, affected_user=blocked
            ).save()

        return Response(data={}, status=status.HTTP_201_CREATED)
示例#29
0
    def get_img_rnd(self, rnd_field, rnd_generator, request):
        if self.cleaned_data[rnd_field]:
            result_img = None
            rnd = rnd_generator(self.cleaned_data[rnd_field])
        else:
            if self.cleaned_data['url']:
                response = requests.get(self.cleaned_data['url'])
                f = ContentFile(response.content, response.url.split('/')[-1])
            else:
                f = self.cleaned_data['upload']
            img = Image(**self.get_img_params(request))
            img.save_image(f)
            result_img = img.save()

            if result_img.banned:
                Log.write(Log.TYPE_VALIDATION, "This image is banned", request, request.POST)
                raise BannedImageException("This image is banned")

            Image.objects.filter(pk=result_img.pk).update(uses=F('uses') + 1)

            rnd = 0
        return result_img, rnd
示例#30
0
def upload_file_cad(request):
    if request.method == 'POST':
        myfile = request.FILES['anexGravacao']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)

        nome = request.POST['nomeGravacao']
        tipo = '2'
        gravacao = Gravacao(nome=nome, link=uploaded_file_url, tipo=tipo)
        gravacao.save()

        texto = request.user.username + ": adicionou a gravação: " + nome
        log = Log(log=texto)
        log.save()

        gravacoes = Gravacao.objects.exclude(musica__isnull=False)
        data = {}
        data['gravacao'] = gravacao
        data['gravacoes'] = gravacoes
        dest_anuncios = Anuncio.objects.all()
        dest_gravacoes = Gravacao.objects.all()
        dest_numeros = NumeroEntrada.objects.all()
        dest_uras = URA.objects.all()
        dest_filas = Fila.objects.all()
        dest_chamadasGrupo = ChamadaEmGrupo.objects.all()
        dest_condicoes = CondicaoTempo.objects.all()
        dest_troncos = Tronco.objects.all()
        data['dest_anuncios'] = dest_anuncios
        data['dest_gravacoes'] = dest_gravacoes
        data['dest_numeros'] = dest_numeros
        data['dest_uras'] = dest_uras
        data['dest_filas'] = dest_filas
        data['dest_chamadasGrupo'] = dest_chamadasGrupo
        data['dest_condicoes'] = dest_condicoes
        data['dest_troncos'] = dest_troncos
        return render(request, 'CadastroAnuncio.html', data)
    def create(self, validated_data):
        sender = self.context["request"].user
        reciever = self.context["other_user"]

        validated_data["sender"] = sender
        validated_data["reciever"] = reciever
        if Block.objects.filter(blocker=reciever, blocked=sender).exists():
            validated_data["is_blocked"] = True

        message = super().create(validated_data)

        Log(type=Log.Types.MESSAGE_SEND, user=sender,
            affected_user=reciever).save()

        return message
示例#32
0
    def post(self, *args, **kwargs):
        blocked = get_object_or_404(User.objects, username=kwargs["username"])
        delete_count = Block.objects.filter(
            blocker=self.request.user, blocked=blocked
        ).delete()

        if delete_count:
            Log(
                type=Log.Types.UN_BLOCK, user=self.request.user, affected_user=blocked
            ).save()

        Message.objects.filter(
            sender=blocked, reciever=self.request.user, is_blocked=True
        ).update(is_blocked=False)

        return Response(data={}, status=status.HTTP_204_NO_CONTENT)
示例#33
0
	def setUp(self):
		super(LogTest, self).setUp()

		self.l = Log(
			turn=1,
			game=self.g,
			delta=1,
			hide_for_players=False,
			public=True,
			event_type=Game.OPE_PROTECTION,
			data=json.dumps({
				"player": "testplayer",
				"market": "testmarket",
				"corporation": "testcorporation"
			}),
			corporation=self.c,
			corporation_market=self.c.get_random_corporation_market()
		)

		self.l.save()
示例#34
0
 def put(self, request, pk):
     problem = get_object_or_404(Problem, pk=pk)
     last_status = problem.status
     back_url = request.GET.get('next', reverse('problems:index'))
     context = {}
     form = ProblemForm(data=request.POST, instance=problem)
     if form.is_valid():
         if last_status == 0 and problem.status == 1:
             log = Log(title='Problema resolvido',
                       description='',
                       problem=problem)
             log.save()
         elif last_status == 1 and problem.status == 0:
             log = Log(title='Problema não resolvido',
                       description='',
                       problem=problem)
             log.save()
         form.save()
         messages.success(request, 'Problema alterado com sucesso!')
     context['form'] = form
     context['back_url'] = back_url
     return render(request, self.template_name, context)
示例#35
0
def clear():
    Log.clear()
    return redirect(url_for('logs.index'))
示例#36
0
def log_creation(sender, instance, created, **kwargs):
    if created:
        log = Log()
        log.save()
        instance.log = log
        instance.save()
示例#37
0
def logs(log_type='debug', name=None, description=None):
    log = Log(log_type=log_type, name=name, description=description)
    log.save()
示例#38
0
 def emit(self, msg):
     log = Log()
     log.level = msg.levelno
     log.name = msg.name
     log.message = msg.msg
     log.save()