def create_message(request, project_id, message_id): project_id = int(project_id) project = get_object_or_404(Project, id=project_id) if message_id == None: # create_message message = Message(project=project, owner=request.user) message.save() return redirect('create_message_page', project_id=project_id, message_id=message.id) else: message_id = int(message_id) message = get_object_or_404(Message, project=project, owner=request.user, id=message_id) # process form if request.method == 'POST': file_upload_form = FileUploadForm(request.POST, request.FILES) perm_choice_form = PermChoiceForm(request.POST) message_info_form = MessageInfoForm(request.POST) if message_info_form.is_valid(): message.title = message_info_form.cleaned_data['title'] message.description = message_info_form.cleaned_data['description'] message.post_flag = True message.save() # redirect to somewhere if file_upload_form.is_valid() and perm_choice_form.is_valid(): uploaded_file = request.FILES['uploaded_file'] owner_perm = perm_choice_form.cleaned_data['owner_perm'] group_perm = perm_choice_form.cleaned_data['group_perm'] everyone_perm = perm_choice_form.cleaned_data['everyone_perm'] # check unique md5 = gen_MD5_of_UploadedFile(uploaded_file) unique_file = UniqueFile.objects.filter(md5=md5) if not unique_file: unique_file = UniqueFile(md5=md5) unique_file.save() unique_file.file.save(md5, uploaded_file) else: unique_file = unique_file[0] # save file info file_info = FileInfo(name=uploaded_file.name, owner_perm=owner_perm, group_perm=group_perm, everyone_perm=everyone_perm, unique_file=unique_file, message=message) file_info.save() return redirect('create_message_page', project_id=project_id, message_id=message.id) else: file_upload_form = FileUploadForm() perm_choice_form = PermChoiceForm(initial={ 'owner_perm': FileInfo.READ_AND_WRITE, 'everyone_perm': FileInfo.READ, 'group_perm': FileInfo.READ }) message_info_form = MessageInfoForm() # rendering # generate uploaded file list # notice that file_name could be duplicate, # so it can not be the key for dict. display_file_info = {file_info.id: file_info.name\ for file_info in message.fileinfo_set.all()} render_data_dict = { 'project_id': int(project_id), 'file_upload_form': file_upload_form, 'perm_choice_form': perm_choice_form, 'post_message_form': message_info_form, 'message_id': message.id, 'file_info_list': display_file_info, } return render(request, 'file_info/create_message_page.html', render_data_dict)
def postFile(request): if request.user is not None: file = request.FILES['file'] groupName = request.data['groupName'] type = request.data['type'] qwirkGroup = QwirkGroup.objects.get(name=groupName) #TODO security #if request.user.qwirkuser in qwirkGroup.qwirkuser_set.all() or request.user.qwirkuser in qwirkGroup.contact_set.all(): message = Message(qwirkUser=request.user.qwirkuser, qwirkGroup=qwirkGroup, text=request.user.username + " upload a file", dateTime=datetime.now(), type=type) message.file.save(file.name, file) message.save() messageSerialized = MessageSerializer(message) text = json.dumps({ "action": "new-message", "content": json.dumps(messageSerialized.data) }) Group(groupName).send({ "text": text, }) if qwirkGroup.isContactGroup: for contact in qwirkGroup.contact_set.all(): print(contact.qwirkUser) if contact.qwirkUser != request.user.qwirkuser: notification = Notification.objects.create( message=message, qwirkUser=contact.qwirkUser) notification.save() serializer = NotificationSerializerSimple(notification) text = json.dumps({ "action": "notification", "notification": serializer.data }) Group("user" + contact.qwirkUser.user.username).send({ "text": text, }) else: for qwirkUser in qwirkGroup.qwirkuser_set.all(): print(qwirkUser) if qwirkUser != request.user.qwirkuser: notification = Notification.objects.create( message=message, qwirkUser=qwirkUser) notification.save() serializer = NotificationSerializerSimple(notification) text = json.dumps({ "action": "notification", "notification": serializer.data }) Group("user" + qwirkUser.user.username).send({ "text": text, }) return HttpResponse(status=200) #else: # return HttpResponse(status=403) else: return HttpResponse(status=401)