def post(request, template_name='comments/comments.html'): """Post a comment""" # Fill out some initial data fields from an authenticated user, if present data = request.POST.copy() if request.user.is_authenticated: if not data.get('name', ''): data["name"] = \ request.user.get_full_name() or request.user.username if not data.get('email', ''): data["email"] = request.user.email # Look up the object we're trying to comment about ctype = data.get("content_type") object_pk = data.get("object_pk") model = utils.get_model(ctype) target = model._default_manager.get(pk=object_pk) # Construct the comment form form = comments.get_form()(target, data=data) if not form.is_valid(): context_data = { 'object': target, 'form': form, } return render(request, template_name, context=context_data) # Otherwise create the comment comment = form.get_comment_object() comment.ip_address = request.META.get("REMOTE_ADDR", None) if request.user.is_authenticated: comment.user = request.user # Signal that the comment is about to be saved comments.signals.comment_will_be_posted.send( sender=comment.__class__, comment=comment, request=request ) # Save the comment and signal that it was saved comment.is_removed = False comment.save() comments.signals.comment_was_posted.send( sender=comment.__class__, comment=comment, request=request ) context_data = { 'object': target, 'form': form, } return render(request, template_name, context=context_data)
def get_comment_target_object(content_type: str, object_pk: int): """Get corresponding model object :param content_type: a string representing the model's content type in format ``app_name.model_name``, for example, ``testcases.testcase``. :type content_type: str :param object_pk: the pk of the object. :type object_pk: int :return: the model object according to the content type. """ return utils.get_model(content_type)._default_manager.get(pk=object_pk)
def add(self): import time import django_comments as comments comment_form = comments.get_form() model = utils.get_model(self.content_type) targets = model._default_manager.filter(pk__in=self.object_pks) for target in targets.iterator(): d_form = comment_form(target) timestamp = str(time.time()).split('.')[0] object_pk = str(target.pk) data = { 'content_type': self.content_type, 'object_pk': object_pk, 'timestamp': timestamp, 'comment': self.comment } security_hash_dict = { 'content_type': self.content_type, 'object_pk': object_pk, 'timestamp': timestamp } data['security_hash'] = d_form.generate_security_hash( **security_hash_dict) form = comment_form(target, data=data) # Response the errors if got if not form.is_valid(): return form.errors # Otherwise create the comment comment = form.get_comment_object() comment.ip_address = self.request.META.get("REMOTE_ADDR", None) if self.request.user.is_authenticated: comment.user = self.request.user # Signal that the comment is about to be saved comments.signals.comment_will_be_posted.send( sender=comment.__class__, comment=comment, request=self.request ) # Save the comment and signal that it was saved comment.save() comments.signals.comment_was_posted.send( sender=comment.__class__, comment=comment, request=self.request ) return
def update_case_run_status(request): """Update Case Run status.""" now = datetime.datetime.now() data = request.POST.copy() ctype = data.get("content_type") vtype = data.get('value_type', 'str') object_pk_str = data.get("object_pk") field = data.get('field') value = data.get('value') object_pk = [int(a) for a in object_pk_str.split(',')] if not field or not value or not object_pk or not ctype: return say_no( 'Following fields are required - content_type, ' 'object_pk, field and value.') # Convert the value type # FIXME: Django bug here: update() keywords must be strings field = str(field) value, error = get_value_by_type(value, vtype) if error: return say_no(error) has_perms = check_permission(request, ctype) if not has_perms: return say_no('Permission Dinied.') model = utils.get_model(ctype) targets = model._default_manager.filter(pk__in=object_pk) if not targets: return say_no('No record found') if not hasattr(targets[0], field): return say_no('%s has no field %s' % (ctype, field)) if hasattr(targets[0], 'log_action'): for t in targets: try: t.log_action( who=request.user, field=field, original_value=getattr(t, field), new_value=TestCaseRunStatus.id_to_name(value)) except (AttributeError, User.DoesNotExist): pass objects_update(targets, **{field: value}) if hasattr(model, 'mail_scene'): from tcms.core.utils.mailto import mailto mail_context = model.mail_scene( objects=targets, field=field, value=value, ctype=ctype, object_pk=object_pk, ) if mail_context: mail_context['context']['user'] = request.user mailto(**mail_context) # Special hacking for updating test case run status if ctype == 'testruns.testcaserun' and field == 'case_run_status': for t in targets: field = 'close_date' t.log_action( who=request.user, field=field, original_value=getattr(t, field) or '', new_value=now) if t.tested_by != request.user: field = 'tested_by' t.log_action( who=request.user, field=field, original_value=getattr(t, field) or '', new_value=request.user) field = 'assignee' try: assignee = t.assginee if assignee != request.user: t.log_action( who=request.user, field=field, original_value=getattr(t, field) or '', new_value=request.user) # t.assignee = request.user t.save() except (AttributeError, User.DoesNotExist): pass targets.update(close_date=now, tested_by=request.user) return HttpResponse(json.dumps({'rc': 0, 'response': 'ok'}))
def update_case_run_status(request): """Update Case Run status.""" now = datetime.datetime.now() data = request.POST.copy() ctype = data.get("content_type") vtype = data.get('value_type', 'str') object_pk_str = data.get("object_pk") field = data.get('field') value = data.get('value') object_pk = [int(a) for a in object_pk_str.split(',')] if not field or not value or not object_pk or not ctype: return say_no('Following fields are required - content_type, ' 'object_pk, field and value.') # Convert the value type # FIXME: Django bug here: update() keywords must be strings field = str(field) value, error = get_value_by_type(value, vtype) if error: return say_no(error) has_perms = check_permission(request, ctype) if not has_perms: return say_no('Permission Dinied.') model = utils.get_model(ctype) targets = model._default_manager.filter(pk__in=object_pk) if not targets: return say_no('No record found') if not hasattr(targets[0], field): return say_no(f'{ctype} has no field {field}') if hasattr(targets[0], 'log_action'): for t in targets: try: t.log_action(who=request.user, field=field, original_value=getattr(t, field), new_value=TestCaseRunStatus.id_to_name(value)) except (AttributeError, User.DoesNotExist): pass objects_update(targets, **{field: value}) if hasattr(model, 'mail_scene'): from tcms.core.mailto import mailto mail_context = model.mail_scene( objects=targets, field=field, value=value, ctype=ctype, object_pk=object_pk, ) if mail_context: mail_context['context']['user'] = request.user mailto(**mail_context) # Special hacking for updating test case run status if ctype == 'testruns.testcaserun' and field == 'case_run_status': for t in targets: field = 'close_date' t.log_action(who=request.user, field=field, original_value=getattr(t, field) or '', new_value=now) if t.tested_by != request.user: field = 'tested_by' t.log_action(who=request.user, field=field, original_value=getattr(t, field) or '', new_value=request.user) field = 'assignee' try: assignee = t.assginee if assignee != request.user: t.log_action(who=request.user, field=field, original_value=getattr(t, field) or '', new_value=request.user) # t.assignee = request.user t.save() except (AttributeError, User.DoesNotExist): pass targets.update(close_date=now, tested_by=request.user) return HttpResponse(json.dumps({'rc': 0, 'response': 'ok'}))
def update(request): """Generic approach to update a model, based on contenttype.""" now = datetime.datetime.now() data = request.POST.copy() ctype = data.get("content_type") vtype = data.get('value_type', 'str') object_pk_str = data.get("object_pk") field = data.get('field') value = data.get('value') object_pk = [int(a) for a in object_pk_str.split(',')] if not field or not value or not object_pk or not ctype: return JsonResponseBadRequest({ 'message': 'Following fields are required - ' 'content_type, object_pk, field and value.' }) # Convert the value type # FIXME: Django bug here: update() keywords must be strings field = str(field) value, error = get_value_by_type(value, vtype) if error: return JsonResponseBadRequest({'message': error}) has_perms = check_permission(request, ctype) if not has_perms: return JsonResponseForbidden({'message': 'Permission Denied.'}) model = utils.get_model(ctype) targets = model._default_manager.filter(pk__in=object_pk) if not targets: return JsonResponseBadRequest({'message': 'No record found'}) if not hasattr(targets[0], field): return JsonResponseBadRequest( {'message': f'{ctype} has no field {field}'}) if hasattr(targets[0], 'log_action'): for t in targets: try: log_info = { 'who': request.user, 'field': field, 'new_value': value, } original_value = getattr(t, field) if original_value is None: log_info['original_value'] = 'None' t.log_action(**log_info) except (AttributeError, User.DoesNotExist): pass objects_update(targets, **{field: value}) if hasattr(model, 'mail_scene'): mail_context = model.mail_scene( objects=targets, field=field, value=value, ctype=ctype, object_pk=object_pk, ) if mail_context: from tcms.core.mailto import mailto mail_context['context']['user'] = request.user mailto(**mail_context) # Special hacking for updating test case run status if ctype == 'testruns.testcaserun' and field == 'case_run_status': for t in targets: field = 'close_date' t.log_action(who=request.user, field=field, original_value=getattr(t, field), new_value=now) if t.tested_by != request.user: field = 'tested_by' t.log_action(who=request.user, field=field, original_value=getattr(t, field), new_value=request.user) field = 'assignee' try: assignee = t.assginee if assignee != request.user: t.log_action(who=request.user, field=field, original_value=getattr(t, field), new_value=request.user) # t.assignee = request.user t.save() except (AttributeError, User.DoesNotExist): pass targets.update(close_date=now, tested_by=request.user) return JsonResponse({})